一、選擇器
1、普通選擇器
通用選擇器 * * 選擇所有元素。
類選擇器 .class .message 選擇 class="intro" 的所有元素。
id選擇器 #id #head 選擇 id="firstname" 的所有元素。
元素選擇器 el p 選擇所有 <p> 元素。
選擇器分組 el,el div,p 選擇所有 <div> 元素和所有 <p> 元素。
后代選擇器 el el div p 選擇 <div> 元素內部的所有 <p> 元素。
子元素選擇器 el > el div>p 選擇 <div> 的第一子代的 所有<p> 元素。
相鄰兄弟選擇器 el + el div+p 選擇與<div>同級且緊接在其后的第一個 <p> 元素
2、屬性選擇器
[attribute] [target] 選擇帶有 target 屬性所有元素
[attribute=value] [target=_blank] 選擇 target="_blank" 的所有元素
[attribute~=value] [title~=flower] 選擇 title 屬性包含單詞 "flower" 的所有元素
[attribute¦=value] [lang¦=en] 選擇 lang 屬性值以 "en" 開頭的所有元素。
[attribute^=”value”] [abc^=”def”] 選擇 abc 屬性值以 “def” 開頭的所有元素
[attribute$=”value”] [abc$=”def”] 選擇 abc 屬性值以 “def” 結尾的所有元素
[attribute*=”value”] [abc*=”def”] 選擇 abc 屬性值中包含子串 “def” 的所有元素
3、偽類
:link a:link 選擇所有未被訪問的鏈接
:visited a:visited 選擇所有已被訪問的鏈接
:active a:active 選擇正在被點擊的活動鏈接
:hover a:hover 選擇鼠標指針位於其上的鏈接
:focus input:focus 選擇獲得焦點的 input 元素
:lang(language) p:lang(it) 選擇帶有以 "it" 開頭的 lang 屬性值的每個 <p> 元素
注:link、visited、active、hover的順序,為LoVe HAte
4、偽元素
類型 eg 描述
:before p:before 在每個 <p> 元素的內容之前插入內容
:after p:after 在每個 <p> 元素的內容之后插入內容
:first-letter p:first-letter 選擇每個 <p> 元素的首字母
:first-line p:first-line 選擇每個 <p> 元素的首行
:first-child p:first-child 選擇屬於父元素的第一個子元素的每個 <p> 元素
el1~el2 p~ul 選擇前面有 <p> 元素的每個 <ul> 元素。
: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(2) 選擇屬於其父元素的第二個子元素的每個 <p> 元素。
:nth-last-child(n) p:nth-last-child(2) 同上,從最后一個子元素開始計數。
:nth-of-type(n) p:nth-of-type(2) 選擇屬於其父元素第二個 <p> 元素的每個 <p> 元素。
:nth-last-of-type(n) p:nth-last-of-type(2) 同上,但是從最后一個子元素開始計數。
: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) 選擇非 <p> 元素的每個元素。
::selection ::selection 選擇被用戶選取的元素部分。
二、定位
1)屬性定位
1.css可以通過元素的id、class、標簽這三個常規屬性直接定位到
2.如下是百度輸入框的的html代碼:
<input id="kw" class="s_ipt" type="text" autocomplete="off" maxlength="100" name="wd"/>
3.css用#號表示id屬性,如:#kw
4.css用.表示class屬性,如:.s_ipt
5.css直接用標簽名稱,無任何標示符,如:input
2)其它屬性
1.css除了可以通過標簽、class、id這三個常規屬性定位外,也可以通過其它屬性定位
2.以下是定位其它屬性的格式
[name=wd] [autocomplete='off'][maxlength='255']
3)標簽
css頁可以通過標簽與屬性的組合來定位元素
input.s_ipt input#kw input[id='kw']
4)層級關系
//form的id屬性
form#form>span>input
//form的class屬性
form.fm>span>input
5)索引
css也可以通過索引nth-child(1)來定位子元素,直接翻譯過來就是第幾個小孩
總結:選擇標簽后,找第幾個小孩即可
Select控件第三個Opel
#select>select>option:nth-child(3)
CheckBox第一個Volvo
#checkbox>input:nth-child(1)
CheckBox第二個Saab
#checkbox>input:nth-child(4)
RadioBox第二個Saab
#radio>input:nth-child(4)
通過索引nth-of-type(2)來定位子元素,按照分類指定
選擇select的saab
#select>select>option:nth-of-type(2);
選擇 id 為 radio 的 div 下的第 1 個子節點
div#radio>input:nth-of-type(4)+label
選擇id 為radio 的div 下的第4 個input 節點之后挨着的 label
節點
div#radio>input:nth-of-type(4)~label
