Command Palette

Search for a command to run...

Hectal
Phase 5Beginner6 of 12 in CSS

The Box Model

Every element is a box: content, padding, border and margin. Learn box-sizing, margin collapsing, display, sizing, overflow, borders, shadows and backgrounds.

Everything the browser draws is a box. A heading, a paragraph, a button, even a single word inside a link: each one sits in a rectangle made of four layers, the content, the padding around it, the border around that, and the margin outside. Once you can see those boxes, CSS stops feeling like guesswork. Most "why is this 20 pixels too wide?" and "where did this gap come from?" questions are box model questions.

This phase goes layer by layer. You'll learn how width is really measured (box-sizing), the strange but logical ways margins behave (auto margins, negative margins and margin collapsing), what the display property changes, how sizes are worked out from content or from the outside (min-content, max-content, fit-content, aspect-ratio), what happens when content doesn't fit (overflow and scrolling), and how to decorate boxes with borders, rounded corners, outlines, shadows, backgrounds and gradients.

Every example is live and most print the real measured sizes to the console, so you can change a number and check the browser's maths yourself. Keep DevTools open as you go: the box model diagram in the Elements panel is the single most useful CSS debugging tool there is. Positioning and layout (Phase 6), Flexbox (Phase 7) and Grid (Phase 8) all build on what you learn here.

0/8 · 0%
8 topics ~3 h 24 code blocks & diagrams
Start with the first topic
1
5.1

The Box Model

Every element the browser draws is a rectangle made of four layers: the content, the padding around it, the border around that, and the margin outside it.

21 min 3 code practice

2
5.2

box-sizing

box-sizing decides what width and height measure: only the content (content-box, the default) or the whole box up to the border (border-box), which is why almost every site sets border-box everywhere.

19 min 3 code practice

3
5.3

Margins in Depth

Margins can be auto (to centre a block), negative (to pull boxes closer or overlap them), and vertical margins between blocks collapse into one, which explains most mystery gaps in CSS.

26 min 3 code practice

4
5.4

The display Property

display decides what kind of box an element makes: a block that takes a full row, an inline box that flows inside text, an inline-block mix of both, no box at all (none, contents), or a block that contains its own layout (flow-root).

22 min 3 code practice

5
5.5

Width, Height, Min and Max

A box's size comes either from outside (the space its parent gives it, or a width you set) or from inside (its content); min-*, max-*, the keywords min-content, max-content and fit-content, and aspect-ratio let you mix the two safely.

26 min 3 code practice

6
5.6

Overflow and Scrolling

When content is bigger than its box, overflow decides what happens: spill out (visible), cut off (hidden, clip), or scroll (scroll, auto); scrollbar-gutter and scroll-behavior polish how scrolling looks and feels.

21 min 3 code practice

7
5.7

Borders, Radius, Outlines and Shadows

Borders are part of the box and take up space; border-radius rounds the corners; outlines and box shadows are painted outside the layout and never move anything, which makes them ideal for focus rings and depth.

21 min 3 code practice

8
5.8

Backgrounds and Gradients

Every box can paint a background colour plus any number of image layers, which you size, position and repeat; gradients are images the browser draws for you, in straight lines, circles or around a point.

20 min 3 code practice

Practise this phase

Hands-on exercises: write the code, see your page live, and press Check to have it tested automatically.