HTML Dog
跳至导航

CSS 选择器

来自最新 CSS3 标准的每一个 CSS 选择器

基本选择器

选择器 描述 示例
element 类型选择器。匹配一个元素。 p { color: red }
/* 匹配段落 */
.class 选择器。匹配 class 属性的值。 .warning { color: red }
/* 匹配包含 class="warning" 的元素 */
#id ID 选择器。匹配 id 属性的值。 #warning { color: red }
/* 匹配包含 id="warning" 的元素 */
* 通用选择器。匹配所有元素。 * { color: red }
/* 匹配所有元素 */

属性选择器

选择器 描述 示例
[attribute] 匹配包含给定属性的元素。 a[href] { color: red }
/* 匹配具有 href 属性的 a 元素 */
[attribute="x"] 匹配包含给定属性且值等于给定值的元素。 a[href="/sitemap/"] { color: red }
/* 匹配 href 属性值为 "/sitemap/" 的 a 元素 */
[attribute~="x"] 匹配包含给定属性的元素,该属性的值在一个空格分隔的列表中包含子值。 abbr[title~="Style"] { color: red }
/* 匹配 title 属性包含 'Style' 的 abbr 元素(例如 title="Cascading Style Sheets") */
[attribute|="x"] 匹配包含给定属性的元素,该属性的值在一个连字符分隔的列表中包含子值。 html[lang|="en"] { color: red }
/* 匹配 lang 属性包含 'en' 的 html 元素(例如 lang="en-gb") */
[attribute^="x"] 匹配包含给定属性的元素,该属性的值以某个值开头 a[href^="http://"] { color: red }
/* 匹配 href 属性值以 'http://' 开头的 a 元素 */
[attribute$="x"] 匹配包含给定属性的元素,该属性的值以某个值结尾 a[href$=".com"] { color: red }
/* 匹配 href 属性值以 '.com' 结尾的 a 元素 */
[attribute*="x"] 匹配包含给定属性的元素,该属性的值包含某个值 a[href*="htmldog"] { color: red }
/* 匹配 href 属性值包含 'htmldog' 的 a 元素 */

伪类

选择器 描述 示例
:link 匹配尚未访问的链接 a:link { color: blue }
:visited 匹配已访问的链接 a:visited { color: purple }
:active 匹配被激活的元素,例如被点击的链接。 a:active { color: red }
:hover 匹配鼠标光标悬停在其上的元素。 a:hover { text-decoration: none }
:focus 匹配获得焦点的元素,例如通过 Tab 键选中的元素。 a:focus { border: 1px solid yellow }
:target 匹配被链接到的元素(例如通过 <a href="#x"…)。 h2:target { color: red }
/* 匹配被链接到的二级标题 */
:lang() 匹配给定语言的元素。 p:lang(fr) { color: red }
/* 匹配声明为或被认为是法语的段落 */
:first-child 匹配元素的第一个子元素 p:first-child { color: red }
/* 匹配作为元素第一个子元素且为段落的元素 */
:last-child 匹配元素的最后一个子元素 div p:last-child { color: blue }
/* 匹配作为元素最后一个子元素且为段落的元素 */
:first-of-type 匹配元素内同类型的第一个同级元素 li:first-of-type { color: red }
/* 匹配元素内的第一个列表项 */
:last-of-type 匹配元素内同类型的最后一个同级元素 li:last-of-type { color: blue }
/* 匹配元素内的最后一个列表项 */
:nth-child() 匹配作为其父元素的序数子元素的元素。 p:nth-child(3) { color: red }
/* 匹配作为元素第三个子元素且为段落的元素 */
:nth-last-child() 匹配作为其父元素的倒序序数子元素的元素。 p:nth-last-child(2) { color: blue }
/* 匹配作为元素倒数第二个子元素且为段落的元素 */
:nth-of-type() 匹配同类型序数同级元素的元素。 li:nth-of-type(5) { color: red }
/* 匹配元素内的第五个列表项 */
:nth-last-of-type() 匹配同类型倒序序数同级元素的元素。 li:nth-of-type(5) { color: red }
/* 匹配元素内倒数第二个列表项 */
:only-child 当元素是其父元素的唯一子元素时匹配该元素。 article p:only-child { color: red }
/* 匹配作为 article 元素的唯一子元素且为段落的元素 */
:only-of-type 当元素是其同类型唯一同级元素时匹配该元素。 article aside:only-of-type { color: blue }
/* 匹配作为 article 元素中唯一 aside 元素的 aside 元素 */
:empty 匹配没有子元素或内容的元素。 td:empty { border-color: red }
/* 匹配内部没有任何内容的表单元格 */
:root 匹配文档的根元素。在 HTML 中,这将是 html 元素。 :root { background: yellow }
:enabled 匹配未被禁用的表单控件元素 input:enabled { border-color: lime }
/* 匹配未被禁用的 input 元素 */
:disabled 匹配被禁用的表单控件元素 input:enabled { border-color: red }
/* 匹配被禁用的 input 元素 */
:checked 匹配被选中的单选按钮或复选框类型的 input 元素 input:checked { outline: 3px solid yellow }
/* 匹配被选中的 input 元素 */
:not() 否定伪类。匹配不符合某个选择器的元素。 p:not(:first-child) { color: orange }
/* 匹配不是第一个子元素的段落 */

伪元素

选择器 描述 示例
::first-line 匹配元素中的第一行文本 p::first-line { font-weight: bold }
/* 匹配段落的第一行 */
::first-letter 匹配元素中的第一个字母 p::first-letter { font-size: 2em }
/* 匹配段落的第一个字母 */
::before content 属性一起使用,以在元素的初始内容之前生成内容。 h1::before { content: "*" }
/* 在顶级标题的开头添加一个星号 */
::after content 属性一起使用,以在元素的初始内容之后生成内容。 h1::after { content: "+" }
/* 在顶级标题的末尾添加一个加号 */
使用伪元素定位首字母和首行。

组合器

选择器 描述 示例
selector selector 后代组合器。匹配是另一个元素后代的元素。 aside p { color: red }
/* 匹配 aside 元素内的段落 */
selector > selector 子元素组合器。匹配是另一个元素子元素的元素。 .warning > p { color: red }
/* 匹配 class="warning" 元素的直接子段落 */
selector + selector 相邻兄弟组合器。匹配紧跟在另一个元素之后的元素。 h1 + * { color: red }
/* 匹配紧跟在 h1 元素之后的第一个元素 */
selector ~ selector 通用兄弟组合器。匹配在另一个元素之后的所有同级元素。 h2 ~ p { color: red }
/* 匹配 h2 元素之后的所有段落 */