Title rewritten to: Is there a "previous sibling selector"?
P粉262113569
2023-08-20 12:54:00
<p>The plus selector (<code> </code>) selects the next adjacent sibling element. </p>
<p>Is there an equivalent selector for selecting the previous sibling? </p>
I found a way to style all previous sibling elements (as opposed to
~
), depending on your needs.Suppose you have a list of links, and when hovering over one of the links, all previous links should turn red. You can do this:
No, there is no "previous sibling" selector.
In related comments,
is used for the next sibling selector, which is CSS2.1.
~
is used for general successor sibling selectors (meaning that the element is after this element, but not necessarily immediately following), and is a CSS3 selector.See Adjacent Sibling Selectors from Selectors Level 3 and ## from Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification #5.7 Adjacent sibling selector.