CSS悬停不适用于子节点
我的 html 设置与此类似,唯一的区别是我的 '.inner 包裹得更深(例如
)aaa
<div class="main">
<div class="inner">aaa</div>
<div class="main anOtherClass">
<div class="inner">aaa</div>
</div>
</div>
我的 css 是:
.main:hover .inner {
border: 1px solid #000;
}
问题是,如果我将鼠标悬停在第一个 .main 上,我的所有.inners得到边界。我的目标只是让第一个 .inner 获得边框。这甚至可以在CSS中做到吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用子选择器:
> 将此选择器限制为
.inner
元素,这些元素是.main
的直接子元素,SitePoint 对其进行了很好的解释:http://reference.sitepoint.com/css/childselector
如果您确定您只想要第一个(如果有两个或多个相邻的)也使用
:first-child
:Use the child selector:
The
>
constrains this selector to.inner
elements that are a direct child of.main
,SitePoint explains it well: http://reference.sitepoint.com/css/childselector
If you're certain you only want the first one (in case there are two or more adjacent ones) use
:first-child
as well: