general sibling combinator
(
~
) separates two selectors and matches
all iterations
of the second element, that are following the first element (though not necessarily immediately), and are children of the same parent
element
.
/* Paragraphs that are siblings of and
subsequent to any image */
img ~ p {
color: red;
}
former_element ~ target_element { style properties }
p ~ span {
color: red;
}
<span>This is not red.</span> <p>Here is a paragraph.</p> <code>Here is some code.</code> <span>And here is a red span!</span> <span>And this is a red span!</span> <code>More code...</code> <div> How are you? </div> <p> Whatever it may be, keep smiling. </p> <h1> Dream big </h1> <h2> that's all. </h2> <span>And yet again this is a red span!</span>
| 规范 | 状态 | 注释 |
|---|---|---|
|
Selectors Level 4
The definition of 'subsequent-sibling combinator' in that specification. |
工作草案 | Renames it the "subsequent-sibling" combinator. |
|
Selectors Level 3
The definition of 'general sibling combinator' in that specification. |
推荐 | 初始定义。 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
General sibling combinator (
A ~ B
)
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 7 | Opera 完整支持 9 | Safari 完整支持 3 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
完整支持
Adjacent sibling combinator
General sibling combinator
Child combinator
Descendant combinator
Column combinator
: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