Tutorials CSS Box Model
Ch 10 / 60
Chapter 10 Beginner 9 min read CSS

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.

What you'll learn
The four layers: content, padding, border, margin
box-sizing: border-box vs content-box
Shorthand syntax for padding and margin
Margin collapse — what it is and why it happens
Block vs inline vs inline-block display
Inspecting the box model with DevTools

What 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:

Margin
Border
Padding
Content
(text, image, child elements)
Content — actual text / image
Padding — space inside border
Border — visible line
Margin — space outside border
Content
The innermost area where text, images, or child elements live. Its size is controlled by width and height (in content-box mode).
Padding
Transparent space between the content and the border. Padding has the element's background-color. Use it to give content breathing room inside a box.
Border
A visible (or zero-width invisible) line that wraps around the padding. Controlled with border-width, border-style and border-color.
Margin
Transparent space outside the border. Margin does not have the element's background-color — it is always transparent. Use it to push elements apart from their neighbours.

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.

CSS — width and height
.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.

padding: 20px;
All four sides = 20px (top, right, bottom, left)
padding: 10px 20px;
Top & bottom = 10px  |  Left & right = 20px
padding: 5px 10px 15px;
Top = 5px  |  Left & right = 10px  |  Bottom = 15px
padding: 5px 10px 15px 20px;
Top → Right → Bottom → Left (clockwise from top)
CSS — padding examples
/* 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.

CSS — border properties
/* 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.

margin: auto;
Horizontally centres a block element when it has a defined width
margin: 0 auto;
Top/bottom = 0 · Left/right = auto (most common centering trick)
margin-left: auto;
Pushes element to the right (used in flex/grid containers too)
margin: -10px;
Negative margins pull elements toward each other — useful for overlapping cards
CSS — margin examples
/* 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.

content-box (DEFAULT — problematic)
Content 300px
← total rendered: 340px →
padding: 20px border: 2px = +44px extra
border-box (RECOMMENDED ✓)
Content
← total rendered: 300px →
padding included border included stays 300px
CSS — box-sizing reset (use in every project)
/* ── 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 */
}
Add this to every project
The universal * { 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.

Box A — margin-bottom: 30px
margin-bottom: 30px
← Margins collapse: result = 30px (larger wins, not 30+20=50px) →
margin-top: 20px
Box B — margin-top: 20px
CSS — Margin collapse examples and fixes
/* 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 */
}
Only vertical margins collapse
Margin collapse only affects top and bottom margins. Left and right margins never collapse — they always add together. Flex and grid containers also prevent margin collapse among their children.

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
Live Demo — Block vs Inline vs Inline-block
display: block (divs take full width)
Block 1
Block 2 — each on its own line
display: inline (spans flow with text)
Normal text with Inline 1 and Inline 2 flowing in the text stream.
display: inline-block (can set width/height, flows inline)
80×48
120×48
60×48

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.

Content Area
Generated CSS:
.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.

Open DevTools
Press F12 (Windows/Linux) or Cmd+Option+I (Mac). Or right-click any element and choose Inspect.
Select an element
Click the cursor icon (top-left of DevTools) then click any element on the page. The HTML is highlighted in the Elements panel.
View the Box Model
In the Styles/Computed panel (right side), scroll down to find the box model diagram. It shows the exact pixel values for content, padding, border and margin as a nested diagram — exactly like the one at the top of this chapter.
Edit live
Click any value in the box model diagram or the Styles panel to edit it in real time. Changes apply instantly and reset on page reload — perfect for experimenting without touching your code.
DevTools — Box Model Inspector (mock-up)
margin
24
24
24
24
border
2
padding
16
260 × 40
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
One printable page covering every CSS3 property including all box model properties, shorthand values and layout reference.
Download PDF

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.

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js — all in one industry-ready curriculum. Build real projects with AI-powered tools and get placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5 CSS3 JavaScript React.js Node.js Tailwind CSS AI Tools

Quick Check — Chapter 10

Questions & Answers
From innermost to outermost:

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.
content-box (default): The 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.
Margin collapse occurs when two vertical margins of adjacent block elements touch. Instead of adding together, they collapse into a single margin equal to the larger of the two.

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 them

How to prevent it: use display: flex on the parent, or add overflow: hidden or a small padding to the parent.
Padding is the space inside the border, between the content and the border. It takes the element's background colour — clicking inside the padding area triggers mouse events on the element. It is part of the element's visible "body".

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; }
Multiple Choice Questions
Question 1 of 5
An element has width: 200px, padding: 20px, border: 5px solid. With the default content-box, what is the actual rendered width?
Question 2 of 5
Which CSS value should be applied universally to make width include padding and border?
Question 3 of 5
Box A has margin-bottom: 40px. Box B (below) has margin-top: 25px. What is the actual gap between them?
Question 4 of 5
What type of element does margin: 0 auto centre horizontally?
Question 5 of 5
Which display value allows setting width and height while keeping the element inline?
Chapter Summary
  • 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 padding and margin accept 1–4 values: all / top-bottom right-left / top right-left bottom / top right bottom left (clockwise).
  • box-sizing: border-box makes width and height include 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 auto horizontally centres a block element that has a defined width.
  • display: block takes full width and starts a new line. display: inline flows with text, cannot set width/height. display: inline-block flows inline but respects width/height.
  • Use browser DevTools (F12 → Elements → Computed) to inspect the exact box model values for any element.
~9 min read
Beginner level
Chapter 10 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel