html - 悬停父元素会触发多个元素,包括自身

标签 html css twitter-bootstrap

HTML

h3,
h2 {
  color: green;
  -webkit-transition: all 1s ease-out;
  -moz-transition: all 1s ease-out;
  -o-transition: all 1s ease-out;
  transition: all 1s ease-out;
}

h2:hover,
h3:hover {
  color: red;
}
<ul class="nav justify-content-between">
  <li class="nav-item">
    <h3>Sibling</h3>
  </li>
  <li class="nav-item">
    <h2>Parent</h2>
  </li>
  <li class="nav-item">
    <h3>Sibling</h3>
  </li>
</ul>

是否可以让父标题在悬停在其自身和兄弟标题上时触发颜色变化,但是,当兄弟标题悬停在上面时,它不会触发任何元素?我使用 bootstrap 来设置列表的样式等。 也可以只使用 css 来做到这一点吗?

工作 JSFIDDLE 链接: https://jsfiddle.net/jd9nenwa/6/

最佳答案

检查这段代码。我不得不稍微更改层次结构。

h3,
h2 {
  color: green;
  -webkit-transition: all 1s ease-out;
  -moz-transition: all 1s ease-out;
  -o-transition: all 1s ease-out;
  transition: all 1s ease-out;
}

h2:hover~.heading, h2:hover {
  color: red;
}
  <h2 class="heading">Parent</h2>
  <h3 class="heading">Sibling</h3>
  <h3 class="heading">Sibling</h3>

关于html - 悬停父元素会触发多个元素,包括自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812333/

相关文章:

javascript - 更改多个复选框的 CSS 样式不起作用

html - 如何在 Razor CheckBoxFor() 中设置 Bootstrap 复选框

twitter-bootstrap - 如何删除 Bootstrap 文件输入中的上传按钮

jquery - Bootstrap : change class of tr in a table when radio button selected

javascript - 通过排队预加载图像?

javascript - 如何在点击以下链接时更改其颜色?

javascript - 获取包含 svg 的 DOM 元素的边界框的正确方法是什么

php - 需要通过 css 将颜色添加到 html 表格

css - 在表格前面显示弹出菜单

javascript - 响应元素显示 :none; at mobile width then brought back by javascript button but links break on display:block;