Html 了解CSS选择器优先级/特异性
我想了解CSS选择器如何处理属性冲突。如何选择一个属性而不是另一个属性Html 了解CSS选择器优先级/特异性,html,css,css-selectors,Html,Css,Css Selectors,我想了解CSS选择器如何处理属性冲突。如何选择一个属性而不是另一个属性 <!-- start class vs id --> <p class="class1" id="id1">.class vs #id: The winner is #id</p> <!-- upper vs bottom --> <p id="id2">the very bottom is the winner</p> <
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
div{
背景色:红色;
}
我的班级{
背景色:黑色;
}
div#我的id{
背景颜色:蓝色;
}
身体科{
背景颜色:绿色;
}
正文>div{
背景颜色:橙色;
}
body>div#我的id{
背景颜色:粉红色;
}
您好
您感兴趣的是
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
Firebug是一个很好的工具来帮助检查这一点。但其他浏览器也有内置的工具,用于检查应用的CSS规则。我将插入一个指向CSS 2.1规范本身的链接,以及浏览器应该如何计算特定性:
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
:
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
选择器的特异性计算如下:
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
- 如果声明来自,则计数1是一个“样式”属性,而不是一个带选择器的规则,否则为0(=a)(在HTML中,元素“样式”属性的值是样式表规则。这些规则没有选择器,因此a=1、b=0、c=0和d=0。)
- 计算选择器中ID属性的数量(=b)
- 计算选择器中其他属性和伪类的数量(=c)
- 计算选择器中元素名称和伪元素的数量(=d)
- 特异性仅基于选择器的形式。特别是,形式为“[id=p33]”的选择器被计为属性选择器(a=0,b=0,c=1,d=0),即使id属性在源文档的DTD中定义为“id”
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
如果特殊性相同,则发挥作用:
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
.a
和.b
具有相同的特异性,则以样式表中最后定义的为准<根据.a
和.b
的定义顺序,code>的样式将相同
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
将显示详细信息以进行演示。您可以在此处参考完整答案
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
从最具体的开始:
id选择器>类选择器>类型选择器(普通h1、p标记等)
!重要的总是赢,但这被认为是一个坏的做法。请参阅上面的链接
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
最好的方法是进行试验:
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
一个测试用例。Firebug是否有特定的(没有双关语)工具来分析特异性?比通过罢工查看被推翻的规则更重要吗?@Pekka,不,没有。尽管如此,这仍然是一个很好的工具。@Pekka在HTML选项卡的右侧,Calculated(?)选项卡(介于样式和外观之间)有另一个贯穿覆盖指令视图的工具。当你只对一个属性感兴趣时,它可能比你所指的样式更有用。目前为止最好的解释是:当你向下滚动到“计算CSS特定值”部分时,它开始变得非常有趣。但是当两个特定值相同时会发生什么?它是根据哪个规则首先定义的吗?具体来说,规则4涵盖了这一点,它适用于特定性之后。我将对答案进行编辑,以包含打破僵局的规则。@Boom:如果有多个相同的特殊性规则,则应用样式表中出现的最后一个规则。希望这有帮助。
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>