一般兄弟結合子
一般兄弟結合子 (general sibling combinator, ~
) は 2 個のセレクターを結びつけ、 1 つ目の要素の後に 2 つ目の要素のがあり(直後である必要はない)、かつ両者が同じ親要素の子であるすべてのパターンに一致します。
/* 任意の画像の兄弟で、
その画像より後方にある段落 */
img ~ p {
color: red;
}
構文
先行する要素 ~ 選択される要素 { スタイルプロパティ }
例
CSS
p ~ span {
color: red;
}
HTML
<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>
結果
仕様書
Specification |
---|
Selectors Level 4 # general-sibling-combinators |
ブラウザーの互換性
BCD tables only load in the browser