General sibling combinator - CSS: Cascading Style Sheets 编辑
The 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;
}
Syntax
former_element ~ target_element { style properties }
Examples
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>
Result
Specifications
Specification | Status | Comment |
---|---|---|
Selectors Level 4 The definition of 'subsequent-sibling combinator' in that specification. | Working Draft | Renames it the "subsequent-sibling" combinator. |
Selectors Level 3 The definition of 'general sibling combinator' in that specification. | Recommendation | Initial definition. |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论