Lesson 12 of 23
In progress
CSS selectors and the cascade
Target elements with classes and structure, understand specificity and inheritance, and stop fighting your own styles with !important.
A stylesheet is a set of rules, and a rule only applies when its selector matches. Most "my CSS is not working" problems are really "my selector does not match what I think it matches" or "another rule is more specific".
What you will learn
- Write selectors based on classes, elements and structure.
- Predict which rule wins when two of them match.
- Use inheritance deliberately and avoid
!important.
The selectors you will use daily
Editable. Runs in your browser.
<p id="intro">A paragraph with an id.</p>
<p class="note">A paragraph with a class.</p>
<div class="card">
<p>Direct child paragraph.</p>
<section><p>Nested paragraph, not a direct child.</p></section>
</div>
Press Run to see the output.
p matches every paragraph. .note matches any element with that class. #intro matches the element with that id, which must be unique. .card p matches any paragraph inside a card; .card > p only matches direct children.
Class selectors are the workhorse. They are reusable, they do not depend on document structure, and changing a class in the HTML is a deliberate act. Ids are for linking and for JavaScript hooks, not for styling, because an id cannot be reused.
Structure and state
Pseudo-classes match a state or a position: :hover, :focus-visible, :first-child, :nth-child(2n), :checked, :disabled.
Editable. Runs in your browser.
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
<p><a href="#">Hover this link</a></p>
<p><input value="Focus me with Tab"></p>
Press Run to see the output.
:focus-visible is the important one. It shows a focus ring for keyboard navigation without flashing one after every mouse click, which is why removing outlines entirely is never the right fix.
Pseudo-elements use ::, as in ::before, ::after, ::first-line. A common mistake is writing :before with one colon; it works in browsers for legacy reasons and is not valid modern CSS.
Specificity, briefly
When several rules match, the browser compares specificity: ids beat classes, classes beat elements. If specificity ties, the later rule wins. Inline styles beat all of them.
p { color: black; } /* 0,0,1 */
.note { color: navy; } /* 0,1,0 */
.card .note { color: purple; } /* 0,2,0 */
Rather than counting, keep selectors flat. One class per rule is easy to reason about and easy to override. Long descendant chains create fragile, highly specific rules that force you to add more specificity later.
A useful escape hatch is :where(), which contributes zero specificity, so a reset written with it can always be overridden by a normal class. !important wins over everything and is almost always a sign that the cascade was not understood. It is legitimate in a few cases, such as a utility that must override a third-party widget, but it should be rare and commented.
Inheritance
Some properties inherit from parent to child: colour, font family, font size, line height. Others do not: margin, padding, border, background.
Editable. Runs in your browser.
<p>Normal text inherits colour and font from the body.</p>
<p class="muted">Muted text overrides only the colour.</p>
Press Run to see the output.
This is why setting typography on body is effective: everything inherits it until a component overrides it. Buttons and inputs do not inherit fonts by default, which is why forms often look inconsistent until you set font: inherit on them.
Naming that survives a redesign
Name classes for meaning, not appearance. .warning survives a colour change; .red-text does not. A light convention such as .card, .card__title, .card--featured keeps related styles discoverable without a framework.
Using an id for styling creates a rule that is hard to override and impossible to reuse. Two elements cannot share an id, so the style applies once. Use a class for anything that might appear more than once, which is almost everything.
Write CSS for a card containing a heading and a paragraph. Give the card a class, style the heading with a descendant selector, add a hover state to the card, and make the first paragraph italic using a structural pseudo-class.
Show solution
Editable. Runs in your browser.
<article class="card">
<h2>A card</h2>
<p>This paragraph is first, so it is italic.</p>
<p>This one is not.</p>
</article>
Press Run to see the output.
The card’s own look lives in .card, the heading adjustment in a descendant selector, and the state in :hover. Each rule is flat enough to override later.
Recap
- Classes are the default selector; ids are unique and should not carry styles.
- Specificity decides conflicts, but flat selectors avoid the fight;
!importantis not a tool for routine fixes. - Inheritance carries typography down the tree, and
:focus-visiblekeeps keyboard focus visible.
Next, you will choose colour and type that stay readable in both themes.
What is the difference between : and ::?
Should I use CSS nesting?
Progress is saved in this browser only. No account, no email.