CSS选择器权重

来源:互联网 发布:mac 蓝光播放器 编辑:程序博客网 时间:2024/05/18 09:19

样式优先级

内联样式>内部样式>外部样式

选择器优先级

内联样式:1000

ID:100

类,属性,伪类:10

标签,伪元素:1


例子:

class为dd的li标签的背景色?

<span style="color:#666666;"><style>ul li{background: green;}li{width:100px;background: red;}.dd{background: yellow;}li.dd{background: black;}li:last-child{    background: purple;}</style><ul> <li>哈哈哈</li><li class="dd"></li></ul></span>

最后结果为purple;

它们分别权重为:

ul li:2,

li:1,

.dd:10,

li.dd:11,

li:last-child:11

其中li.dd和li:last-child权重一样都为11,如果li.dd在li:last-child后面,则会覆盖其颜色。


总结:

1.css选择器权值越大越优先显示

2.同样的权值后面会覆盖之前的定义

3.标有!important优先级最高,但在低版本IE浏览器下有问题

0 0