Skip to the workspace
Pebellslearn by building
CSS Basics  ›  Capstone Project

Cascading Order

Cascading order 15 minThe cascade chooses among applicable declarations using origin and importance, cascade layers, specificity, scoping proximity and source order. Specificity is only one stage of that decision.

css / Cascading Order

Explore each scene at your own pace.

01 · The rule

The rule

Cascading Order

The cascade chooses among applicable declarations using origin and importance, cascade layers, specificity, scoping proximity and source order. Specificity is only one stage of that decision.

Resolve a conflict in cascade order before comparing selector specificity.

The lassi's price is green in one rule, black in another and orange in a third. Only one colour can reach the screen. The cascade is the browser's fixed procedure for picking the winner, and once you know its order, "why is my CSS not working?" becomes a question you can answer in seconds.

The rule

Importance first, then inline, then specificity, then the last one wins.

When several declarations set the same property on the same element, the browser checks these in order and stops at the first one that separates them:

  1. Importance. A declaration marked !important beats every normal one.
  2. Inline. A declaration in a style attribute beats any selector-based rule of the same importance.
  3. Specificity. The more specific selector wins. Count it as three columns, (ids, classes, elements): #menu is (1,0,0), .dish .price is (0,2,0), .price is (0,1,0), p is (0,0,1). Compare the left column first; only if it ties, move right.
  4. Source order. If everything else ties, the declaration that comes later wins.

One thing is not on the list: whether CSS came from a <style> element or a linked file. Both are just stylesheets. Whichever the browser reads later wins a tie.

.price { color: orange; }       /* (0,1,0) */
.dish .price { color: teal; }   /* (0,2,0) */
p { color: black; }             /* (0,0,1), comes last */

The lassi's price is teal. Nothing is important or inline, so specificity decides, and (0,2,0) is highest. Coming last does not rescue p.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.