Lesson 14 of 23

In progress

Course progress

HTML and CSS

The box model, without the guessing

Understand content, padding, border and margin, switch to border-box, and explain why two margins sometimes become one.

Every element is a rectangle made of four layers. Almost every "why is this 20 pixels wider than I asked for" question is answered by naming which layer the width applied to.

What you will learn

  • Name the four layers of a box and which one width affects.
  • Use box-sizing: border-box so sizes include padding and borders.
  • Predict when vertical margins collapse.

Four layers

From the inside out:

  1. Content — the text or image.
  2. Padding — space inside the border, part of the element’s background.
  3. Border — the visible edge.
  4. Margin — space outside the border, always transparent.
HTML
Editable. Runs in your browser.





Press Run to see the output.

By default, width sets the content box only. The element’s total width is 240 + 20 + 20 + 4 + 4 = 288 pixels. That is why a "240 pixel" box does not line up with a 240 pixel column.

border-box makes the number honest

HTML
Editable. Runs in your browser.





Press Run to see the output.

With border-box, the declared width includes padding and border. The content area shrinks instead. This is the near-universal reset because it makes layout arithmetic match the number you typed.

The universal selector applies it to pseudo-elements too, which prevents a stray ::before from using the old model.

Height, min-height and overflow

Setting a fixed height on a text container is risky: content grows when the font size increases or the text changes, and the overflow is either clipped or spills.

Prefer min-height so the box can grow. If content must scroll, say so explicitly with overflow: auto, and test with a keyboard to make sure the scroll area is reachable.

HTML
Editable. Runs in your browser.





Press Run to see the output.

Margin collapse

Vertical margins between block elements in normal flow collapse: the larger margin wins instead of adding. A heading with margin-bottom: 24px followed by a paragraph with margin-top: 16px produces 24 pixels, not 40.

HTML
Editable. Runs in your browser.





Press Run to see the output.

Margin collapse does not happen inside Flexbox or Grid containers, which is one reason those layouts feel more predictable. It also does not happen when a border or padding separates the margins.

aspect-ratio is the modern way to reserve a shape without a fixed height: aspect-ratio: 16 / 9 keeps a video or image box proportional while the width changes, and the browser can reserve the space before the media loads.

Modern CSS gives you margin-block and margin-inline, which map to top/bottom and left/right in horizontal writing. They are shorter and adapt to vertical writing modes.

display changes the box

block elements take the full width and start on a new line. inline elements sit in the text flow and ignore width and vertical margin. inline-block sits in the flow but accepts width and height.

HTML
Editable. Runs in your browser.





Press Run to see the output.

Outlines are drawn outside the box and do not affect layout, which is exactly why focus rings use them instead of borders. An inline element with width: 200px will not become 200 pixels wide; that is the single most common display confusion. Change it to inline-block or block when you need dimensions.

Adding width: 100% and padding to an element without border-box makes it wider than its container and causes horizontal scrolling. Either switch to border-box globally or use max-width: 100% and avoid fixed widths on fluid layouts.

Create a card exactly 320 pixels wide including 24 pixels of padding and a 2 pixel border, using border-box. Add a second card with the default box model and compare their total widths in the Elements panel.

Show solution
HTML
Editable. Runs in your browser.





Press Run to see the output.

The first card’s content area is 320 minus 48 padding minus 4 border, which is 268 pixels. The second card’s content is 320 pixels, so its total is 372.

Recap

  • Width applies to the content box by default; padding and border add to it.
  • box-sizing: border-box makes declared sizes include padding and border.
  • Adjacent vertical margins collapse, and Flexbox or Grid containers prevent it.

Next, you will turn spacing into a small system instead of a pile of arbitrary numbers.

Should I reset margins to zero?
A common approach is to zero margins and then set a deliberate spacing scale. Removing them without replacing them produces cramped text, so replace rather than delete.
Why does padding not push content away from a sibling?
Padding is inside the element. Only margin creates space outside the border, which is why the two are not interchangeable.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.