E:first-child { sRules }
示例代碼:
- 列表項一
- 列表項二
- 列表項三
- 列表項四
在上述代碼中,如果我們要設置第一個li的樣式,那么代碼應該寫成li:first-child{sRules}
,而不是ul:first-child{sRules}
。
示例代碼:
p:first-child{color:#f00;}
我是一個p
這段代碼你能看到p元素被命中變成了紅色
示例代碼:
p:first-child{color:#f00;}
我是一個標題
我是一個p
只是在p前面加了一個h2標簽,你會發現選擇器失效了,沒有命中p,why?
IE | Firefox | Chrome | Safari | Opera | iOS Safari | Android Browser | Android Chrome |
---|---|---|---|---|---|---|---|
6.0 | 2.0+ | 4.0+ | 3.1+ | 3.5+ | 3.2+ | 2.1+ | 18.0+ |
IE7.0+ |