Web Development Pro | Lesson 011 | CSS Selectors, Cascade, and Inheritance
Web Development Pro · Lesson 11 of 60
Module 2: CSS and responsive design
In this lesson
Prerequisites and learning outcomes
Complete lesson 10 first. This lesson builds on stylesheets and competing declarations. By the end, you can predict applied styles using selector matching, specificity, and inheritance. Plan 10–15 minutes for the core explanation and additional time for the worked practice.
Style a meaningful document
The workshop site already has headings, paragraphs, and navigation. CSS lets you change their presentation without rewriting that meaning. Create styles.css beside index.html and connect it from head with a stylesheet link. Keeping the rules in one file lets several pages share a visual language, though each page must include the link itself.
<link rel="stylesheet" href="styles.css">
A CSS rule contains a selector and declarations. The selector identifies matching elements. Each declaration contains a property, a colon, a value, and normally a terminating semicolon. A missing semicolon between declarations can prevent the browser from understanding the intended values. Browsers usually recover from CSS errors rather than stopping the entire page, which makes careful inspection important.
Select the elements you mean
A type selector such as p matches paragraphs. A class selector such as .notice matches elements whose class list includes notice. An id selector such as #arrival matches the element with that id. A descendant selector, such as .card p, matches paragraphs anywhere inside a card. The space is meaningful; .card.notice matches one element carrying both classes instead.
body { color: #243746; font-family: sans-serif; }
p { color: #334155; }
.notice { color: #8a2400; }
.card p { color: #174b36; }
.notice { font-weight: 700; }
If a paragraph has class="notice" and is inside .card, several selectors match it. The declarations do not replace whole rules as indivisible units. CSS resolves each property separately, so color may come from one rule and font-weight from another. This is why a paragraph can remain bold even when a more specific color rule wins.
Understand the cascade in the right order
The full cascade considers origins, importance, layers, specificity, and other ordering rules. It is not simply “the last declaration always wins.” In this lesson all shown declarations are normal author rules in the same unlayered context, with no animations or inline styles. Under those conditions, specificity breaks competing matches, and later source order breaks equal-specificity ties.
For the simple selectors here, compare ids first, then classes and similar selectors, then element names. .card p has one class and one type; .notice has one class and no type, so .card p wins their color conflict. If two .notice rules both set color in the same context, the later one wins. Do not add these counts as ordinary decimal numbers; the categories are compared in order.
An !important declaration changes priority rules, but it is not a good first repair for a stylesheet you do not understand. It can make future overrides harder and interact differently with layers and user styles. First inspect which selector matches, which declaration wins, and why. A small clear selector is usually easier to maintain than increasingly specific chains.
Separate inheritance from direct styling
Some properties, such as color and font-family, normally inherit from a parent when the child has no applicable value of its own. Many box-related properties, such as margin and border, do not. Setting a border on body does not give every paragraph the same border. Inheritance is property-specific, not a universal copying mechanism.
A directly specified color on a paragraph beats an inherited color from body even if the body rule uses an id selector. The competing declarations are not targeting the same element in that case. Inheritance fills a value when needed; it does not compete as if the parent's selector had also matched every descendant. This distinction resolves many apparent specificity mysteries.
Debug with the actual applied rule
When a paragraph has the wrong color, check that styles.css loaded first. Then inspect the element's matching rules and computed color in browser developer tools when available. A crossed-out declaration often indicates that another rule won; it does not necessarily mean the syntax is invalid. A rule absent from the matching list may have the wrong selector or an unloaded file.
Keep class names tied to roles, such as notice or workshop-card, rather than temporary appearances like red-text-2. The presentation may change while the role remains. Use low, predictable specificity so a small component can be adjusted without surprising unrelated pages. Save a backup before experimenting with overrides.
The supplied static checks confirm the stylesheet link and the presence of intended selectors and declarations. They do not calculate a browser's full cascade. The predictions in this lesson follow the explicitly limited rule context above; you should verify them in the browser as part of your own practice.
Worked practice
Worked exercise 1: Predict a winning color
Task: A notice paragraph is inside a card and the displayed stylesheet is the only author CSS. Worked answer: .card p supplies #174b36 because it has one class plus one type, compared with one class for .notice. The later .notice font-weight still makes the text bold because it sets a different property.
Worked exercise 2: Resolve an equal tie
Task: Add .notice { color: #112233; } followed by .notice { color: #445566; }, with no other matching color rules. Worked answer: #445566 wins because specificity and cascade context are equal, leaving source order as the tie-breaker. Reversing those two rules reverses this result.
Worked exercise 3: Explain inheritance
Task: Body has color: green, while a paragraph has color: navy. Worked answer: the paragraph is navy. Its own specified value is used instead of inheriting body's color. Other descendants without their own color may still inherit green, so one document can legitimately contain both colors.
Knowledge check
Question 1
What does .card.notice match? A) Notices anywhere inside cards. B) One element with both classes. C) All card and notice elements separately. D) Only a notice id.
Check question 1: answer and explanation
Answer: B. With no space, the class selectors apply to the same element.
Question 2
Under the lesson's equal cascade context, which is more specific? A) .card p. B) .notice. C) p. D) All are equal.
Check question 2: answer and explanation
Answer: A. One class and one type outranks one class alone or one type alone.
Question 3
When do equal-specificity rules use source order? A) Before importance is considered. B) Before origin is considered. C) When higher cascade priorities are tied. D) Only when they are inherited.
Check question 3: answer and explanation
Answer: C. Source order is a later tie-breaker, not the entire cascade.
Question 4
Which property normally inherits? A) margin. B) border-width. C) padding. D) color.
Check question 4: answer and explanation
Answer: D. Color commonly inherits; box dimensions and borders generally do not.
Question 5
What is the best first response to an unexpected style? A) Add !important everywhere. B) Inspect loading, matching rules, and computed values. C) Replace all classes with ids. D) Copy the rule into every element.
Check question 5: answer and explanation
Answer: B. Evidence identifies the actual conflict before increasing complexity.