CSS Box Model
Every single HTML element — from a heading to a tiny icon — is a rectangular box with four invisible layers: content, padding, border and margin. Understanding the box model is the single most important concept in CSS layout. Master it here.
box-sizing: border-box vs content-boxWhat is the CSS Box Model?
The CSS Box Model describes the rectangular box that every HTML element occupies on a page. It has four distinct areas, stacked from the inside out:
(text, image, child elements)
width and height
(in content-box mode).background-color. Use it to give content breathing room
inside a box.border-width, border-style and border-color.The Content Area — width & height
The content area is sized with width and height. By default (in content-box mode), padding and border are added on top of these
dimensions — making the total element size larger than the declared width.
.box {
width: 300px; /* content area width */
height: 150px; /* content area height */
min-width: 200px; /* cannot shrink below this */
max-width: 100%; /* never wider than parent */
min-height: 80px; /* at least 80px tall */
max-height: 400px; /* cap height; overflow scrolls */
}
/* Percentage widths are relative to the PARENT's width */
.half { width: 50%; }
.full { width: 100%; }
/* Fluid: fill available space */
.fluid { width: auto; } /* default for block elements */
Padding — Space Inside the Border
padding adds space between the content and the
border. The background colour fills the padding area. Padding cannot be negative.
/* Individual sides */
.card {
padding-top: 16px;
padding-right: 24px;
padding-bottom: 16px;
padding-left: 24px;
}
/* Equivalent shorthand — top/bottom right/left */
.card { padding: 16px 24px; }
/* Percentage padding — relative to parent WIDTH (not height!) */
.aspect-16-9 {
width: 100%;
padding-bottom: 56.25%; /* 9/16 = 56.25% — classic aspect ratio trick */
position: relative;
height: 0;
}
Border — The Visible Edge
The border property wraps around the padding. It
has three sub-properties: border-width, border-style and border-color.
/* Shorthand: width style color */
.card { border: 1px solid #e5e7eb; }
.error{ border: 2px solid #ef4444; }
.dashed{ border: 2px dashed #264de4; }
/* Individual sides */
.bottom-only { border-bottom: 2px solid #264de4; }
.left-accent { border-left: 4px solid #264de4; }
/* Border styles */
/* solid | dashed | dotted | double | groove | ridge | inset | outset | none */
/* Rounded corners */
.pill { border-radius: 999px; } /* fully rounded */
.card { border-radius: 12px; } /* rounded card */
.circle { border-radius: 50%; } /* circle (if width==height) */
/* Remove border */
.no-border { border: none; }
Margin — Space Outside the Border
margin pushes elements apart from each other.
Unlike padding, margin is always transparent — it never shows the element's background colour. Margin
accepts the same one-to-four-value shorthand as padding, plus the special value auto.
/* Centre a block element */
.container {
width: 960px;
max-width: 100%;
margin: 0 auto; /* horizontal centre */
}
/* Vertical rhythm — consistent spacing between sections */
section { margin-bottom: 48px; }
h2 { margin-bottom: 16px; }
p { margin-bottom: 12px; }
/* Remove margin */
h1 { margin: 0; }
/* Push the last item in a flex row to the right */
.nav { display: flex; }
.nav-cta { margin-left: auto; } /* pushes CTA to far right */
box-sizing — The Most Important Reset
box-sizing controls whether width and height include or exclude the
padding and border. It is the single most confusing part of the box model — get this right first.
/* ── Universal border-box reset ── */
*,
*::before,
*::after {
box-sizing: border-box;
}
/*
content-box (DEFAULT):
width: 300px + padding: 20px + border: 2px
→ total rendered width = 300 + 40 + 4 = 344px ← WIDER THAN DECLARED
border-box (RECOMMENDED):
width: 300px includes padding AND border
→ total rendered width = 300px ← EXACTLY WHAT YOU DECLARED
*/
.content-box-example {
box-sizing: content-box; /* browser default */
width: 300px;
padding: 20px;
border: 2px solid;
/* actual rendered width: 344px */
}
.border-box-example {
box-sizing: border-box; /* ← use this */
width: 300px;
padding: 20px;
border: 2px solid;
/* actual rendered width: 300px */
}
* { box-sizing: border-box; } reset is the first
rule in almost every professional CSS file. It prevents hundreds of confusing layout bugs where
elements are slightly wider than expected. Frameworks like Tailwind CSS, Bootstrap and Normalize.css all
include it.
Margin Collapse — The Tricky Part
When two vertical margins touch, CSS collapses them into a single margin equal to the larger of the two — they do not add together. This is one of the most common sources of confusion for beginners.
/* Both elements have margins — they COLLAPSE, not add */
h2 { margin-bottom: 30px; }
p { margin-top: 20px; }
/* The gap between h2 and p = 30px (NOT 50px!) */
/* ── When does collapse NOT happen? ── */
/* 1. Horizontal margins never collapse */
/* 2. Flex/grid children do not collapse */
/* 3. Elements with padding, border, overflow: hidden don't collapse */
/* ── Fix options ── */
.parent {
overflow: hidden; /* prevents child margin escaping parent */
/* OR */
display: flex; /* flex children never collapse */
/* OR */
padding-top: 1px; /* any padding stops collapse */
}
Block, Inline & Inline-block
The display property changes how the box model
behaves. It determines whether an element takes up the full width, flows with text, or acts as a hybrid.
| display | Full width? | New line? | width/height work? | Examples |
|---|---|---|---|---|
| block | ✓ Yes | ✓ Yes | ✓ Yes | div, p, h1, section |
| inline | ✗ Content only | ✗ No | ✗ No | span, a, strong, em |
| inline-block | ✗ Content only | ✗ No | ✓ Yes | img, button, input |
| none | Element is completely removed from layout and not rendered | script, hidden elements |
||
Interactive Box Model Playground
Drag the sliders to adjust each layer of the box model and watch the element update in real time. The CSS output updates automatically.
.element {
padding: 16px;
border: 2px solid #264de4;
margin: 24px;
border-radius: 8px;
box-sizing: border-box;
}
Inspecting the Box Model with DevTools
Every browser's Developer Tools includes a visual box model inspector. It shows the exact computed size of each layer in real time — essential for debugging layout issues.
F12 (Windows/Linux) or Cmd+Option+I (Mac). Or right-click any element and choose
Inspect.
Try It Yourself
This demo shows three boxes — two with content-box
(default) and two with border-box — at the same declared width. Watch
how their actual rendered sizes differ.
Quick Check — Chapter 10
1. Content — the actual text, image or child elements. Sized by
width and height.2. Padding — transparent space between the content and the border. The element's
background-color fills this area. Cannot be negative.3. Border — a visible (or zero-width invisible) line around the padding. Controlled with
border-width, border-style
and border-color.4. Margin — transparent space outside the border that separates the element from neighbouring elements. Always transparent regardless of the element's background. Can be negative to create overlap.
width and height apply only to the content area. Padding and border are added
on top. A box declared at 300px with 20px padding and 2px border is actually rendered at 344px
wide — wider than declared.border-box (recommended): The
width and height include the padding and border. The same box at 300px stays
exactly 300px wide — padding and border are carved out of the declared width.Best practice: add
*, *::before, *::after { box-sizing: border-box; } at the top of every
stylesheet. This prevents hundreds of layout bugs.
Example: if a
h2 has margin-bottom: 30px and the following p has margin-top: 20px, the gap
between them is 30px — not 50px.Margin collapse does NOT happen when:
— Horizontal (left/right) margins
— Flex or grid container children
— Elements with
overflow: hidden or any padding/border between
themHow to prevent it: use
display: flex on the parent,
or add overflow: hidden or a small padding to the parent.
Margin is the space outside the border. It is always transparent — it never shows the element's background. It creates separation between elements. Mouse events do not trigger in the margin area.
Quick rule: padding = breathing room inside the box. margin = gap between boxes.
margin: 0 auto sets top and bottom margin to 0 and left and right
margin to auto. When margin-left
and margin-right are both auto,
the browser divides the remaining horizontal space equally between them — which centres the
element.Requirements for it to work:
1. The element must be a
display: block element (not inline).2. The element must have an explicit
width (or max-width). Without a width, a block element takes up 100% of its parent
anyway — there's nothing to centre.Example:
.container { width: 960px; max-width: 100%; margin: 0 auto; }
width: 200px, padding: 20px, border: 5px solid.
With the default content-box, what is the actual rendered width?
width include padding and border?margin-bottom: 40px. Box B (below)
has margin-top: 25px. What is the actual gap between them?margin: 0 auto
centre horizontally?display value allows setting width and height while keeping the
element inline?- Every HTML element is a rectangular box with four layers from inside out: Content → Padding → Border → Margin.
- Padding is inside the border and carries the background colour. Margin is outside the border and is always transparent.
- Both
paddingandmarginaccept 1–4 values:all/top-bottom right-left/top right-left bottom/top right bottom left(clockwise). box-sizing: border-boxmakeswidthandheightinclude padding and border — much more predictable. Apply with* { box-sizing: border-box; }in every project.- Margin collapse: touching vertical margins combine into the larger value — they do NOT add. Horizontal margins never collapse. Flex/grid children never collapse.
margin: 0 autohorizontally centres a block element that has a defined width.display: blocktakes full width and starts a new line.display: inlineflows with text, cannot set width/height.display: inline-blockflows inline but respects width/height.- Use browser DevTools (F12 → Elements → Computed) to inspect the exact box model values for any element.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.