选择器的优先级主要用于样式发生冲突的情况下
选择器范围越小,优先级越高
行内样式>id选择器>类选择器>标签选择器>通用选择器
这里涉及一个权重值的问题,权重值越高,优先级越大
权重值
HTML分别为每种类型选择器分配了一个权值:
(1)通用选择器
权重值:0
(2)标签选择器
权重值:1
(3)类选择器
权重值:10
(4)ID选择器
权重值:100
(5)行内样式
权重值:1000
权重值的计算:
权值的计算很简单,直接看代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
a{color: yellow;}/*1*/
div a{color: green;}/*1+1*/
.demo a{color: black;}/*10+1*/
#demo a{color: orange;}/*100+1*/
div#demo a{color: red;}/*1+100+1*/
</style>
</head>
<body>
<a href="">this is 黄色</a>
<div class="demo">
<a href="">this is 黑色</a>
</div>
<div id="demo">
<a href="">this is 红色</a>
</div>
</body>
</html>
运行结果:
自己在编译器敲敲就懂了