XML technologies / CSS / CSS Cookbook / General topics / Different selectors for assigning styles / Selector for adjacent siblings

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Two elements are adjacent siblings if they are directly “next to each other” in the HTML source.

To select adjacent siblings, join them with a plus sign (+), as shown here:

li + li {
 font-size: 200%;
}

The following figure shows the effect of adjacent sibling selector rules. They apply only to the second and third list items, because these are the only list items directly “next to each other” in the hierarchy.

The adjacent sibling selector affects only the second and third list items

Figure: The adjacent sibling selector affects only the second and third list items because they are directly “next to each other” in the hierarchy.

The diagram in the following figure shows which elements are affected by the CSS rule for adjacent siblings.

Diagram of the elements selected in the preceding figure

Figure: Diagram of the elements selected in the preceding figure.

Warning:
Adjacent siblings are not always supported even in modern browsers, particularly Internet Explorer 6 for Windows. However, Mozilla, Firefox, Opera 5+ and Safari handle adjacent sibling selectors correctly.

  

 

 

 

back to top