XML technologies / CSS / Advanced websites with CSS / Behind the scenes / Selectors and the structure of CSS rules / Class selector
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
A class selector defines a style for several HTML elements at once. Each element that should receive the style gets a class attribute referring to the rule.
To create a class in a stylesheet, choose any name and put a dot before it. No space may appear between the dot and class name.
The following rule applies to the class named special.
.special {
font-family: Verdana, Arial, Helvetica, sans-serif;
}The class special can be added to any HTML element that should display these properties:
<h1 class="special">Eine spezielle Überschrift</h>
<p class="special">Ein Absatz mit Inhalt.</p>To restrict a class to particular element types, put the type before the dot in the selector, again without spaces. In the following example, the class special is created only for the paragraph element.
p.special {
font-family: Verdana, Helvetica, Arial, sans-serif;
}If a class is created only for a specific element type (such as p.special) and then assigned to a different element, the rule does not apply.
An element can also be assigned to several classes. List their names in the class attribute, separated by spaces:
<p class="special aufregend">Absatz! Mit! Inhalt!</p>
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