简体   繁体   English

使用两个相同的属性选择器 [class][class]

[英]Using two identical attribute selectors [class][class]

Yesterday I stumbled across a rule in one of our company's css files that looks like this:昨天我在我们公司的一个 css 文件中偶然发现了一条规则,如下所示:

.classname1[class][class] { 
   margin:0 !important; 
}

My interpretation of this rule is that maybe someone wanted to make sure that this rule will apply, even if another class with the ".important" property was added.我对这条规则的解释是,即使添加了另一个具有“.important”属性的 class,也许有人想确保这条规则适用。 If this was the case I believe it would be enough with one [class] attibute: Like this:如果是这种情况,我相信一个 [class] 属性就足够了:像这样:

.classname1[class] {
   margin:0 !important;
}
.classname2 {
   margin:5px !important;
}
<div class="classname1 classname2">
   ...
</div>

My question is, why the second attribute property [class] in the rule?我的问题是,为什么规则中的第二个属性属性 [class]?

[class][class] matches any element that has a class attribute (or, to be more precise, at least one class attribute — see below for why). [class][class]匹配任何具有class属性的元素(或者,更准确地说,至少有一个class属性 - 请参阅下文了解原因)。 The only functional difference between [class][class] and [class] is specificity, as each attribute selector contributes its own specificity amount : [class][class][class]之间唯一的功能区别是特异性,因为每个属性选择器都有自己的特异性量

Note: Repeated occurrences of the same simple selector are allowed and do increase specificity.注意:允许重复出现相同的简单选择器并且确实增加了特异性。

For reference, here are the specificities of all three example selectors:作为参考,以下是所有三个示例选择器的特性:

/* 1 class, 2 attributes -> specificity = (0,3,0) */
.classname1[class][class]

/* 1 class, 1 attribute  -> specificity = (0,2,0) */
.classname1[class]

/* 1 class               -> specificity = (0,1,0) */
.classname2

An !important declaration under a less specific selector will still override a non-important declaration under this selector.一个不太具体的选择器下的!important声明仍将覆盖此选择器下的非重要声明。 An !important declaration under this selector will override any !important declarations under less specific selectors (or equally specific selectors appearing earlier in the source order).此选择器下的!important声明将覆盖不太具体的选择器下的任何!important声明(或在源顺序中较早出现的同样具体的选择器)。

If the only selector that needed to be overridden was .classname2 , then adding two attribute selectors on top of a class selector would indeed be overkill.如果唯一需要覆盖的选择器是.classname2 ,那么在 class 选择器之上添加两个属性选择器确实是矫枉过正。 But for all we know, the author might have intended to override a selector like your intermediate example.但据我们所知,作者可能打算覆盖像您的中间示例这样的选择器。 Or it might have indeed been a mistake.或者这可能确实是一个错误。 Only they would know for sure, but these are my educated guesses.只有他们肯定知道,但这些是我有根据的猜测。


The reason [class][class] matches is because it does not require the element to have two class attributes — within a compound selector, all simple selectors are treated independently of each other, and this includes attribute selectors regardless of their names and/or values. [class][class]匹配的原因是因为它不需要元素具有两个class属性——在复合选择器中,所有简单选择器都相互独立处理,这包括属性选择器,无论其名称和/或价值观。 Selectors does not specify or presume whether an element can have multiple class attributes — only that an attribute presence selector matches based on the presence of at least one.选择器不指定或假定一个元素是否可以具有多个class属性 - 只是属性存在选择器基于至少一个的存在进行匹配。

Having said that, the spec does contain an informative note with respect to class selectors (ie not attribute selectors for the class attribute) suggesting that an element may theoretically have multiple class attributes, although this isn't possible in contemporary HTML. Having said that, the spec does contain an informative note with respect to class selectors (ie not attribute selectors for the class attribute) suggesting that an element may theoretically have multiple class attributes, although this isn't possible in contemporary HTML. It also states, normatively , that an element can have multiple ID attributes and all of them must be used when matching ID selectors.它还规范地指出,一个元素可以具有多个 ID 属性,并且在匹配 ID 选择器时必须使用所有这些属性。 No such explicit text exists for value-matching with respect to attribute selectors, as multiple IDs are only possible in HTML with different attributes, but it's probably logical to deduce that the same would apply.对于属性选择器的值匹配,不存在这样的显式文本,因为多个 ID 仅在具有不同属性的 HTML 中才有可能,但推断同样适用可能是合乎逻辑的。

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

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