Модуль 3 · Урок 10

Cascade, specificity та inheritance

Specificity — не перший і не єдиний крок cascade. Спочатку browser відсіює irrelevant rules, порівнює origin, importance і layer; лише потім selectors у winning context змагаються за specificity, proximity та source order.

Cascade orderID–CLASS–TYPEInheritanceSource order

П’ять запитань до conflict

  1. Relevance: selector matches, media/supports condition true?
  2. Origin/importance/layer: яке declaration знаходиться у precedence context?
  3. Specificity: який weight ID–CLASS–TYPE серед declarations цього context?
  4. Scoping proximity: чи є competing scoped rules?
  5. Source order: якщо все інше рівне, яке declaration останнє?

Не починайте з !important

Він змінює importance order і створює дорожчі майбутні overrides. Спочатку поясніть conflict і спростіть selector architecture.

Specificity без міфів

SelectorWeightПояснення
p0–0–1Один type
.hero p0–1–1Class + type
#main .hero p1–1–1ID + class + type
:where(.hero) p0–0–1:where() має zero specificity

DOM proximity сама по собі не додає ваги. За рівної specificity і context перемагає later source order.

Inheritance і defaulting

Inheritedcolor і font properties зазвичай отримують computed value parent, якщо немає direct declaration.
Non-inheritedmargin, border, background зазвичай беруть initial value.
Direct targetDeclaration на child перемагає inherited value незалежно від specificity ancestor.
inheritПримусово бере computed value parent.
initialПовертає property initial value, не browser default element style.
unsetПрацює як inherit для inherited і initial для non-inherited property.

Практика

  1. Заповніть cascade-trace.csv для hero text, featured card і button link.
  2. Запишіть усі candidate declarations до зміни source.
  3. Приберіть ID escalation і досягніть predictable override класами/порядком.
  4. Перевірте inherited color окремо від direct link color.

Офіційні джерела

Практична перевірка · урок 10 з 60

Закріпіть матеріал уроку

Три сценарні питання. Для зарахування уроку потрібно дати щонайменше дві правильні відповіді.

1. Коли cascade порівнює specificity?
2. Яка specificity у .hero p?
3. Яка specificity у #main .hero p?