css - .class a :hover vs . class:hover a

标签 css hover

找不到我一直想知道的问题的答案。

.class2 a:hover {}.class2:hover a {} 有区别吗?还是偏爱其中之一?

我一直在使用 .class2 a:hover {} 来改变 anchor 悬停时的 anchor (即: anchor 文本颜色),但是当我想改变持有 anchor 的 div 时(即: anchor 颜色和 div 背景颜色都会在悬停时发生变化),我必须使用 .class2:hover a {} 才能正常工作。在这样做的过程中,我对两者之间的区别感到困惑,因为它们的写法非常相似。

谢谢!

编辑
编辑问题更清楚。感谢您解开我的大脑:)

最佳答案

我的理解是这样的:

.class2 a:hover 将在 a 标签悬停时定位 .class2 元素内的任何超链接标签。

.class2:hover a 将在 .class2 元素中定位任何超链接 a 标签悬停。

区别在于您将鼠标悬停在哪个元素上以更改这些样式规则。

例子:

.box{
  background: red;
  width: 50px;
  height: 50px;
  margin-bottom: 10px;
}
.case1 a:hover {
  background: blue;
}  

.case2:hover a {
   background: green;
}
<html>
  <body>
    <div class="box case1"><a href="#">case 1</a></div>
    <div class="box case2"><a href="#">case 2</a></div>
  </body>
</html>

在这种情况下,您想将鼠标悬停在 .class2 元素还是 a 上?

关于css - .class a :hover vs . class:hover a,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396880/

相关文章:

Jquery Accordion :hover action does not trigger when a section has previously been opened

html - CSS nav Im 构建出现问题,悬停不起作用

javascript - 内部有 2 个 div 的 Flex 盒子,其中一个内部有 Canvas

javascript - 在悬停时使用 Javascript/jQuery 淡化自定义 SVG 的颜色

javascript - 使用 angularjs 时轮播不工作?

css - Suckerfish 下拉子菜单与主菜单不对齐

jQuery 悬停和 IE

html - 线到中心的 CSS 过渡宽度

CSS列高不继承

javascript - Mouseenter 和 mouseleave - 在触发功能前稍等片刻?