Usually, I’m good with CSS, but I can’t seem to figure this one out. If I have a structure of
<div>
<h2 class="open">1</h2>
<h2>2</h2>
<h2>3</h2>
<h2>4</h2>
<h2>5</h2>
</div>
how can I target all of the sibling h2s using the .open class with CSS? My main issue is that sibling selectors (.open + h2) will only target the h2 immediately following .open.
You can select all the following siblings using
~instead of+:If you need to select all
h2elements that aren’t.openwhether they precede or follow.open, there is no sibling combinator for that. You’ll need to use:not()instead:Optionally with a child combinator if you need to limit the selection to
divparents: