CSS Essentials·Foundations

Selectors and the cascade

How CSS decides which rule wins, and how to write selectors that stay maintainable.

Beginner 12 min read Lesson 1 of 480 Challenge included
What you will be able to do
  • Connect a stylesheet
  • Use class and element selectors
  • Predict which declaration wins

CSS is a list of rules. Each rule selects elements and declares how they should look. The browser combines all matching rules using the cascade.

styles.cssCSS
/* element selector */
h1 {
  color: #17130f;
}

/* class selector */
.lede {
  font-size: 1.125rem;
  color: #5f564c;
}

/* state selector */
a:hover {
  text-decoration-thickness: 2px;
}
PrioritySource
1 (highest)!important declarations (avoid)
2Inline style attribute
3ID selectors
4Classes, attributes, pseudo-classes
5Elements and pseudo-elements
Your turn

Style the callout

Easy

Write a class selector .callout that adds a light background, 16px padding and a 12px border radius.

CSS · starter
/* your rule here */
Check your understanding before moving on.
Knowledge checkQuestion 1

Which selector has the highest specificity?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: The box model
Lesson 1 of 480 · 0% of course