HTML & CSS Foundations · 18 min · 130 XP
The cascade without the mystery
Know exactly which rule wins when several target the same element, and keep it that way.
When several rules set the same property on an element, the browser picks one by working down a fixed list. 1. Importance: !important beats normal declarations. 2. Specificity: the more specific selector wins. 3. Order: among equals, the rule that comes later wins. Styles in a style="" attribute beat stylesheet rules. It isn't mysterious, just strict.
p (0, 0, 1) .price (0, 1, 0) .product p (0, 1, 1) .product .price (0, 2, 0) #products p (1, 0, 1) one id beats any number of classes :where(.x) p (0, 0, 1) :where() adds nothing a:hover, [type=email] pseudo-classes and attributes count as classes
Inheritance fills in what isn't set. Text properties (color, font-*, line-height) pass down from parent to child; box properties (padding, border, width) don't.
The habit that avoids fights: keep specificity low and flat. Style with single classes, avoid ids in CSS, and don't nest selectors deeper than you need to. Reaching for !important usually means an earlier rule was too specific; fix that rule instead, or the next change will need an !important of its own. DevTools shows every rule that matched, with the losers crossed out.
#products p { color: #333; }
.sale-price { color: #c00; }
/* <p class="sale-price"> inside <section id="products"> */Loading your workspace…