为什么.foo a:link,.foo a:visited {} selector在CSS中覆盖a:hover,a:active {} selector?

编程入门 行业动态 更新时间:2024-10-26 13:26:07
本文介绍了为什么.foo a:link,.foo a:visited {} selector在CSS中覆盖a:hover,a:active {} selector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

示例代码: jsfiddle/RuQNP/

<!DOCTYPE html> < html> < head> < title> Foo< / title> < style type =text / css> a:link,a:visited { color:blue; } a:hover,a:active { color:red; } .foo a:link,.foo a:visited { color:green; } / *可能的修复程序* / / * .foo a:hover,.foo a:active { color:red; } * / < / style> < / head> < body> < div class =foo> < a href =example/>示例< / a> < / div> < / body> < / html>

我预期的:

悬停时链接会显示为红色。

我得到了什么:

悬停时链接显示为绿色。

问题:

  • 为什么中定义颜色 .foo a:link,.foo a:visited 选择器重写 a:hover,a:active 中的一个?这是怎么回事?
  • 我明白我可以通过取消注释注释的代码来修复它并获得我的期望。但是,我想知道我们如何更正 .foo a:link,.foo a:visited 选择器,以便它不会覆盖中定义的颜色 c:a:hover,a:active ?
  • 如果我理解 www.w3/TR/CSS21/cascade.html#specificity (感谢,BoltClock),这是代码中各种选择器的特异性表。

    a:link - 0 0 1 1 a:visited - 0 0 1 1 a:hover - 0 0 1 1 a:active - 0 0 1 1 .foo a:link - 0 0 2 1 .foo a:visited - 0 0 2 1 pre>

    因此,为 .foo a:link 定义的样式覆盖当链接以及 hover 伪类应用于A时,a:hover 同样,为 .foo定义的样式a:访问的 访问以及时,覆盖 a:hover 的样式c> hover 伪类应用于 foo 的A元素。

    解决方案

    当您第一次使用CSS时,您可能已经了解了LoVe-HAte助记符,用于指定链接选择器的顺序( a:link , a:visited , a:hover , a:active )。你是否曾经想过为什么选择这个助记符?

    那么, spec 了解当使用所有规则应用于同一元素时如何处理链接和动态伪类,这解释了为什么你需要按照以下顺序设置链接选择器:

    请注意,A:悬停必须放置在A:链接和A: ,因为否则级联规则将隐藏A:hover规则的'color'属性。类似地,因为A:active放置在A:hover之后,当用户激活并悬停在A元素上时,将应用活动颜色(lime)。

    无论如何,我试图在上面的点是所有四个伪类,作为伪类,具有相等的特异性。关于特殊性的一切都适用。在这种情况下,在一组同样特定的选择器中,应用最后一个规则。

    现在,简单介绍 .foo 选择器会导致您的第二组链接/访问规则覆盖您的第一组链接/访问样式和悬停/活动样式,强制该类的元素中的链接始终显示为绿色,直到您添加悬停/ 对不起,如果我的答案似乎缝合或slipshod的方式,我现在在我的iPhone上打字,这是很难想到这里...

    Sample code: jsfiddle/RuQNP/

    <!DOCTYPE html> <html> <head> <title>Foo</title> <style type="text/css"> a:link, a:visited { color: blue; } a:hover, a:active { color: red; } .foo a:link, .foo a:visited { color: green; } /* A possible fix */ /* .foo a:hover, .foo a:active { color: red; } */ </style> </head> <body> <div class="foo"> <a href="example/">Example</a> </div> </body> </html>

    What I was expecting:

    The link would appear red on hover.

    What I get:

    The link appears green on hover.

    Questions:

  • Why does the color defined in .foo a:link, .foo a:visited selector override the one in a:hover, a:active? What's going on?
  • I understand that I can fix it and get what I expect by uncommenting the commented code. However, I want to know how can we correct the .foo a:link, .foo a:visited selector such that it does not override the color defined in a:hover, a:active?
  • If I understand www.w3/TR/CSS21/cascade.html#specificity properly (Thanks, BoltClock), this is the specificity table for the various selectors in the code.

    a:link - 0 0 1 1 a:visited - 0 0 1 1 a:hover - 0 0 1 1 a:active - 0 0 1 1 .foo a:link - 0 0 2 1 .foo a:visited - 0 0 2 1

    So, the style defined for .foo a:link overrides the style for a:hover when both link as well as hover pseudo-classes apply to an A element of class foo.

    Similarly, the style defined for .foo a:visited overrides the style for a:hover when both visited as well as hover pseudo-classes apply to an A element of class foo.

    解决方案

    When you first started with CSS, you might have learned about the LoVe-HAte mnemonic for the order in which to specify link selectors (a:link, a:visited, a:hover, a:active). Have you ever wondered why this mnemonic was chosen?

    Well, there's a note in the spec on how the link and dynamic pseudo-classes are treated when multiple rules using all of them apply to the same element, which explains why you need to set link selectors in that order:

    Note that the A:hover must be placed after the A:link and A:visited rules, since otherwise the cascading rules will hide the 'color' property of the A:hover rule. Similarly, because A:active is placed after A:hover, the active color (lime) will apply when the user both activates and hovers over the A element.

    Anyway, the point I'm trying to make above is that all four pseudo-classes, being pseudo-classes, have equal specificity. Everything else about specificity applies. In this case, out of a bunch of equally specific selectors, the last rule is applied. When or how each pseudo-class is triggered is never relevant.

    Now, the simple introduction of the .foo selector causes your second set of link/visited rules to override your first set of link/visited styles and the hover/active styles, forcing links in elements with that class to always appear green until you add hover/active styles with the .foo selector.

    Sorry if my answer seems stitched-up or slipshod by the way, I'm typing this on my iPhone right now and it's pretty hard to think out here...

    更多推荐

    为什么.foo a:link,.foo a:visited {} selector在CSS中覆盖a:hover,a:active {} selector?

    本文发布于:2023-10-16 15:56:43,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:visited   link   foo   selector   active

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!