简体   繁体   English

CSS通配符选择器-没有覆盖?

[英]CSS Wildcard Selector - No override?

Can anyone tell me why the last span is dotted? 谁能告诉我为什么最后一个跨度是虚线? Why isn't it overwritten? 为什么不覆盖?

Forkable pen https://codepen.io/anon/pen/pqjREd 可叉笔https://codepen.io/anon/pen/pqjREd

 div div div * span { border: double; } div * * span { border: dotted; } div * { border: dashed; } div { border: none; } 
 <div> Line 1 <br> <span>Line 2</span> <br> <div> Line 3 <br> <span>Line 4</span> <br> <div> Line 5 <br> <span>Line 6</span> <br> </div> </div> </div> 

Let's break this down rule by rule. 让我们按规则细分这个规则。

Let's assume you've now heard of the term specificity, as others have alluded to in the comments. 假设您已经听说过术语特异性,正如其他人在评论中提到的那样。 But let's assume either that you don't know anything about it beyond that it has something to do with your problem, or that you've read the links given to you but you're having trouble applying that knowledge to this specific problem. 但是,让我们假设您除了对问​​题有任何了解之外,对它一无所知,或者您已经阅读了提供给您的链接,但是在将这些知识应用于特定问题时遇到了麻烦。

To start, specificity only takes into account selectors that match the element to begin with. 首先,特异性只考虑与元素匹配的选择器。 Since a div and a span are two different things, the last rule is irrelevant, so let's get rid of that: 由于divspan是两个不同的事物,因此最后一条规则是无关紧要的,因此让我们摆脱它:

div div div * span {
  border: double;
}

div * * span {
  border: dotted;
}

div * {
  border: dashed;
}

Now we have three rules, two of which ask for a span specifically, and the third, any element. 现在我们有了三个规则,其中两个规则专门要求一个span ,第三个规则是任何元素。

The first rule, div div div * span , doesn't match the "Line 6" element, because its div ancestors only go up to three levels. 第一个规则div div div * span不匹配“第6行”元素,因为它的div祖先最多可以升至三个级别。 There is no fourth level between the innermost div and the span , and the parent of the outermost div is not another div , but body . 最里面的divspan之间没有第四级,最外面的div的父级不是另一个div ,而是body So the only two rules that actually match this element are: 因此,实际上与该元素匹配的仅有两个规则是:

div * * span {
  border: dotted;
}

div * {
  border: dashed;
}

Now we can talk specificity. 现在我们可以谈谈特异性。 The universal selector * has zero specificity. 通用选择器*特异性为零。 All of your rules have only type selectors and/or universal selectors, so calculating their specificity is fairly straightforward: 您的所有规则都只有类型选择器和/或通用选择器,因此计算其特异性非常简单:

div * * span /* 2 types -> specificity = (0, 0, 2) */
div *        /* 1 type  -> specificity = (0, 0, 1) */

In conclusion, because the dotted border rule is more specific, the dashed border rule doesn't override it. 总之,由于虚线边框规则更具体,虚线边框规则不会覆盖它。

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

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