Cascade and specificity
A stylesheet is a list of declarations. When two of them set the same property, one wins and the other is ignored. This lesson is that contest, using color so the later layout lessons can assume you already know who wins.
Your console output appears here.
The element is an h1 with class card and id title. The inline style carries the color that also wins in the stylesheet, because the id beats the class and the class beats the element.
+Go deeper
Where declarations come from
A rule has a selector and a block of declarations, each one a property and a value. The selector chooses the elements. CSS can sit in three places: an inline style attribute, a style element in the head, or a linked stylesheet. The linked file is the usual choice, because one file can cover the site and the HTML stays the structure. The browser also has its own rules, such as a heading that is large and bold. Your stylesheet is the author origin. An inline style is also author origin, and it is more specific than a selector in the file. The sorts these before it looks at the selector text.
Specificity is a count
counts ids, then classes, attributes, and pseudo-classes, then elements. One id outweighs any number of classes. A class outweighs an element selector. The numbers are compared from the left, not added into one pile. In the sandbox, #title beats .card, and .card beats h1. Among equal weights, the later rule wins. That is why a declaration can look correct and still do nothing: a heavier selector elsewhere already won.
| Selector | It matches |
|---|---|
| * | Every element |
| div | Every div |
| div p | Every p anywhere inside a div |
| div > p | Every p that is a direct child of a div |
| .card | Every element with that class |
| #title | The one element with that id |
| a:hover | A link while the pointer is over it |
| input:focus | An input while it is focused |
| p:first-child | A p that is the first child of its parent |
| p::first-letter | Only the first letter of a paragraph |
| li::marker | The bullet or number in front of a list item |
Source order breaks a tie
If specificity is equal, the one that appears later wins. Appending a rule to the bottom of the file only helps when it is at least as specific as the rule you are trying to replace. A later h1 color does not beat an earlier #title color. The list marker from the document-structure lesson is the li::marker row above, so its color can change without a background image. An ordered list also keeps a count named list-item, which is what lets a stylesheet print 01, 02, 03 instead of typing those numbers into the HTML.
Importance is a trap
!important jumps the declaration into a stronger importance bucket, which is considered before specificity. It is hard to undo without another !important. Prefer a clearer selector.
The crossed-out rule lost
In the browser tools, a crossed-out color lost. Editing it does nothing while the winner still matches. The sandbox stylesheet shows the three rules. The inline style matches the winner so a script can read that value back.
Guided challenge
Cascade and specificity
The element is an h1. The script logs h2. Read tagName from the heading so the first log matches the HTML.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
h1 card title #9a3412
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Edit the HTML and CSS. The JavaScript tab can read the result. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.