网上看到很多关于选择器优先级问题。
主要存疑在 伪类选择器 类选择器 属性选择器
于是做了如下实验;
得出结果
!important > 行内样式 > ID选择器 > 伪类选择器 > 类选择器 > 标签 > 属性选择器 > 通配符 > 继承 > 浏览器默认属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>伪类选择器</title>
<style type="text/css">
h4 {
color: blue !important;
}
* {
margin: 0;
padding: 0;
color: orange;
}
#text {
color: red;
}
[title] {
color: brown;
}
div {
color: blue;
}
p:nth-child(4),
p:nth-child(5) {
color: aqua;
}
.text {
color: green;
}
</style>
</head>
<body>
<h1> !important > 行内样式 > ID选择器 > 伪类选择器 > 类选择器 > 标签 > 属性选择器 > 通配符 > 继承 > 浏览器默认属性</h1>
<h4 style="color:gray">
important(blue) > 行内
</h4>
<p id="text" style="color:gray">
行内(gray) > id
</p>
<p id="text">
id(red) > 伪类选择器aqua </p>
<p class="text">
伪类选择器aqua> class
</p>
<p class="text" title="1">
类选择器green > 属性选择器brown
</p>
<div title="1">
属性选择器brown > 标签
</div>
</body>
</html>