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.
Copyright © 2006 of the German translation, dpunkt.verlag GmbH
You may print the online version for your private use.
Otherwise, this chapter from “CSS − Anspruchsvolle Websites mit Cascading Stylesheets” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
dpunkt.verlag GmbH, Ringstraße 19, 69115 Heidelberg
back to top