CSS - Box Model

Overview

Estimated time: 10 minutes

The box model underpins layout. Mastering it helps you reason about spacing and sizing without surprises.

Learning Objectives

  • Explain content, padding, border, and margin.
  • Identify margin collapsing and how to avoid it.

Details & Examples

Every element is a rectangular box: content → padding → border → margin.

Padding adds space inside the border; margin adds space outside.

Margin Collapsing

Vertical margins between block elements may collapse into a single margin. Add padding/border to avoid when undesired.

Checks for Understanding

  1. Which part of the box model adds space inside vs outside the border?
Show answers
  1. Padding is inside; margin is outside.

Exercises

  1. Visually debug a layout using outlines to show box boundaries.
  2. Create a card with padding and margin that doesn’t collapse with adjacent elements.
Suggested answers
  1. *{ outline:1px dashed #64748b; outline-offset:-1px } (for debugging only)
  2. Add padding or border to prevent vertical margin collapse.