XML technologies / CSS / Advanced websites with CSS / Behind the scenes / Selectors and the structure of CSS rules / Adjacent sibling selectors

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

Adjacent elements have nothing to do with inheritance. They are elements that immediately follow one another in the HTML document. They must not be nested, but must occupy the same level in the inheritance tree. Consider the following HTML fragment:

<h1>Das hier ist wichtig!</h1>
<h2>Die erste wichtige Sache</h2>
<h2>Die zweite wichtige Sache</h2>

The first h2 heading is an adjacent sibling of the h1 heading. The second h2 heading is not adjacent to the h1 heading because it does not immediately precede or follow it.

Adjacent elements are joined in a rule by a plus sign (+):

h1 + h2 {
  margin-top: 11px;
}

This rule creates an eleven-pixel gap between an h1 heading and the immediately following h2 heading. If a paragraph appears between the headings in the HTML document, h1 and h2 are not adjacent, so the rule does not apply.

Here too, Internet Explorer for Windows up to and including version 6 is the only, but unfortunately the most important, browser not supporting this selector. Support is announced for version 7. The adjacent sibling selector is therefore still rare on web pages.

  

 

 

 

back to top