Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate18 minLESSON 07 / 16

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.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

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.

What a selector matches
SelectorIt matches
*Every element
divEvery div
div pEvery p anywhere inside a div
div > pEvery p that is a direct child of a div
.cardEvery element with that class
#titleThe one element with that id
a:hoverA link while the pointer is over it
input:focusAn input while it is focused
p:first-childA p that is the first child of its parent
p::first-letterOnly the first letter of a paragraph
li::markerThe 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.

04

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.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

h1
card
title
#9a3412

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Edit the HTML and CSS. The JavaScript tab can read the result. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Which selector beats a class?
    02What does a later rule win?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson