CSS 特异性如何决定应用哪些样式?

发布于 2025-01-02 11:10:13 字数 997 浏览 0 评论 0原文

CSS 如何确定何时应用一种样式而不是另一种样式?

我已经浏览过几次 W3 CSS3 选择器文档,这帮助我理解了如何在 jQuery 中更好地使用 CSS 选择器,但它并没有真正帮助我理解何时将一个 CSS 规则应用于另一个 CSS 规则。

我有以下 HTML:

<div class='item'>
    <a>Link 1</a>
    <a class='special'>Link 2</a>
</div>

我有以下 CSS:

.item a {
    text-decoration: none;
    color: black;
    font-weight: bold;
    font-size: 1em;
}

.special {
    text-decoration: underline;
    color: red;
    font-weight: normal;
    font-size: 2em;
}

鉴于上述情况,链接 1 和链接 2 的样式将相同,由 .item a CSS 确定。为什么与 .special 关联的样式不优先用于链接 2?

显然,我可以这样解决它:

.special {
    text-decoration: underline !important;
    color: red !important;
    font-weight: normal !important;
    font-size: 1em !important;
}

但是,我觉得这是一个黑客,由于我缺乏理解,我必须加入其中。

How does CSS determine when to apply one style over another?

I have been through the W3 CSS3 selectors document a few times, and that has helped me understand how to better use CSS selectors in jQuery, but it has not really helped me understand when one CSS rule will be applied over another.

I have the following the HTML:

<div class='item'>
    <a>Link 1</a>
    <a class='special'>Link 2</a>
</div>

I have the following CSS:

.item a {
    text-decoration: none;
    color: black;
    font-weight: bold;
    font-size: 1em;
}

.special {
    text-decoration: underline;
    color: red;
    font-weight: normal;
    font-size: 2em;
}

Given the above, both Link 1 and Link 2 will be styled the same, as determined by the .item a CSS. Why does the style associated with .special not take precedence for Link 2?

Obviously, I can get around it like this:

.special {
    text-decoration: underline !important;
    color: red !important;
    font-weight: normal !important;
    font-size: 1em !important;
}

But, I feel like that is a hack that I have to sprinkle in due to my lack of understanding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦浮鱼 2025-01-09 11:10:13

它基于 IDclassestagsID 具有最高的特异性,然后是 classes,然后是 tags,因此:

.item a     == 0 1 1      0 (id) 1 (class=item) 1 (tag=a)
.special    == 0 1 0
#foo        == 1 0 0
#foo .bar a == 1 1 1
#foo #bar   == 2 0 0

以最多者获胜:) 如果平局,则以最后一个为准在文档中获胜。请注意,1 0 0 优于 0 1000 1000

如果您希望应用 .special,请使其更具体:.item a。特别

It's based on IDs, classes and tags. IDs have the highest specificity, then classes then tags, so:

.item a     == 0 1 1      0 (id) 1 (class=item) 1 (tag=a)
.special    == 0 1 0
#foo        == 1 0 0
#foo .bar a == 1 1 1
#foo #bar   == 2 0 0

whichever has the most wins :) If it's a tie, whichever one comes last in the document wins. Note that 1 0 0 beats 0 1000 1000

If you want .special to apply, make it more specific: .item a.special

盛夏尉蓝 2025-01-09 11:10:13

我建议您熟悉以供将来参考。对于这种特殊情况,请注意级联顺序下的第 3 点:

  1. 计算选择器中 ID 属性的数量。
  2. 计算选择器中 CLASS 属性的数量。
  3. 计算选择器中 HTML 标记名称的数量。

如果将这些应用到您的代码中,则 .item a 有 1 个类属性 + 1 个 HTML 标记名称,而 .special 只有 1 个类属性。因此,前者赢得了设计特殊链接的权利。

I would suggest you get familiar with this for future reference. For this particular case, note point 3 under Cascading Order:

  1. Count the number of ID attributes in the selector.
  2. Count the number of CLASS attributes in the selector.
  3. Count the number of HTML tag names in the selector.

If these are applied to your code, .item a has 1 class attribute + 1 HTML tag name, while .special has only one class attribute. Hence, the former wins the right to style the special link.

酒与心事 2025-01-09 11:10:13

http://www.w3.org/TR/CSS21/cascade.html#specificity 是官方的特异性规范。

但如果这就是 TL;DR,那么(太)短的版本就是选择器中的单词越多,特异性就越高。并且与 !important 甚至更高。就是这样。

编辑:哦,我看到你的链接具有与我的相同的信息。对此感到抱歉。

http://www.w3.org/TR/CSS21/cascade.html#specificity is the official specificity specification.

But if that's TL;DR, the (too) short version is the more words you have in your selector, the higher the specificity. And with !important even higher. That's about it.

Edit: oh, I see that your link has the same information as mine. Sorry about that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文