1、不同选择器权重
选择器类型 | 权重值 |
---|
元素选择器 | 1 |
类选择器 | 10 |
id选择器 | 100 |
!important | 1000 |
2、相同选择器权重,后面的会覆盖掉前面的(.title3 覆盖.title2
)
3、层级选择器:按权重累加计算(.box #box_title > #box_title
)
4、源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>选择器权重</title>
<style>
h1 {
color: black;
}
.title1 {
color: red;
}
#title1 {
color: yellow;
}
.title2 {
color: antiquewhite;
}
.title3 {
color: black;
}
.box #box_title {
color: wheat;
}
#box_title {
color: red !important;
}
</style>
</head>
<body>
<h1 class="title1" id="title1">Hello world!</h1>
<h1 class="title2 title3">Kasmne</h1>
<div class="box">
<h1 class="title4 title5" id="box_title">jasmine</h1>
</div>
</body>
</html>