The CSS Cascade and Inheritance
Learning Objectives
- Understand cascading rules.
- Understand specificity and source order.
- Explain inherited and non-inherited properties.
- Use initial, inherit, unset, revert and revert-layer.
The Cascade
When multiple declarations apply to the same element and property, CSS resolves them through the cascade. The process considers origin and importance, cascade layers, specificity, scoping proximity where applicable, and source order.
Example: Specificity
p { color: black; }
.note { color: blue; }
#message { color: red; }<p id="message" class="note">Which color?</p>For ordinary author declarations outside differing layers or importance levels, the ID selector is more specific than the class and element selectors, so this paragraph is red.
Source Order
p { color: green; }
p { color: orange; }When otherwise equivalent declarations compete, the later declaration wins.
Inheritance
body { color: navy; font-family: Arial, sans-serif; }
.card { border: 1px solid gray; }
Text color and font family commonly inherit from ancestors. Borders, margins, and padding do not normally inherit automatically.
Special CSS-wide Keywords
inherit: use the parent element's computed value.initial: use the property's initial value.unset: behave as inherit for inherited properties and initial for others.revert: roll back the value through the cascade to an earlier origin.revert-layer: roll back the value to an earlier cascade layer.
Important Declarations
p { color: red !important; }The important flag changes cascade priority. Avoid using it as a routine fix because it makes styles harder to override and maintain.
Practice
Create three conflicting selectors and predict the result. Then change the selectors, source order, and inherited properties to test your understanding.
Quiz
- What does specificity help determine? (A) Which competing declaration wins (B) Image dimensions only (C) HTML validation (D) Server speed. Answer: A. Specificity is one cascade criterion.
- Which selector is normally more specific: #menu or .menu? (A) #menu (B) .menu (C) Equal (D) Neither. Answer: A. An ID selector has greater specificity than a class selector.
- When equivalent rules have equal priority and specificity, what generally matters? (A) Source order (B) File size only (C) HTML title (D) Screen brightness. Answer: A. The later declaration generally wins.
- Which property commonly inherits? (A) color (B) margin (C) padding (D) border. Answer: A. Text color normally inherits.
- Which property does not normally inherit? (A) font-family (B) color (C) margin (D) line-height. Answer: C. Margin does not normally inherit.
- What does inherit do? (A) Takes the parent's computed value (B) Deletes the element (C) Resets all CSS (D) Adds a class. Answer: A. It explicitly requests inheritance.
- What does !important do? (A) Changes cascade priority (B) Creates a selector (C) Imports a file (D) Makes a property responsive. Answer: A. It marks a declaration important.
- Does the most specific selector always win regardless of importance? (A) Yes (B) No (C) Only in Firefox (D) Only with IDs. Answer: B. Cascade priority is considered before specificity.
- What does unset do? (A) Inherit for inherited properties, otherwise initial (B) Delete HTML (C) Set everything to red (D) Disable stylesheets. Answer: A. That is the behavior of unset.
- What is the best approach to maintainable CSS? (A) Use clear selectors and avoid unnecessary !important (B) Make everything important (C) Repeat all rules (D) Use only IDs. Answer: A. Clear, manageable cascade rules reduce conflicts.