The CSS
selector list
(
,
) selects all the matching nodes.
/* Selects all matching elements */
span,
div {
border: red 2px solid;
}
To reduce the size of style sheets, one can group selectors in comma-separated lists.
element, element, element { style properties }
Grouping selectors in a single line using a comma-separated lists.
h1, h2, h3, h4, h5, h6 { font-family: helvetica; }
Grouping selectors in a multiple lines using a comma-separated lists.
#main,
.content,
article {
font-size: 1.1em;
}
A downside to using selector lists is that the following aren't equivalent:
h1 { font-family: sans-serif }
h2:maybe-unsupported { font-family: sans-serif }
h3 { font-family: sans-serif }
h1, h2:maybe-unsupported, h3 { font-family: sans-serif }
This is because a single unsupported selector in a selector list invalidates the whole rule.
A way to remedy this us to use the
:is()
or
:where()
selectors, which accept a forgiving selector list. This will ignore invalid selectors in the list but accept those which are valid.
h1 { font-family: sans-serif }
h2:maybe-unsupported { font-family: sans-serif }
h3 { font-family: sans-serif }
:is(h1, h2:maybe-unsupported, h3) { font-family: sans-serif }
| 规范 | 状态 | 注释 |
|---|---|---|
|
Selectors Level 4
The definition of 'Selector Lists' in that specification. |
工作草案 | Renamed to "selector list" |
|
CSS Level 1
The definition of 'grouping' in that specification. |
推荐 | 初始定义 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Selector list (
,
)
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 3 | Opera 完整支持 3.5 | Safari 完整支持 1 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
完整支持
选择器列表
:active
:any-link
:checked
:blank
:default
:defined
:dir()
:disabled
:empty
:enabled
:first
:first-child
:first-of-type
:fullscreen
:focus
:focus-visible
:focus-within
:has()
:host()
:host-context()
:hover
:indeterminate
:in-range
:invalid
:is() (:matches(), :any())
:lang()
:last-child
:last-of-type
:left
:link
:not()
:nth-child()
:nth-last-child()
:nth-last-of-type()
:nth-of-type()
:only-child
:only-of-type
:optional
:out-of-range
:placeholder-shown
:read-only
:read-write
:required
:right
:root
:scope
:target
:valid
:visited
:where()
::-moz-progress-bar
::-moz-range-progress
::-moz-range-thumb
::-moz-range-track
::-webkit-progress-bar
::-webkit-progress-value
::-webkit-slider-runnable-track
::-webkit-slider-thumb
::after (:after)
::backdrop
::before (:before)
::cue
::cue-region
::first-letter (:first-letter)
::first-line (:first-line)
::grammar-error
::marker
::part()
::placeholder
::selection
::slotted()
::spelling-error