html - 标题和鼠标悬停

标签 html css

我是 CSS 新手,遇到了一些小问题。我创建了带有标题的标题。标题名称由两部分组成。第一部分只是文本,第二部分是带背景的文本。我想创建一个标题,它会在 :hover 上更改文本颜色和背景。但是,如果您将鼠标悬停在右侧的标题上,则一切正常,但左侧则不然。

这是我的代码

#rus {
  background: red none repeat scroll 0 0;
  border-radius: 8px;
  color: white;
  padding: 4px 5px;
}

#rus:hover {
  background: white;
  border-radius: 8px;
  color: red;
  padding: 4px 5px;
  border: 1px solid red;
}

h1 {
  border: 0;
  font-family: inherit;
  font-size: 100%;
  font-style: inherit;
  font-weight: inherit;
  margin: 0;
  outline: 0;
  padding: 0;
  vertical-align: baseline;
  clear: both;
}

#site-title {
  margin-right: 270px;
  padding: 3em 0 0;
  font-family: 'Ubuntu', sans-serif;
}

#site-title a {
  color: #111;
  font-size: 30px;
  font-weight: bold;
  line-height: 36px;
  text-decoration: none;
}

#site-title a:hover,
#site-title a:focus,
#site-title a:active {
  color: #1982d1;
}
<hgroup>
  <h1 id="site-title">
    <a href="#" rel="home">Bundesliga 
      <span id="rus">RUS</span>
    </a>
  </h1>
</hgroup>

如您所见,将 span 标签放入 link 标签中。如果我从右侧悬停它会影响链接标签和跨度,因为无论如何鼠标都在链接标签上。但是从右侧鼠标悬停仅触及链接标签并且跨度不受影响。 我应该更改什么以使整个标题在鼠标悬停时更改其 CSS 属性?

最佳答案

您需要做的就是将 :hover 从您的 span 移动到父级 a:

#rus {
    background: red;
    border-radius: 8px;
    color: white;
    padding: 4px 5px;
}

a:hover #rus {
    background: white;
    border-radius: 8px;
    color: red;
    padding: 4px 5px;
    border: 1px solid red;
}

h1 {
    border: 0;
    font-family: inherit;
    font-size: 100%;
    font-style: inherit;
    font-weight: inherit;
    margin: 0;
    outline: 0;
    padding: 0;
    vertical-align: baseline;
    clear: both;
}

#site-title {
    margin-right: 270px;
    padding: 3em 0 0;
    font-family: 'Ubuntu', sans-serif;
}

#site-title a {
    color: #111;
    font-size: 30px;
    font-weight: bold;
    line-height: 36px;
    text-decoration: none;
}

#site-title a:hover,
#site-title a:focus,
#site-title a:active {
    color: #1982d1;
}
<hgroup>
  <h1 id="site-title">
    <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">Bundesliga 
      <span id="rus">RUS</span>
    </a>
  </h1>
</hgroup>

关于html - 标题和鼠标悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42589887/

相关文章:

c# - 使用 LINQ to XML 遍历 HTML 表

html - 背景附件:修复了在 Firefox 中无法工作的问题

html - ios safari 中的 flex 元素离开屏幕

jquery - 我如何使 liteAccordion(jQuery 的水平 Accordion 插件)在 IE6 中工作

javascript - 如何在 dragover/dragenter HTML 5 拖放期间更改图标

javascript - Handlebars 查找以生成 href

javascript - 如何在网页上隔离包含第 3 方 HTML 片段?

css - 在 CSS 或 SASS 中,是否不建议使用 "#foo #bar { width: 200px }"来增加文件大小?

css - 如何在不同的 React 组件之间重用 styled-components?

javascript - 检测多个元素何时位于浏览器屏幕顶部