--- title: CSS选择器 createTime: 2018/09/20 03:29:20 permalink: /article/8vev8ixl author: pengzhanbo tags: - css top: false type: null --- ## Basic Selectors 基础选择器 ### Element selector 根据 element type 匹配 一组元素 ``` html ... ...

content

... ``` ### Class selector 根据 element 声明的 class属性值 匹配一组元素 ``` html ... ...

content

... ``` ### ID selector 根据 element 声明的 ID属性值,匹配一个元素(一个页面中,ID具有唯一性) ``` html ... ...

content

... ``` ### Universal selector 通配符,匹配所有 element ``` html ... ...

content

span ... ``` ## Attribute Selectors ### \[attribute\] selector 匹配声明了该attribute的 一组 element ``` html ... ... content ... ``` ### \[attribute="x"\] selector 匹配声明了该attribute,且值为 x 的一组 element ``` html ... ... abbr ... ``` ### \[attribute~="x"\] selector 匹配声明了该attribute,且值包含了 单词 x 的一组 element ``` html ... ... abbr ... ``` ### \[attribute|="x"\] selector 匹配声明了该attribute,且值包含了一个 `x-` 开头的连字符拼接的词 的一组 element ``` html ... ... abbr ... ``` ### \[attribute^="x"\] selector 匹配声明了该attribute,且值是以 x 作为开头的 一组 element ``` html ... ... content ... ``` ### \[attribute$="x"\] selector 匹配声明了该attribute,且值是以 x 作为结尾的 一组 element ``` html ... ... content ... ``` ### \[attribute*="x"\] selector 匹配声明了该attribute,且值包含了子串 x 的 一组 element ``` html ... ... content ... ``` ## Combinators 关系选择器 关系选择器适用于 任意选择器 的组合 ### selector1 selector2 后代关系选择器 匹配 selector1 的元素中,所有 selector2 的 元素 ``` html ... ...

... ``` ### selector1 > selector2 子代关系选择器 匹配 selector1 的下一级满足 selector2 的 一组元素 ``` html ... ...

... ``` ### selector1 + selector2 相邻兄弟选择器 匹配selector1后同级的紧跟的selector2的一个元素 ``` html ... ...

... ``` ### selector ~ selector2 一般兄弟选择器 匹配selector1后同级的selector2的一组元素 ``` html ... ...

... ``` ## Group Selectors 组合选择器 ### selector1, selector2, ... 匹配用`,` 隔开的所有选择器 ``` html ... ...

... ``` ## Pseudo-elements 伪元素选择器 ### ::first-letter 匹配 element中的首个字符(字母、中文字、符号均可) ``` html ... ...

One

... ``` ### ::first-line 匹配 element中的首行文字 ``` html ... ...

One Two
Three

... ``` ### ::before 和 `content` 属性一起使用,在匹配的元素内容之前生成的内容 ``` html ... ...

One Two

... ``` ### ::after 和 `content` 属性一起使用,在匹配的元素内容之后生成的内容 ``` html ... ...

One Two

... ``` ## Pseudo-classes 伪类选择器 ### :link 匹配一个没有被访问过的链接 ``` html ... ... link ... ``` ### :visited 匹配一个已访问过的链接 ``` html ... ... link ... ``` ### :active 匹配一个正在被激活的链接 ``` html ... ... link ... ``` ### :hover 匹配一个被光标悬停的链接 ``` html ... ... link ... ``` ### :focus 匹配一个具有焦点的元素 ``` html ... ... ... ``` ### :target 匹配一个已被链接到的元素。 例如通过``链接的head元素 ``` html ... ...

heading

... ``` ### :first-child 匹配在同一个父元素内的的第一个子元素 ``` html ... ...

first child

second child

... ``` ### :last-child 匹配在同一个父元素内的的最后一个子元素 ``` html ... ...

first child

last child

... ``` ### :nth-child(n) 匹配在同一个父元素内的从上往下数的第N子个元素 ``` html ... ...

first child

second child

... ``` ### :nth-last-child(n) 匹配在同一个父元素内的从下往上数的第N个子元素 ``` html ... ...

first child

second child

... ``` ### :first-of-type 匹配在同一个父元素中的同类型的第一个元素 ``` html ... ...

first child

second child

... ``` ### :last-of-type 匹配在同一个父元素中的同类型的最后一个元素 ``` html ... ...

first child

second child

... ``` ### :nth-of-type(n) 匹配在同一个父元素中的同类型的从上往下数的第N个元素 ``` html ... ...

first child

second child

... ``` ### :nth-last-of-type(n) 匹配在同一个父元素中的同类型的从下往上数的第N个元素 ``` html ... ...

first child

second child

... ``` ### :only-child 如果元素是其父元素的唯一子元素,则匹配该元素 ``` html ... ...

only child

... ``` ### :only-type 如果元素是其父元素的唯一的同类型的子元素,则匹配该元素 ``` html ... ...

only

... ``` ### :lang(lang) 匹配给定语言的元素 ``` html ... ...
This French quote has a nested quote inside.
... ``` ### :empty 匹配没有子元素或内容的元素 ``` html ... ...
... ``` ### :root 匹配文档的根元素, (即匹配的 ``元素) ### :enabled 匹配未被禁用的表单控件元素 ### :disabled 匹配被禁用的表单控件元素 ### :checked 匹配选中的单选或复选框类型的输入元素。 ### :not(selector) 协商伪类。匹配不匹配选择器的元素。 ## 实验中的 Selectors 这些选择器在某些浏览器中尚处于开发中,功能对应的标准文档可能被修改,在未来的版本中可能发生变化,谨慎使用。 ### :any-link 匹配有链接锚点的元素,而不管元素是否被访问过。 即会匹配每一个有 `href`属性的``,``,``的元素,匹配到所有的`:link`或`:visited`。 ``` html ... ... External link
Internal target link
Placeholder link (won't get styled) ... ``` ::: caniuse css-any-link ::: ### :dir(dir) 如果元素的内容的书写方向是 dir , 则匹配该元素 *dir* : ltr | rtl ``` html ... ...
test1
test2
עִבְרִית
... ``` ::: caniuse css-dir-pseudo ::: ### :has(selector) 如果一个元素A恰好满足包含了selector 匹配的元素,则匹配元素A ``` html ... ... ... ``` ::: caniuse css-has ::: ### :is() / :any() 匹配一组选择器选中的元素。 优先级是由它的选择器列表中优先级最高的选择器决定。 ``` html ... ... ... ``` ::: caniuse css-matches-pseudo ::: ### :where() 匹配一组选择器选中的元素。 :where() 的优先级总是为 0。 ``` html ... ... ... ```