XML technologies / CSS / Advanced websites with CSS / Text effects and cascading / Cascading and inheritance in CSS / Specificity
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
How specifically a rule describes an element matters more than its position in the document. The cascade always prefers a specific rule to a more general one. As mentioned, rules for a paragraph are less specific than rules for a paragraph of class warnung.
The following HTML fragment contains two rules that both apply to the element p of class spezial. According to the order-of-events criterion, this paragraph should appear white on blue. Instead, it appears red on white. The first rule, not the last, applies because it outranks the second in specificity (English: specificity).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Warnung</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
p.warnung {
color: red;
background-color: white;
}
.warnung {
color:yellow;
background-color: red;
}
p {
color: white;
background-color: blue;
}
</style>
</head>
<body>
<p class="warnung">Dies ist eine Warnung.</p>
</body>
</html>The more precisely a rule describes an element in its selector, the higher its priority.
Our simple example makes this easy to follow. In more complex documents, however, simply comparing selectors is not enough to determine which applies.
A selector’s specificity is reflected in its specificity score, calculated in the CSS specification using this formula:
(100 x IDWerte) + (10 x WeitereTypen) + BezeichneteElemente
This formula simulates the browser’s algorithm for determining selector specificity. It sums these three components:
The number of ID selectors (for example,
#spezial) in the rule, multiplied by 100.The number of other specific selectors (such as classes or pseudo-classes, but not pseudo-elements), multiplied by 10.
The number of named elements (for example,
pordiv).Add all these values together.
The following table gives selector examples and their specificity scores calculated with this formula.
Inline properties declared with the HTML attribute style are exempt from this formula. Specificity always gives them priority because they inherently apply to a single specific element. No external or embedded rule can override an inline property.
When rules compete for an element, the highest specificity score wins, provided the more important criteria of origin and weight are equal.
The score in the last column is not an absolute value: eleven classes would not outrank one ID. Think of an Olympic medal table: a country with one gold medal ranks above one with eleven silvers.
Table: Selected CSS selectors ordered by specificity.
Selector | IDs | Classes | Elements | Specificity score |
|---|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 0 | 0 | 2 | 2 |
| 0 | 1 | 0 | 10 |
| 0 | 1 | 1 | 11 |
| 0 | 1 | 3 | 13 |
| 1 | 0 | 0 | 100 |
| 1 | 0 | 1 | 101 |
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