--- 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 ... ...
One
... ``` ### ::first-line 匹配 element中的首行文字 ``` html ... ...
One Two
Three
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 ... ...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
This French quote has anestedquote inside.
...
```
::: caniuse css-has
:::
### :is() / :any()
匹配一组选择器选中的元素。
优先级是由它的选择器列表中优先级最高的选择器决定。
``` html
...
...
...
```
::: caniuse css-matches-pseudo
:::
### :where()
匹配一组选择器选中的元素。
:where() 的优先级总是为 0。
``` html
...
...
...
```