CSS :first-of-type 选择器
实例
指定父元素的首个 p 元素的背景色:
<!DOCTYPE html><html><head><style>p:first-of-type {background: red;}</style></head><body><p>第1个段落。</p><p>第2个段落。</p><p>第3个段落。</p><p>第4个段落。</p></body></html>
浏览器支持
表格中的数字注明了完全支持该属性的首个浏览器版本。
| 选择器 | |||||
|---|---|---|---|---|---|
| :first-of-type | 4.0 | 9.0 | 3.5 | 3.2 | 9.6 |
定义和用法
:first-of-type 选择器匹配属于其父元素的特定类型的首个子元素的每个元素。
提示:等同于 :nth-of-type(1)。
语法
:first-of-type {css declarations;}