1. 程式人生 > >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節點

[attribute]

[target]

選擇帶有target屬性的所有節點

[attribute=value]

[target=blank]

選擇target="blank"
的所有節點 [attribute~=value] [title~=flower] 選擇title屬性包含單詞flower的所有節點 :link a:link 選擇所有未被訪問的連結 :visited a:visited 選擇所有已被訪問的連結 :active a:active 選擇活動連結 :hover a:hover 選擇滑鼠指標位於其上的連結 :focus input:focus 選擇獲得焦點的input節點 :first-letter p:first-letter 選擇每個p節點的首字母 :first-line p:first-line 選擇每個p節點的首行 :first
-child p:first-child 選擇屬於父節點的第一個子節點的所有p節點 :before p:before 在每個p節點的內容之前插入內容 :after p:after 在每個p節點的內容之後插入內容 :lang(language) p:lang 選擇帶有以it開頭的lang屬性值的所有p節點 element1~element2 p~ul 選擇前面有p節點的所有ul節點 [attribute^=value] a[src^="https"] 選擇其src屬性值以https開頭的所有a節點 [attribute$=value] a[src$=".pdf"
] 選擇其src屬性以.pdf結尾的所有a節點 [attribute*=value] a[src*="abc"] 選擇其src屬性中包含abc子串的所有a節點 :first-of-type p:first-of-type 選擇屬於其父節點的首個p節點的所有p節點 :last-of-type p:last-of-type 選擇屬於其父節點的最後p節點的所有p節點 :only-of-type p:only-of-type 選擇屬於其父節點唯一的p節點的所有p節點 :only-child p:only-child 選擇屬於其父節點的唯一子節點的所有p節點 :nth-child(n) p:nth-child 選擇屬於其父節點的第二個子節點的所有p節點 :nth-last-child(n) p:nth-last-child 同上,從最後一個子節點開始計數 :nth-of-type(n) p:nth-of-type 選擇屬於其父節點第二個p節點的所有p節點 :nth-last-of-type(n) p:nth-last-of-type 同上,但是從最後一個子節點開始計數 :last-child p:last-child 選擇屬於其父節點最後一個子節點的所有p節點 :root :root 選擇文件的根節點 :empty p:empty 選擇沒有子節點的所有p節點(包括文字節點) :target #news:target 選擇當前活動的#news節點 :enabled input:enabled 選擇每個啟用的input節點 :disabled input:disabled 選擇每個禁用的input節點 :checked input:checked 選擇每個被選中的input節點 :not(selector) :not 選擇非p節點的所有節點 ::selection ::selection 選擇被使用者選取的節點部分