選擇器是CSS的核心組件。本文依據W3C的Selectors Level 4規範,歸納總結了Level1-Level4中絕大多數的選擇器,並作了簡單的語法說明及示例演示。但願對程序員有所助益。css
類型選擇器也能夠稱爲標籤名選擇器,會選中文檔中該類型元素的實例。html
h1 { color: red; }
將文檔中元素類型爲h1的顏色設置成紅色。程序員
查看示例程序ide
通用選擇器是一種特殊類型選擇器,表示任何元素類型的元素。函數
* { color: red; }
設置文檔中全部元素的顏色爲紅色。學習
h1[name] { color: red; }
選中元素 h1
中具備屬性 name
的元素。ui
查看示例程序翻譯
h1[title='cool'] { color: red; }
匹配元素 h1
中包含屬性 title
而且屬性值是 cool
的元素。指針
查看示例程序code
h1[title~='cool'] { color: red; }
匹配元素 h1
中包含屬性 title
而且屬性值中包含 cool
的元素。
h1[title|='cool'] { color: red; }
匹配元素 h1
中包含屬性 title
,而且屬性值是 cool
的元素 或以 cool-
開頭的元素。
h1[title^='cool'] { color: red; }
匹配元素 h1
中包含屬性 title
,而且屬性值串以 cool
開頭的元素。
h1[title$='cool'] { color: red; }
匹配元素 h1
中包含屬性 title
,而且屬性值串以 cool
結尾的元素。
h1[title*='cool'] { color: red; }
匹配元素 h1
中包含屬性 title
,而且屬性值串包含 cool
子串的元素。
.red { color: red; }
匹配文檔中 class
屬性爲 red
的元素並設置其顏色爲紅色。
#title { color: red; }
匹配文檔中 id
屬性爲 title
的元素並設置其顏色爲紅色。
dir()
主要用於匹配符合某個方向性的元素,例如 :dir(ltr)
和 :dir(rtl)
,其中ltr是left to right簡寫,表示從左向右,而rtl是right to left,表示從右向左。
:dir(ltr) { color: red; }
匹配文字方向爲從左到右的元素。
:lang(fr) { color: red; }
匹配語言爲法語的元素並設置其顏色爲紅色。
匹配帶有 href
屬性的 <a>
、<link>
、<area>
等元素。
:any-link { color: red; }
匹配任意超連接,並設置其顏色爲紅色。
:link { color: red; }
匹配未被訪問的超連接,並設置其顏色爲紅色。
:visited { color: blue; }
匹配訪問過的超連接,並設置其顏色爲藍色。
:local-link { color: red; }
匹配本地超連接,並設置其顏色爲紅色。
<h1><a href="#content">三十課</a></h1> <p id="content">程序員的筆記本</p> <p id="content2">程序員的筆記本2</p>
:target { color: red; }
點擊 html
代碼中的內部連接,連接的目標對象 id
爲 content
的 p元素顏色變爲紅色。
用戶指針設備懸停於指定元素時,匹配該元素。
:hover { color: red; }
用戶指針懸停於某元素,匹配該元素並將其設置其顏色爲紅色。
:active { background: red; }
當特定元素處於激活狀態時,設置元素背景色爲紅色。
:focus { background: red; }
當特定元素得到焦點,設置元素背景色爲紅色。
div:focus-within { border : 1px solid blue; }
當div中的子元素得到輸入焦點時,設置div元素邊框爲1象素值的藍色邊框。
在文檔的語音渲染或是顯示字幕期間,常會用到時間軸僞類。
:current(p) { color: red; }
以上規則定義了當前語音渲染段落的顏色爲紅色。
:past(p) { color: red; }
以上規則定義了以完成語音渲染段落的顏色爲藍色。
:future(p) { color: yellow; }
以上規則定義了以未進行語音渲染段落的顏色爲×××。
:playing { border : 1px solid red; }
匹配當前播放狀態的元素,併爲其添加1象素的紅色邊框。
:playing { border : 1px solid grey; }
匹配當前播放狀態的元素,併爲其添加1象素的灰色邊框。
:enabled { color: red; }
匹配啓用狀態的元素並設置其顏色爲紅色。
:disabled{ color: yellow; }
匹配禁用狀態的元素並設置其顏色爲×××。
:read-only { color: red; }
匹配只讀狀態的元素並設置其顏色爲紅色。
:read-write { color: yellow; }
匹配讀寫狀態的元素並設置其顏色爲×××。
:placeholder-shown { background: red; }
選中佔位符爲顯示狀態的元素並設置其背景色爲紅色。
:default { color: red; }
匹配缺省的元素並設置其顏色爲紅色。
:checked { height: 4em; }
匹配選中的元素並將其高度設成 4em
。
不肯定值僞類 :indeterminate
適用於其值處在不肯定狀態的元素。例如 : radio
元素組在未被初始選擇的狀況下就爲不肯定值狀態。
:indeterminate { height: 4em; }
匹配不肯定值元素並設置高度爲 4em
。
空值僞類 :blank
用來匹配輸入值爲空的輸入框,如 textarea
或 input
框。
:blank { background: red; }
目前該僞類兼容性較差。
:valid { color: red; }
匹配輸入值合法的元素並設置其顏色爲紅色。
:valid { color: blue; }
匹配輸入值不合法的元素並設置其顏色爲藍色。
:in-range { color: red; }
匹配輸入值在定義範圍內的元素並設置其顏色爲紅色。
:out-of-range { color: blue; }
匹配輸入值不在定義範圍內的元素並設置其顏色爲藍色。
:required { color: red; }
匹配定義爲必填項的元素並設置其顏色爲紅色。
:optional { color: blue; }
匹配定義爲選填項的元素並設置其顏色爲藍色。
僞類 :root
表示文檔的根元素。例如,在DOM文檔中,僞類 :root
與Document對象的根元素匹配。 在HTML中,就表示html元素。
:root { color: red; }
匹配文檔根元素並定義其顏色爲紅色。
:empty { color : red; }
匹配爲空的元素並設置其顏色爲紅色。
p:nth-child(3n+1) { color: red; }
匹配第1,4,7,10等 p
元素並設置其顏色爲紅色。
p:nth-child(3n+1) { color: red; }
匹配倒數第1,4,7,10等 p
元素並設置其顏色爲紅色。
p:first-child { color: red; }
匹配第一個 p
元素並設置其顏色爲紅色。
p:last-child { color: red; }
匹配最後一個 p
元素並設置其顏色爲紅色。
p:only-child { color: red; }
匹配父元素只包含惟一子元素的元素 p
並設置其顏色爲紅色。
p:nth-of-type(3n+1) { color: red; }
匹配類型爲 p
的第1,4,7,10等索引位置的元素並設置其顏色爲紅色。
p:nth-last-of-type(3n+1) { color: red; }
匹配類型爲 p
的倒數第1,4,7,10等索引位置的元素並設置其顏色爲紅色。
p:first-of-type { color: red; }
匹配第一個類型爲 p
的元素並設置及顏色爲紅色。
p:last-of-type { color: red; }
匹配倒數第一個類型爲 p
的元素並設置及顏色爲紅色。
p:only-of-type { color: red; }
匹配父元素只包含惟一類型爲 p
子元素並設置其顏色爲紅色。
h1 { color: red } h2 { color: red } h3 { color: red } h4 { color: red }
利用分組選則器,能夠將上述CSS規則簡寫成下面的形式。
h1,h2,h3,h4 { color: red }
上述兩種寫法效果相同。
僞類 :is()
是一個以多個選擇器作爲參數的函數,匹配由其參數表示的元素。
目前兼容性較差。
*:is(:hover, :focus) { color: red; }
匹配僞類 hover
和 focus
的元素並設置其顏色爲紅色。
負向邏輯組合僞類 :not()
是一個以多個選擇器作爲參數的函數,匹配不在其參數表中的元素。
目前兼容性較差。
button:not([DISABLED]) { color : red; }
匹配不包含 DISABLED
屬性的 button
元素並設置其顏色爲紅色。
目前兼容性較差。
a:has(> img) { border : 1px solid red; }
匹配包含 img
子元素的超連接並設置其邊框爲1象素紅色。
h1 em { color: red; }
匹配 h1
的後代的 em
元素並設置其顏色爲紅色。
h1 > em { color: red; }
匹配 h1
的子元素 em
並設置其顏色爲紅色。
h1 + h2 { color: red; }
匹配 h1
的相鄰兄弟元素 h2
並設置其顏色爲紅色。
h1 ~ h2 { color: red; }
匹配 h1
的後面的同級結點 h2
並設置其顏色爲紅色。
W3C Working Draft : Selectors Level 4 - 21 November 2018
本文是@毛瑞依據CSS選擇器規範4擇取的部分CSS選擇器內容編寫而成。因本人水平有限,理解和翻譯時不免有誤差和錯誤,還請程序員朋友多多指正!
文中一些選擇器兼容性還不好,只能作爲學習儲備,不適用於實際產品中運用。
費力原創十分不易,請你們轉載時必定要明確註明出處來自【三十課】!