CSS 選擇器大全


在CSS中,選擇器是用於選擇要設置樣式的元素的模式。

選擇器 例子 描述
.class .intro 選擇class=“intro”的所有元素
#id #firstname 選擇id=“firstname”的元素
* * 選擇所有元素
element p 選擇所有<p>元素
element,element div, p 選擇所有<div>元素和所有<p>元素
element element div p 選擇<div>元素內的所有<p>元素
element>element div > p 選擇父元素為<div>元素的所有<p>元素
element+element div + p 選擇緊跟在<div>元素之后的所有<p>元素
element1~element2 p ~ ul 選擇前面帶有<p>元素的每個<ul>元素
[attribute] [target] 選擇具有target屬性的所有元素
[attribute=value] [target=_blank] 選擇target =“_ blank”的所有元素
[attribute~=value] [title~=flower] 選擇標題屬性包含單詞“flower”的所有元素
[attribute|=value] [lang|=en] 選擇具有以“en”開頭的lang屬性值的所有元素
[attribute^=value] a[href^="https"] 選擇其href屬性值以“https”開頭的每個<a>元素
[attribute$=value] a[href$=".pdf"] 選擇href屬性值以“.pdf”結尾的每個<a>元素
[attribute*=value] a[href*="w3schools"] 選擇每個<a>元素,其href屬性值包含子字符串“w3schools”
:active a:active 選擇活動鏈接
::after p::after 在每個<p>元素的內容之后插入一些內容
::before p::before 在每個<p>元素的內容之前插入一些內容
:checked input:checked 選擇每個選中的<input>元素
:default input:default 選擇默認的<input>元素
:disabled input:disabled 選擇每個禁用的<input>元素
:empty p:empty 選擇每個沒有子元素的<p>元素(包括文本節點)
:enabled input:enabled 選擇每個啟用的<input>元素
:first-child p:first-child 選擇作為其父級的第一個子元素的每個<p>元素
::first-letter p::first-letter 選擇每個<p>元素的第一個字母
::first-line p::first-line 選擇每個<p>元素的第一行
:first-of-type p:first-of-type 選擇每個<p>元素,它是其父元素的第一個<p>元素
:focus input:focus 選擇具有焦點的<input>元素
:hover a:hover 選擇鼠標懸停時的鏈接
:in-range input:in-range 選擇具有指定范圍內的值的<input>元素
:indeterminate input:indeterminate 選擇處於不確定狀態的<input>元素
:invalid input:invalid 選擇具有無效值的所有<input>元素
:lang(language) p:lang(it) 選擇lang屬性等於“it”的每個<p>元素(意大利語)
:last-child p:last-child 選擇作為其父項的最后一個子項的每個<p>元素
:last-of-type p:last-of-type 選擇每個<p>元素,它是其父元素的最后一個<p>元素
:link a:link 選擇所有未訪問的鏈接
:not(selector) :not(p) 選擇不是<p>元素的每個元素
:nth-child(n) p:nth-child(2) 選擇作為其父級的第二個子元素的每個<p>元素
:nth-last-child(n) p:nth-last-child(2) 選擇每個<p>元素作為其父元素的第二個子元素,從最后一個子元素開始計算
:nth-last-of-type(n) p:nth-last-of-type(2) 選擇每個<p>元素作為其父元素的第二個<p>元素,從最后一個子元素開始計算
:nth-of-type(n) p:nth-of-type(2) 選擇每個<p>元素,它是其父元素的第二個<p>元素
:only-of-type p:only-of-type 選擇每個<p>元素,它是其父元素的唯一<p>元素
:only-child p:only-child 選擇每個<p>元素,它是其父元素的唯一子元素
:optional input:optional 選擇沒有“required”屬性的<input>元素
:out-of-range input:out-of-range 選擇值超出指定范圍的<input>元素
::placeholder input::placeholder 使用占位符文本選擇<input>元素
:read-only input:read-only 選擇具有指定“readonly”屬性的<input>元素
:read-write input:read-write 選擇未指定“readonly”屬性的<input>元素
:required input:required 選擇具有指定“required”屬性的<input>元素
:root :root 選擇文檔的根元素
::selection ::selection 選擇用戶選擇的元素部分
:target #news:target 選擇當前活動的#news元素(單擊包含該錨名稱的URL)
:valid input:valid 選擇具有有效值的所有<input>元素
:visited a:visited 選擇所有訪問過的鏈接


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM