XML technologies / CSS / Advanced websites with CSS / Behind the scenes / Selectors and CSS rule structure / ID: selector
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
An ID selector defines rules for a single HTML element on a page. A rule with an ID selector overrides all other CSS property definitions applying to that element. Choose a name for the ID and write it after a # symbol. (Note: an ID can also be combined with a particular element type, written before #, for example div#searchbox. Since an ID can be assigned to only one element in an HTML document anyway, restricting the rule to a particular element type is unnecessary.) Only one element in an HTML document can be assigned to the ID rule. Assigning the same ID to multiple HTML tags is invalid under the CSS2 specification.
The following rule defines an element with ID einzigartig:
#einzigartig {
font-size: 70%;
}An ID selector also applies only to elements whose HTML attribute contains the selector’s name. The following example assigns an HTML element to an ID rule:
<h4 id="einzigartig">Das wird eine winzige Überschrift</h4>Suppose an HTML document contains five instances of the class <div class="seitenleiste">. To make the single paragraph div containing a search box take some properties from the rule for class seitenleiste, proceed as follows:
div.seitenleiste {
border: 1px solid black;
background-color: yellow;
}
#suchbox {
background-color: orange;
}The element div containing the search box then looks like this in HTML:
<div id="suchbox" class="seitenleiste">
<!-- HTML für Suchleiste -->
</div>Because the element div has the attributes id="suchbox" and class="seitenleiste", all declarations in the rule for seitenleiste apply to the search box, but it uses its own background color, defined with background-color in the rule for #suchbox. Under the principle of cascading, which determines which declaration wins when different rules define the same property for an element, an ID rule always takes precedence over a class rule. For more about cascading CSS rules, see Cascading and inheritance in CSS.
The same result can be achieved by creating a new class for the area div containing the search box instead of using an ID rule. This requires a little more programming but offers greater flexibility later. Suppose you have a class or other rule applying to all level-three headings except one, for which you define an ID selector. What if a redesign or content changes produce more such exceptions? The ID selector would become a dead end: no other style rule can override it.
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