CSS Box Model
Every HTML element is a rectangular box. The CSS Box Model explains exactly how that box is built — with four layers: content, padding, border, and margin. Understanding this one concept fixes most layout problems beginners face.
- What is the Box Model?
- The Four Layers
- Content
- Padding
- Border
- Margin
- Width Calculation
- box-sizing
- Browser DevTools
- Common Mistakes
- Try It
- FAQ
What is the CSS Box Model?
In CSS, every element on a webpage is treated as a rectangular box. This includes headings, paragraphs, images, divs, buttons — everything. The CSS Box Model is the set of rules that defines how these boxes are sized and how they relate to each other.
The box model has four layers, each adding space around the element's actual content. From the inside out: content → padding → border → margin. Understanding how these stack together lets you control spacing and layout precisely.
Everything is a Box
Every HTML element — headings, paragraphs, images, links — is rendered as a rectangular box by the browser.
Four Layers Stack Up
Content, padding, border, and margin all add to an element's total space — even if you don't set them all.
Width Is Tricky by Default
By default, width only sets the content area. Padding and border add on top — a common source of layout bugs.
DevTools Shows It Visually
Every browser's developer tools has a box model diagram that shows exact margin, border, padding, and content sizes.
The Four Layers
Each layer of the box model has a specific job. Here's how they work together:
Content
The actual content of the element — text, image, or nested elements. Controlled by width and height properties. This is the innermost box.
Padding
Transparent space between the content and the border. Padding inherits the element's background colour. Set with padding shorthand or individual sides.
Border
A line that goes around the padding and content. Has width, style, and color. Unlike padding, you can see the border and style it visually.
Margin
Transparent space outside the border, separating this element from neighbouring elements. Margin is always transparent — no background colour shows through.
Complete Box Model Example
Here's a <div> with all four box model layers applied. Look at the CSS carefully — then we'll calculate the total rendered width:
<!DOCTYPE html> <html lang="en"> <head> <style> div { width: 300px; /* content width */ padding: 20px; /* space inside border */ border: 5px solid black; /* visible border */ margin: 30px; /* space outside border */ background-color: lightblue; } </style> </head> <body> <div>This div uses the CSS Box Model.</div> </body> </html>
Now let's calculate the total horizontal space this element takes up on the page:
| Part | CSS Value | Width Added |
|---|---|---|
| Content | width: 300px | 300px |
| Padding | padding: 20px | 20px left + 20px right = 40px |
| Border | border: 5px | 5px left + 5px right = 10px |
| Margin | margin: 30px | 30px left + 30px right = 60px |
| Total width on page | 300 + 40 + 10 + 60 = 410px | |
Padding in Detail
Padding creates space between the content and the border. The background colour of the element fills the padding area, so it's not invisible space — it's coloured space. You can set each side individually or use the shorthand:
p { padding: 10px; /* all four sides = 10px */ padding: 10px 20px; /* top/bottom: 10px, left/right: 20px */ padding: 10px 20px 15px 5px; /* top right bottom left (clockwise) */ /* Or set sides individually: */ padding-top: 10px; padding-right: 20px; padding-bottom: 15px; padding-left: 5px; }
Padding vs Margin — The Key Difference
Padding is space inside the element's border. The element's background colour fills padding. Clicking inside the padding area still counts as clicking the element.
Margin is space outside the border. It is always transparent — no background colour. Margins separate the element from its neighbours. Clicking in a margin does not interact with the element.
Border in Detail
The border wraps around the padding and content. It has three properties — width, style, and color — which can be set in one shorthand or individually:
<style> .box1 { border: 2px solid #2563eb; padding: 12px; } .box2 { border: 4px dashed #e85d04; padding: 12px; } .box3 { border: 6px dotted #16a34a; padding: 12px; } .box4 { border-top: 3px solid #7c3aed; padding: 12px; } /* one side only */ </style> <div class="box1">Solid blue border</div> <div class="box2">Dashed orange border</div> <div class="box3">Dotted green border</div> <div class="box4">Top border only</div>
Margin in Detail
Margin is the transparent space outside the border that pushes neighbouring elements away. Like padding, it supports the same four-value shorthand. A very useful trick: margin: 0 auto centres a block element horizontally inside its parent.
.centred-box { width: 300px; margin: 0 auto; /* top/bottom: 0, left/right: auto → centres the box */ } .spaced-box { margin-top: 20px; margin-bottom: 20px; /* margin: 20px 0; does the same thing */ }
box-sizing: border-box
By default, when you set width: 300px on an element, that 300px applies to the content area only. Padding and border are added on top — so the rendered element is wider than 300px. This is called content-box sizing and causes endless confusion.
The fix is box-sizing: border-box. With this, the width you set becomes the total rendered width including padding and border — the content area shrinks to fit. Most professional developers apply this globally:
<!DOCTYPE html> <html lang="en"> <head> <style> /* Apply border-box to everything — professional standard */ * { box-sizing: border-box; } .default-box { width: 200px; padding: 20px; border: 5px solid #2563eb; background: #dbeafe; /* Actual rendered width = 200px (padding+border fit inside) */ } p { font-size: .85rem; margin-top: 6px; } </style> </head> <body> <div class="default-box">Width is exactly 200px</div> <p>With border-box: width 200px stays 200px total — padding and border fit inside.</p> </body> </html>
| Sizing Mode | width: 200px means | Rendered width (with 20px padding + 5px border) |
|---|---|---|
| content-box (default) | Content = 200px | 200 + 40 + 10 = 250px |
| border-box | Total box = 200px | 200px (content shrinks to 150px) |
Always Use This at the Top of Your CSS
Add this rule at the very top of every CSS file you write. It makes layouts predictable — the width you set is always the actual width:
Inspecting the Box Model with DevTools
Every modern browser has built-in developer tools that show a visual box model diagram for any element. This is one of the most useful tools for debugging layout issues.
Open DevTools
Press F12 (or Ctrl+Shift+I on Windows, Cmd+Option+I on Mac) to open browser developer tools.
Inspect an Element
Right-click any element on the page and choose "Inspect" to open DevTools with that element selected.
Find the Box Model Panel
In the Styles or Computed tab, scroll down to see the box model diagram showing exact pixel values for each layer.
Edit Live
Click any value in the box model diagram to edit it live. The page updates instantly — great for experimenting.
Common Mistakes
The most common box model mistake. You set width: 100% on an element with padding: 20px — and it overflows its parent. The fix: always add box-sizing: border-box so the width you set is the total width.
Adding margin when you want space inside the element (you want padding), or adding padding when you want space between elements (you want margin). Rule of thumb: padding is inside the border, margin is outside. The background colour fills padding — not margin.
If you give a heading margin-bottom: 30px and the paragraph below it margin-top: 20px, the gap between them is not 50px — it's 30px (the larger margin wins). This surprises beginners every time. Only vertical margins collapse; horizontal margins always add up normally.
Setting a fixed height on a container that holds text will cause content to overflow if the text grows. Let height be automatic (height: auto) and use min-height if you need a minimum size. Fixed heights are the second most common cause of overflow bugs.
CSS Box Model FAQ
The questions beginners ask most about the CSS Box Model: