The Box Model & the Cascade
Why every element is a box with content, padding, border, and margin, and how the cascade decides which of several competing styles actually wins.
The Box Model & the Cascade
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Every element renders as a rectangular box made of content, padding, border, and margin, in that order from the inside out.
- box-sizing: border-box makes width and height describe the whole visible box instead of just the content, which is why most modern stylesheets set it globally.
- When two rules target the same element, the cascade resolves the conflict using origin and importance first, then specificity, then source order as the final tiebreaker.
- Specificity is calculated per selector as a count of IDs, classes/attributes/pseudo-classes, and element names — it is not a single number you can eyeball from how a selector looks.
- Inheritance is a separate mechanism from the cascade: some properties (like color and font) pass from parent to child automatically unless a rule overrides them.
Every element is a box, whether you wanted it to be or not
Open the dev tools on almost any page, click an element, and you’ll see a
little diagram: a blue rectangle, surrounded by an orange band, surrounded by
a green band, surrounded by a purple band. That’s not a debugging feature —
it’s the actual model the browser uses to lay out every single element on
the page. Nothing in CSS escapes it. A <span>, a <div>, a <button> — all
of them are boxes with four layers, always in the same order:
- Content — the text or child elements themselves.
- Padding — transparent space inside the border, pushing the content away from the edge.
- Border — a visible (or invisible) line wrapping the padding.
- Margin — transparent space outside the border, pushing other elements away.
The reason this matters so much is that almost every layout bug a beginner hits — “why is there a gap I didn’t add,” “why did my button get wider than I set it,” “why do these two boxes not line up” — traces back to someone misjudging which of these four layers is responsible for the space they’re seeing. Once you can look at a layout and mentally separate padding from margin from border, half of CSS debugging stops being guesswork.
The box-sizing trap
Here’s where it gets genuinely confusing if nobody ever explains it to you.
By default, the CSS property width only sets the width of the content
box. Padding and border get added on top of that. So if you write:
.card {
width: 300px;
padding: 20px;
border: 2px solid black;
}
the element actually renders at 300 + 20 + 20 + 2 + 2 = 344px wide. That’s
not a bug — it’s the spec working exactly as designed (this default is
called content-box) — but it’s almost never what a developer actually
wants. You set width: 300px because you wanted a 300px box, not a 344px
one that happens to contain 300px of content.
The fix almost every real stylesheet uses is:
* {
box-sizing: border-box;
}
With border-box, width describes the entire rendered box, and padding
and border are subtracted from the content area instead of added to the
total. A 300px-wide card with 20px of padding still renders at 300px — the
content area just shrinks to make room. This one-line reset is so close to
universal that if you ever see a layout misbehaving in a way that looks like
“my padding is making things bigger than I expected,” check whether
box-sizing was set before you check anything else.
Margins collide; padding doesn’t
One more box-model quirk worth knowing early: vertical margins between
sibling block elements collapse — two stacked elements with margin-bottom: 20px and margin-top: 20px end up with a 20px gap between them, not 40px.
The browser takes the larger of the two, not the sum. This trips people up
constantly because it only happens with margin, only in the vertical
direction, and only under specific conditions (it doesn’t happen with
flexbox or grid children, for instance — one more reason those layout modes
feel more predictable than plain block flow).
When two rules disagree: the cascade
The box model explains how one element is built. The cascade explains what happens when more than one rule in your stylesheet tries to style the same element with conflicting values. This is unavoidable in any real project — you’ll have a base style for all buttons and a more specific style for buttons inside a warning banner, and they can’t both win.
The browser resolves this with a defined, ordered set of tiebreakers, checked in this sequence:
- Origin and importance. Author styles (yours) normally beat browser
defaults, but an
!importantflag overrides normal ordering entirely, which is exactly why relying on!importanttends to create more problems than it solves — it breaks the predictable order everything else depends on. - Specificity. If origin and importance are tied, the more specific selector wins.
- Source order. If specificity is also tied, whichever rule appears later in the stylesheet (or later in the cascade of loaded stylesheets) wins.
Most day-to-day CSS debugging lives in step 2, so it’s worth being precise about how specificity actually works.
Specificity is a count, not a vibe
A common misconception is that a longer selector is always more specific, or that specificity is some single intuitive number. It isn’t — it’s a structured count across three tiers:
- IDs (
#header) — the heaviest tier. - Classes, attribute selectors, and pseudo-classes (
.button,[type="text"],:hover) — the middle tier. - Element and pseudo-element selectors (
div,p,::before) — the lightest tier.
You compare selectors tier by tier, highest first. A single ID selector
(#nav) beats any number of class selectors stacked together
(.nav .list .item), because you never carry remainders down from a higher
tier to make up for a lower one. This is why “I added three more classes and
it still didn’t override the style” is such a common complaint — if the
competing rule has even one ID in its selector, no number of classes will
out-rank it. The practical lesson is to keep selectors as flat and low-tier
as possible (mostly single classes) so that overriding something later
doesn’t turn into a specificity arms race.
Inheritance: a separate, quieter mechanism
Inheritance often gets lumped in with the cascade, but it’s a distinct idea.
A handful of properties — color, font-family, font-size,
line-height, and a few others, mostly ones related to text — automatically
pass from a parent element down to its children if nothing more specific
sets them directly. That’s why setting font-family once on body styles
every piece of text on the page without you writing a rule for every tag.
Most properties, though — margin, padding, border, width, display,
and nearly everything layout-related — do not inherit. That’s deliberate:
if width inherited, every nested element would collapse to match its
parent’s exact size, which would make layout unworkable. Knowing which
handful of properties inherit (and that it’s a short, mostly typography-
related list) saves you from writing redundant rules, and from being
surprised when a layout property you expected to “flow down” simply doesn’t.
Check yourself
Why does setting box-sizing: border-box change what width: 300px means for an element?
Without it, width only sets the content area, so padding and border get added on top and the box ends up wider than 300px. With border-box, width is the final rendered width, and padding and border are carved out of that 300px instead of added to it.
If a div has both a class selector and an ID selector setting different colors, which one applies, and why?
The ID selector's rule applies, because an ID selector carries more specificity than a class selector regardless of which rule appears later in the stylesheet. Specificity is checked before source order.
Two rules have identical selectors, .card and .card, both setting background-color, in that order. Which one wins?
The second one. When specificity and origin are tied, the cascade falls back to source order, and the last rule declared wins.
Why does a paragraph nested inside a styled div usually pick up that div's font-family without any rule targeting the paragraph directly?
font-family is an inherited property, so it flows down from parent to child automatically. This is separate from the cascade's specificity rules — it only kicks in when nothing more specific overrides it.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.