Open zxdfe opened 1 year ago
15px
<style type="text/css">
#a {font-size:12px} //100 ==> 继承 ,权重为0
div p{ font-size:13px } // 2
div .c{ font-size:14px } //11
.a .b .c{ font-size:15px } //30
#b{ font-size:16px } // 100 ==> 继承,权重为0
</style>
<body>
<div id="a" class="a">
<div id="b" class="b">
<p id="c" class="c">I’am here</p> // 15px
</div>
</div>
</body>
1. 计算优先级的前提是选择器作用于相同的元素 2. 继承来的样式权重优先级最低,可视为0
div .c 会找不到目标吗?
15px, .c的权重最高
15px,算一下权重就好,第一个和最后一个都是继承,没有权重;
15px 记住继承权重为0 再依次算就行
15px
<style type="text/css">
#a {font-size:12px} //继承权重为0
div p{ font-size:13px } // 1, 1
div .c{ font-size:14px } //1, 10
.a .b .c{ font-size:15px } // 10,10,10 √√√
#b { font-size:16px } // 继承权重为0
</style>
<body>
<div id="a" class="a">
<div id="b" class="b">
<p id="c" class="c">I’am here</p> //15px
</div>
</div>
</body>
<style type="text/css">
#a {font-size:12px} 权重为100
div p{ font-size:13px } 权重为2
div .c{ font-size:14px } 权重为11
.a .b .c{ font-size:15px } 权重为30
#b { font-size:16px } 权重100
</style>
<body>
<div id="a" class="a">
<div id="b" class="b">
<p id="c" class="c">I’am here</p>
</div>
</div>
</body>
通过权重判断以及去掉继承的,I’am here 的字体大小为15px
15px
15px
.a .b .c的权重是300,其余都没有它大,所以字体大小为15px
15px
div p ----> 标签选择器,c 继承,权重低 div .c ----> 标签选择器+类选择器,权重较低 .a .b .c ----> 类选择器+类选择器+类选择器,权重最高
因此 class .c 的css样式字体大小为15px
15px
但是#a和#b都没有选到I’am here的p标签,继承权重为0 div p 2 div .c 11 .a .b .c 30 .a .b .c{ font-size:15px } ==> 15px
类型 | 权重 |
---|---|
!important | 10000 |
行内(内联)样式,style="" | 1000 |
ID选择器, #id | 100 |
Class、伪类、属性选择器 :hover [type="text"] | 10 |
标签选择器、伪元素选择器,如p、div、::before | 1 |
通用选择器*; 子选择器 > ; 相邻选择器 + ; 同胞选择器 ~ ;继承选择器 | 0 |