Can I target a :before or :after pseudo-element with a sibling combinator?

You can’t target :after since it’s content is not rendered in the DOM and it does not manipulate it – for this to work the DOM would have to be re-rendered and CSS can’t manipulate it like this.

Check the specification for detailed understanding: http://www.w3.org/TR/CSS2/generate.html#propdef-content

Generated content does not alter the document tree. In particular, it
is not fed back to the document language processor (e.g., for
reparsing).

I suggest you use JavaScript to do the job for you.

Leave a Comment