选择器优先级

今天被问到优先级问题,这个问题只是有稍微了解一下,但是答不清楚。

不同级别

1)id选择器与类选择器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./test.css">
<title>Document</title>
</head>
<style>
</style>
<style>
div{
width: 200px;
height: 200px;
}
.priority{
background: red;
}
#priority{
background: yellow;
}
</style>
<body>
<div class="priority" id="priority"></div>
</body>

</html>

如图:

WX20190305-122053@2x

id选择器 》类选择器

2)标签选择器与id选择器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./test.css">
<title>Document</title>
</head>
<style>
</style>
<style>
div{
width: 200px;
height: 200px;
}
div{
background: red;
}
#priority{
background: yellow;
}
</style>
<body>
<div class="priority" id="priority"></div>
</body>

</html>

如图:

WX20190305-122053@2x

id选择器 》标签选择器

3)类选择器与标签选择器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./test.css">
<title>Document</title>
</head>
<style>
</style>
<style>
div{
width: 200px;
height: 200px;
}
.priority{
background: yellow;
}
div{
background: red;
}
</style>
<body>
<div class="priority" id="priority"></div>
</body>

</html>

如图:

WX20190305-122053@2x

类选择器 》标签选择器

4)行内样式 与 id选择器 与 类选择器 与 标签选择器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./test.css">
<title>Document</title>
</head>
<style>
</style>
<style>
div {
width: 200px;
height: 200px;
}

div {
background: red;
}

.priority {
background: yellow;
}

#priority {
background: green;
}
</style>

<body>
<div class="priority" id="priority" style="background: blue"></div>
</body>

</html>

如图:

WX20190305-125605@2x

结论 : 行内样式》id选择器 》类选择器 》标签选择器

5)行内样式与!important

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./test.css">
<title>Document</title>
</head>
<style>
</style>
<style>
div {
width: 200px;
height: 200px;
}

div {
background: red!important;
}

.priority {
background: yellow!important;
}

#priority {
background: green!important;
}
</style>

<body>
<div class="priority" id="priority" style="background: blue"></div>
</body>

</html>

如图:

WX20190305-125825@2x

结论: !important 》 行内样式

6)通配符选择器与标签选择器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./test.css">
<title>Document</title>
</head>
<style>
</style>
<style>
div {
width: 200px;
height: 200px;
}

div {
background: red!important;
}
*{
background: yellow!important;
}
</style>

<body>
<div class="priority" id="priority" ></div>
</body>

</html>

如图:

WX20190305-130042@2x

结论:标签选择器 》通配符选择器

总结

!important 》行内样式 》id选择器 》类选择器 》标签选择器 》通配符选择器

####

相同级别

1)后面写的覆盖之前的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./test.css">
<title>Document</title>
</head>
<style>
</style>
<style>
div {
width: 200px;
height: 200px;
}

.priority {
background: red;
}
.priority {
background: yellow;
}
</style>

<body>
<div class="priority" id="priority" ></div>
</body>

</html>

如图:

WX20190305-122053@2x

2)复合选择与类选择器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./test.css">
<title>Document</title>
</head>
<style>
</style>
<style>
div {
width: 200px;
height: 200px;
}

div.priority {
background: red;
}
.priority {
background: yellow;
}
</style>

<body>
<div class="priority" id="priority" ></div>
</body>

</html>

如图:

WX20190305-131207@2x

3)复合选择与id选择器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./test.css">
<title>Document</title>
</head>
<style>
</style>
<style>
div {
width: 200px;
height: 200px;
}

div.priority {
background: red;
}
#priority {
background: yellow;
}
</style>

<body>
<div class="priority" id="priority" ></div>
</body>

</html>

如图:

WX20190305-122053@2x

总结

id选择器》复合选择 》类选择器