简体   繁体   English

CSS属性选择器优先于普通CSS类选择器

[英]CSS attribute selectors taking precedence over normal CSS class selectors

I originally noticed this problem when working with CSS in an SVG file, and thought it was rendering error, but after trying it in HTML, the same situation occurred. 我最初在SVG文件中使用CSS时注意到此问题,并认为这是呈现错误,但是在HTML中尝试后,发生了同样的情况。

Take the following code: 采取以下代码:

.example {color:green}
.example {color:blue}

In this case, as expected using normal class selectors, the value of color is initially green , though later in the file it is redefined as blue , thus the resulting color of elements in the class are blue. 在这种情况下,正如使用普通类选择器所期望的那样, color的值最初是green ,尽管后来在文件中将其重新定义为blue ,因此类中元素的最终颜色为蓝色。

Now take this example: 现在来看这个例子:

div[class='example'] {color:green}
.example {color:blue}

In this case, now initially defining the color value for divs in example using attribute selectors, when the value is redefined using normal CSS class selectors, the change from green to blue is ignored in the divs, and the value set by the attribute selector takes precedence, despite the blue color value for the whole class being redeclared later in the file. 在这种情况下,现在最初限定的颜色值的div在example使用属性选择器,当该值是使用正常CSS类选择重新定义的,从绿色到蓝色的变化中的div被忽略,由属性选择器设定的值取优先级,尽管稍后在文件中重新声明了整个类的蓝色值。

According to Mozilla documentation on CSS class selectors , it says normal selectors and attribute selectors are "equivalent", though that doesn't appear to be the case in this situation. 根据Mozilla关于CSS类选择器的文档,它说普通的选择器和属性选择器是“等效的”,尽管在这种情况下似乎并非如此。 What is the cause of this? 这是什么原因?

I'd originally posted this as a comment, but perhaps I should've made it answer. 我最初将其发布为评论,但也许我应该做出回答。


Let's look at the actual conditions of your two CSS rules: 让我们看一下两个CSS规则的实际情况:

div[class='example'] {color:green}
  • Element must be a <div> 元素必须为<div>
  • Element must have class "example" 元素必须具有“示例”类
.example {color:blue}
  • Element must have class "example" 元素必须具有“示例”类

Because your first CSS rule has two conditions, whereas your second rule only has one , the first rule is more specific - therefore it will take precedence. 因为您的第一个CSS规则有两个条件,而您的第二个规则只有一个条件 ,所以第一个规则更具体-因此它将优先。

If you were to remove the div portion from your first rule, it would be considered equivalent (as MDN states), at which point the text would be blue. 如果要从第一条规则中删除div部分,则该部分将被视为等效(如MDN所述),此时文本将变为蓝色。

Mozilla documentation is correct. Mozilla文档是正确的。 But when considering specificity, you need to take in to the account div and [class='example'] . 但是,考虑到特殊性,您需要使用div[class='example']帐户。 These two combined are stronger than .example . 这两个组合比.example更强大。

Here is an nice representation of specificity: https://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/ 这是一个很好的特异性表示: https : //www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/

If you go and open smasingmagazine.com articele, you will conclude that: 如果您打开smasingmagazine.com文章,您将得出以下结论:

.example has power of 10 .example为10

Whereas div[class='example'] has power of 11 div[class='example']幂为11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM