blue应该是蓝色的,不向元素添加ids,不使用JavaScript,也不知道父#id的,因为它们可以在将来的某个日期更改,同时允许JavaScript设置正确应用的样式属性。
custom-tag {
color: blue;
/*
* the above should have the specificity of exactly 2×ids + 1×tag
*/
}
/* later loaded CSS, which is not controllable... */
#one custom-tag, #two custom-tag {
color: red;
}<div id="one">
<custom-tag>blue</custom-tag>
<custom-tag style="color:orange">orange</custom-tag>
</div>
<div id="two">
<custom-tag style="color:green">green</custom-tag>
<custom-tag>blue</custom-tag>
</div>
发布于 2018-03-21 15:21:29
在不需要父母的情况下增加选择器的特异性,使用:not() +没有所需特异性的选择器:
any-tag:not(#bogus_id):not(#bogus_id) {
color: blue;
}
#one any-tag, #two any-tag {
color: red;
}<div id="one">
<any-tag>blue</any-tag>
<any-tag style="color:orange">orange</any-tag>
</div>
<div id="two">
<any-tag style="color:green">green</any-tag>
<any-tag>blue</any-tag>
</div>
发布于 2018-03-21 15:34:32
您还可以像下面这样对选择器进行叠加:
.selector.selector这将增加其特异性。
您还可以将它们叠加两次以上。
在所有浏览器中都能工作。
https://stackoverflow.com/questions/49410231
复制相似问题