XML technologies / CSS / Advanced websites with CSS / Text effects and cascading / Cascading and inheritance in CSS / The principle of cascading
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
Basic cascading is enough for simple CSS, such as external sheets rarely overridden by embedded rules. More complex cascade behavior may never arise.
Complex pages sharing sheets often produce unexpected results because conflicting definitions are resolved differently than intended. Understanding the cascade gives better control and more logical, consistent, efficient CSS.
CSS cascading follows four principles:
Weight
Origin
Specificity
Order of events
Conflicting rules are resolved in five stages. A decision at a higher stage skips the lower ones.
The browser checks applicable rules for
!important, giving marked rules priority: weight.
!importantcan be added to any declaration, as here, invalidating later declarations that would normally apply.
div.warnung {
background-color: red !important;
}More examples follow later.
Rules marked
!importantin a user’s local sheet outrank all others: origin combined with weight.Among rules without
!important, author sheets outrank normal user sheets: origin again.More specific rules outrank general ones. A rule for all paragraphs
pis less specific than a rule with a selector for paragraphspof a class or ID. Rules in HTML’s attributestylealways take priority because they target one element: specificity.Finally, later declarations outrank earlier ones: order of events. Rather than “first come, first served,” think “he who laughs last laughs best.”
The winning rule’s properties apply; the others are ignored.
Designers usually work in reverse, relying on declaration order. That is fine until complexity requires specificity. Origin is outside author control, and the keyword !important should be avoided because of its strong priority.
We will examine stages from the lowest: least important to the browser, most relevant to designers. For clear structure and fewer errors, use higher cascade stages only when necessary.
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