为什么 a:hover 在 CSS 中被覆盖?
如果我有这个 CSS:
a:link { color: blue; }
a:hover { color: red; }
#someID a:link { color: black; }
ID 下的链接在悬停时始终显示为黑色。 我知道使用 ID 具有更高的优先级,但是,我不会覆盖 :hover
选择器,仅覆盖 :link
选择器,所以不应该悬停显示为红色?
If I have this CSS:
a:link { color: blue; }
a:hover { color: red; }
#someID a:link { color: black; }
Links under the ID always appears in black on hover. I'm aware that using an ID gives a higher priority, however, I'm not overriding the :hover
selector, only the :link
selector, so shouldn't the hover display in red?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使您将鼠标悬停在链接上,
:link
伪类也适用于该链接。 由于带有 id 的样式更加具体,因此它会覆盖其他样式。:hover
样式完全覆盖:link
样式的唯一原因是它出现在样式表的后面。 如果按以下顺序放置它们::link
样式位于样式表的后面,并覆盖:hover
样式。 当您将鼠标悬停在链接上时,该链接将保持蓝色。要使
:hover
样式适用于黑色链接,您必须使其至少与:link
样式一样具体,并将其放在样式表中的后面:The
:link
pseudo class applies to the link even when you are hovering over it. As the style with the id is more specific it overrides the others.The only reason that the
:hover
style overrides the:link
style at all is that it comes later in the style sheet. If you place them in this order:the
:link
style is later in the style sheet and overrides the:hover
style. The link stays blue when you hover over it.To make the
:hover
style work for the black link you have to make it at least as specific as the:link
style, and place it after it in the style sheet:存在顺序问题,如 W3Schools 中所述:
http://www.w3schools.com/CSS/css_pseudo_classes.asp
There's an order issue, as explained in W3Schools:
http://www.w3schools.com/CSS/css_pseudo_classes.asp