前言
优先级是基于不同种类选择器组成的匹配规则。浏览器通过优先级来判断哪些样式与一个元素最为相关,从而在该元素上应用这些样式。
CSS选择器的分类

不同种类选择器的用法
基本选择器以外的选择器的用法。
属性选择器
通过已经存在的属性名或属性值匹配元素。
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
| <style> div { margin-top: 20px;} [title]{ background: #faf3e0; } [class=div1]{ background: #eabf9f; } [attr~=attr-test2]{ background: #b68973; } [attr^=te]{ background: #f39189; } [attr$=Test]{ background: #bb8082; } [attr*=test3]{ background: #6e7582; } [attr|=attr1]{ background: #046582; } [attr*=test5 i]{ background: #865858; } </style> <body> <div title='helloWorld'>[title]</div> <div class="div1">[class=div1]</div> <div attr='attr-test1 attr-test2'>[attr~=attr-test2]</div> <div attr='test'>[attr^=te]</div> <div attr='attrTest'>[attr$=Test]</div> <div attr='attr-test3'>[attr*=test3]</div> <div attr='attr1-test4'>[attr|=attr1]</div> <div attr='attr-Test5'>attr*=test5 i</div> </body>
|

伪类选择器

动态伪类(多用于超链接的样式)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <style> a:link { color: #11698e; } a:visited {color: #9fb8ad; } a:hover{ color: #383e56; } a:active{ color: #fb743e; } </style> <body> <a target="_blank" href='https://juejin.cn/user/3456520257288974'>超链接</a> </body>
|
伪类的active在IOS下存在兼容问题。
目标伪类、否定伪类、语言伪类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <style> html { font-size: 14px; } div:target { color: #f05454; } p:not(#p1){ color: #e27802; } div:lang(zh) { color: #ffc1b6; } </style> <body> <h3>目标伪类 :target</h3> <div id="div1">目标伪类: 这是 div1</div> <div id="div2">目标伪类: 这是 div2</div> <h3>否定伪类 :not</h3> <p id="p1">否定伪类: 这是 p1</p> <p id="p2">否定伪类: 这是 p2</p> <h3>语言伪类 :lang</h3> <div lang="en">语言伪类: 这是 en</div> <div lang="zh">语言伪类: 这是 zh</div> </body>
|

结构伪类
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
| <style> p:first-child { background: #046582; } span:first-of-type { background: #6e7582; } p:nth-child(2n) { background: #bb8082; } span:nth-of-type(2n) { background: #f39189; } i:only-child { background: #865858; } strong:only-of-type { background: #8e7f7f; } p:empty { height: 16px; background: #bbb; } :root { background: #e2d5d5; color: #fff; } </style> <body> <div> <p class="p1">这是 p1</p> <p class="p2">这是 p2</p> <p class="p3"><i>这是 p3</i></p> <p class="p4">这是 p4</p> <span class="span1">这是 span1</span> <span class="span2">这是 span2</span> <p class="empty-p p5">这是 p5</p> <strong>这是 strong</strong> </div> </body>
|

UI元素伪类
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
| <style> input[type="radio"]:enabled { box-shadow: 0 0 0 3px #7c9473; } input[type="radio"]:disabled { box-shadow: 0 0 0 3px #cfdac8; cursor: not-allowed; } input[type="radio"]:checked { box-shadow: 0 0 0 3px #c0e218 ; } input[type="radio"]:default { box-shadow: 0 0 0 3px #86aba1;} input:read-write { background: #7c9473; } input:read-only { background: #cfdac8; } </style> <body> <div> <input type="radio" name="my-radio" id="radio1" checked> <label for="radio1">默认选中</label> <input type="radio" name="my-radio" id="radio2"> <label for="radio2">未选中-可用</label> <input type="radio" name="my-radio" id="radio1" disabled> <label for="radio1">未选中-禁用</label> </div> <div> <input type="input" name="my-input" id="input1" value="input1"> <input type="input" name="my-input" id="input2" value="input2" readonly> </div> </body>
|

伪元素选择器
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
| <style> div { margin-left: 50px;} .div1::after { content: 'div1 的 after'; margin-left: 10px; color: #ef4f4f } .div2::before { content: 'div2 的 before'; margin-right: 10px; color: #ee9595 } .div3::first-letter { color: #ff4646 } .div4::first-line { color: #9dab86 } .div5 ul li::marker { color: #fdb827 } .div6::selection { background: #9dab86; color: white } </style> <body> <div class="div1">div1</div> <div class="div2">div2</div> <div class="div3">div3</div> <div class="div4">div4第一行<br>div4第二行</div> <div class="div5">div5 <ul> <li>item1</li> <li>item2</li> <li>item3</li> </ul> </div> <div class="div6">div6</div> </body>
|

一个选择器中只能使用一个伪元素
CSS3 中伪元素应该用双冒号,以便区分伪元素和伪类。但是旧版的规范未做明确区分,所以大多数浏览器中支持部分伪元素使用单双冒号两种写法。
组合选择器
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
| <style> div span { margin-left: 10px; background: #ff8585 } .div1>span { color: #6155a6 } .div1, .div2 { color: #a7c5eb } .div3 + div { color: #fd3a69 } .div5 ~ div { color: #008891 ; } </style> <body> <div class="div1">div1 <span class="span1">span1 <span class="span1-1">span1-1</span> </span> </div> <div class="div2">div2<span class="span2">span2</span></div> <div class="div3">div3<span class="span3">span3</span></div> <div class="div4">div4<span class="span4">span4</span></div> <div class="div5">div5<span class="span5">span5</span></div> <div class="div6">div6<span class="span6">span6</span></div> <div class="div7">div7<span class="span7">span7</span></div> <div class="div8">div8<span class="span8">span8</span></div> </body>
|

CSS选择器优先级
基本选择器的优先级是:!important > 内联 > ID 选择器 > 类选择器 > 标签选择器 > 通用选择器。那么它如何计算的呢?有这样一个计算表格。
选择器 |
示例 |
权重值 |
!impotant |
color: #fff !important; |
正无穷 |
内联选择器 |
样式作用元素 |
1 0 0 0 |
ID 选择器 |
#id |
1 0 0 |
类选择器、属性选择器、伪类选择器 |
.class |
1 0 |
标签选择器、伪元素选择器 |
div |
1 |
通用选择器 |
* |
0 |
继承属性 |
样式作用元素 |
-1 |
假设在一个拍卖会上,有以下几种筹码:
- 一个
内联样式
相当于一千元
- 一个
ID 选择器
相当于一百元
- 一个
类选择器
相当于十元
- 一个
标签选择器
相当于一元
- 通用选择器为零元
每出现一个上述选择器,就增加对应筹码的钱数,最终采用出钱最多的样式。但是,这里的钱数计算方法和生活中的计算方法不一样,这里的”单位”不会因为值的累加而进行换算。例如十个一百只能是“十百“,仍然小于一千。
基本选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <style> * { color: #31326f } div { background: #9ddfd3 } .div-class { background: #dbf6e9 } .p1, .p2, .p3 { background: #ffdada; color: #060930 } #p1-1 { background: #595b83 } .p3 { color: #595b83 !important } </style> <body> <div class="div-class"> <h1>Hello word!</h1> <p class="p1" id="p1-1">Hello word!</p> <p class="p2" style="color: white">Hello word!</p> <p class="p3" style="color: white">Hello word!</p> </div> </body>
|

其他选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <style> div { color: #a20a0a } div .p1 ~ p { background: #fceef5; color: #ffa36c } div p ~ p { background: pink; } .div-class .p1 ~ p { color: #799351 } .div-class p::after { content:'p-after'; margin-left: 10px; color: #333456 } div .p1::after { color: #42bfd8 } .p1 { color: #92817a } [name="p1"] { color: #bedbbb } div .span1 { color: #0e918c !important; } .span1 { color: #6a097d !important; } </style> <body> <div class="div-class"> <h1>Hello word!</h1> <p class="p1" name="p1">p1</p> <p class="p2">p2</p> <p class="p3">p3</p> <span class="span1">span1</span> </div> </body>
|

权重值相同时,写在后面的样式生效
!important 是 CSS 选择器中的一个 “流氓” 属性,不论选择器的权重或者优先级的高低,只要加上!important,那么这个样式的优先级就是最高的
如果针对同一元素样式存在冲突且同时存在!important ,那么选择器总权重值高者生效
1 2 3 4 5 6 7 8 9 10
| <style> div { max-width: 100px; } div, p { background: #bedbbb } .div1 { width: 200px !important; } .p1 { width: 200px; } </style> <body> <div class="div1"> 这是 div1</div> <p class="p1"> 这是 p1</p> </body>
|

对于一些互斥的样式,例如 max-width 与 width,选择器的权重值再高也是无能为力的。
总结
如你所见,CSS 选择器也是暗藏玄机的哦。不过到目前为止,暂时没有能够通过元素选择其父元素或其父元素相关元素的选择器,这就让人很是头疼。