Selectors and the cascade
How CSS decides which rule wins, and how to write selectors that stay maintainable.
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.
/* element selector */
h1 {
color: #17130f;
}
/* class selector */
.lede {
font-size: 1.125rem;
color: #5f564c;
}
/* state selector */
a:hover {
text-decoration-thickness: 2px;
}| Priority | Source |
|---|---|
| 1 (highest) | !important declarations (avoid) |
| 2 | Inline style attribute |
| 3 | ID selectors |
| 4 | Classes, attributes, pseudo-classes |
| 5 | Elements and pseudo-elements |
Your turn
EasyStyle the callout
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 inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.