权重是什么?
首先权重一个相对概念,可以认作天平中的砝码,权重大的地方就会被重视(使用),当然这在权重等级相同的前提下。
一:权重的等级划分:
- 第一等级:内部样式 (style=" "),权值:1000.
- 第二等级:ID选择器(#id),权值:100.
- 第三等级:class、伪类、属性选择器(.class , :hover , :link , [type]),权值为:10.
*伪类有::link,:visited,:hover,:active,:focus,:frist-child。* - 第四等级:标签、伪元素选择器(id,name :after :before),权值为:1.
二:优先级规则:
同等等级情况下:
- 权值高的优先。
- 权值相同时后定义的优先。
- 属性后面加 !important 时,最优先。
等级不同的情况下,等级高的优先!!
三:权值的计算:
权值 = 1000第一等级个数 + 100第二等级个数 + 10第三等级数 + 第四等级数;***
例:
<style>
#div div{
<!-- 权值 101 -->
width: 100px;
height: 100px;
background-color:red;
}
div .a .b .c .d .e .f .g .h .i .j div{
<!-- 权值 102 -->
width:100px;
height