CSS Portal

CSS Subsequent-Sibling Combinator (~) Selector

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The Subsequent-Sibling Combinator () selector, is used in CSS to target elements that share the same parent and appear after a specified element in the document tree. Unlike the Child Combinator (>), which only selects immediate children, the Subsequent-Sibling Combinator () selector does not require the matched elements to be directly adjacent; any element that follows the specified element, as long as they share the same parent, will be selected. This makes it especially useful for styling groups of elements dynamically based on the presence of a preceding element.

One practical use of the Subsequent-Sibling Combinator (~) selector is in creating interactive or state-based designs. For instance, when combined with form elements such as checkboxes or radio buttons, it can be used to style subsequent content when a particular input is checked. Consider the following example:

<div>
  <input type="checkbox" id="toggle">
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
  <p>Paragraph 3</p>
</div>
#toggle:checked ~ p {
  color: green;
  font-weight: bold;
}

In this example, whenever the checkbox with id toggle is checked, all subsequent p elements that share the same parent are styled with a green, bold font. This demonstrates the selector's ability to propagate styles to multiple siblings after a triggering element.

The Subsequent-Sibling Combinator (~) selector is also useful in layouts or lists where you want to apply styling to items following a specific type of element without changing the HTML structure. For example, in a menu or a list, if one item needs to be highlighted differently, all items that follow can inherit or respond to this condition, often combined with pseudo-classes like :hover or :checked.

By providing a mechanism to select non-adjacent elements efficiently, the Subsequent-Sibling Combinator (~) selector enables more flexible and maintainable CSS, reducing the need for extra classes or JavaScript-based styling. Its behavior emphasizes document order and hierarchy, allowing designers to create cascading effects and interactions purely with CSS.

Syntax

element1 ~ element2 { css declarations; }

Example

<div class="container">
<p>I am the first paragraph (not affected).</p>

<h2 class="target">The Reference Point</h2>

<p>I am a sibling (styled!).</p>
<span>I am a span (not a paragraph, not styled).</span>
<p>I am another sibling further down (styled!).</p>

<div class="other">
<p>I am nested inside a div (not a sibling, not styled).</p>
</div>
</div>
/* Target all <p> elements that follow an element with the class .target */
.target ~ p {
color: #2ecc71;
font-weight: bold;
border-left: 3px solid #2ecc71;
padding-left: 10px;
margin-bottom: 10px;
}

/* Optional styling for the reference point */
.target {
color: #3498db;
text-decoration: underline;
}

Browser Support

The following information will show you the current browser support for the CSS Subsequent-Sibling Combinator (~) selector. Hover over a browser icon to see the version that first introduced support for this selector.

This CSS subsequent-sibling combinator (~) selector is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 3rd January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!