To apply CSS to an adjacent sibling selector, you use the CSS + selector for the first element, followed by a plus (+) sign and a selector for the sibling element.
CSS + (Plus) Selector Example
In the following example, the CSS selector will select any <p>
(paragraph) elements that are adjacent to the h2
elements. That means it will select the first <p>
element that follows the h2
element:
h2 + p {margin-top: 10px;}
Conclusion
To select a sibling element that’s adjacent to another element, use an adjacent sibling selector that consists of the first element, a plus sign (+), and the sibling element.
Leave a comment