Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 minLESSON 15 / 16

CSS architecture

You already know how the cascade picks a winner. At a larger page that contest happens by accident, because every rule lives in one pile and any of them can touch any element. This lesson is how to keep the pile readable.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

The heading class is card__title, and the heading also has an id. The id color wins for the reason you learned with specificity. The class is the name you can reuse without that fight.

+Go deeper

Global rules collide

A rule on h1 styles every heading. A rule on a class styles only the elements that opted in. Once the page has many regions, the element rule becomes a surprise in a place you were not looking. The is doing what you asked. The ask was too wide.

Names should say the job

Once several people share a stylesheet, two of them both reach for .card and one silently wins. A naming scheme such as BEM keeps the relationship in the name: .card is the block, .card__title is a piece of that block, and .card--featured is a variation. You can find the rule without guessing which .card was meant. A utility-first set of classes, the kind this site uses for spacing and color, avoids invented names by keeping each class to one declaration. Either approach can work. The useful part is seeing why an unscoped name collides. Layout you already know, such as a flex row or a grid template, belongs in a name for that region, not in a rule that styles every div.

Do not win with an id

still works the way the cascade lesson said. An id on a title beats every class you add later. You then reach for !important, and the next change needs another one. Drop the id hook. Let the class be the contract.

Rules that stay findable
ApproachWhat it stylesWhat goes wrong
h1 or divEvery element of that typeA later region inherits a surprise
.cardElements that opted inYou can find the name in the HTML
#titleOne element, very heavilyClasses can no longer override it
!importantWhatever you were fightingThe next change needs another one

One job per rule

A rule that sets color, the flex direction, the grid tracks, and a transition is four reasons to edit one block. Split the color from the layout. The , the flex or grid arrangement, and the motion can each have a name. A should add the wider arrangement, not repeat the entire rule.

What scale actually breaks

The file is too big when you cannot predict which rule wins without the browser tools. Crossed-out declarations are the symptom. The fix is a narrower selector and a name, not a heavier one.

04

Guided challenge

CSS architecture

The heading has id title and class card__title. The script looks for a class that is not there, so the read fails. Query the id and log the class, the id, and the text.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

card__title
title
Recipe card

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.

    01Why avoid styling every h1 from one rule on a large page?
    02What makes an id a poor everyday hook?

    Ready to call this one understood?

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

    Sign in to save progress
    Next lesson