Lesson 12 of 23

In progress

Course progress

HTML and CSS

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

HTML
Editable. Runs in your browser.





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.

HTML
Editable. Runs in your browser.





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.

HTML
Editable. Runs in your browser.





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
HTML
Editable. Runs in your browser.





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; !important is not a tool for routine fixes.
  • Inheritance carries typography down the tree, and :focus-visible keeps keyboard focus visible.

Next, you will choose colour and type that stay readable in both themes.

What is the difference between : and ::?
A single colon is a pseudo-class, matching a state such as hover. A double colon is a pseudo-element, creating a part such as ::before. Both are written with one colon in old code.
Should I use CSS nesting?
It is supported in current browsers and can reduce repetition, but it also makes specificity harder to see. Start with flat selectors and adopt nesting only where it clearly helps.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.