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:

Conflicting rules are resolved in five stages. A decision at a higher stage skips the lower ones.

  1. The browser checks applicable rules for !important, giving marked rules priority: weight.
    !important can be added to any declaration, as here, invalidating later declarations that would normally apply.

div.warnung {
  background-color: red !important;
}

  More examples follow later.

  1. Rules marked !important in a user’s local sheet outrank all others: origin combined with weight.

  2. Among rules without !important, author sheets outrank normal user sheets: origin again.

  3. More specific rules outrank general ones. A rule for all paragraphs p is less specific than a rule with a selector for paragraphs p of a class or ID. Rules in HTML’s attribute style always take priority because they target one element: specificity.

  4. 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.

  

 

 

 

back to top