CSS Box Model
Introduction
In CSS, every element is treated as a rectangular box. If spacing or layout looks wrong, the cause is often in margin, padding, border, or sizing rules. This chapter builds a practical box model mental model so you can control spacing confidently and debug layout issues quickly.
Prerequisites
- Basic CSS selectors and syntax
- Basic understanding of units (
px,rem,%) - A page connected to an external stylesheet
Every Element Is a Box
By default, the browser calculates an element as:
total size = margin + border + padding + contentCore layers from inside to outside:
- content: text or child elements
- padding: inner space around content
- border: line around padding/content
- margin: outer space between this box and others
Understanding which layer you are changing prevents most spacing mistakes.
Content Box
width and height apply to the content area by default.
.card {
width: 320px;
height: 160px;
}With default sizing behavior, padding and border are added on top of this width/height.
Padding: Inner Spacing
Padding creates breathing room inside the box.
.card {
padding: 1rem;
}Shorthand pattern:
padding: 16px;(all sides)padding: 12px 20px;(top/bottom, left/right)padding: 8px 12px 16px 12px;(top, right, bottom, left)
Padding increases clickable area and readability without changing content itself.
Border: Visual Edge
Border sits outside padding.
.card {
border: 1px solid #e2e8f0;
border-radius: 12px;
}Common uses:
- visual separation
- focus rings (with
outlinepatterns) - component boundaries
Thin borders (1px) are common; test contrast against backgrounds.
Margin: Outer Spacing
Margin creates space outside the element.
.card {
margin-bottom: 1rem;
}Use margin to separate neighboring components and vertical rhythm between blocks.
Practical rule:
paddingfor spacing inside a componentmarginfor spacing between components
Default box-sizing Behavior
Default is:
box-sizing: content-box;Meaning:
- declared width/height applies only to content
- padding and border increase final rendered size
Example:
.box {
width: 200px;
padding: 20px;
border: 2px solid #0f172a;
}Actual rendered width becomes:
200 + 20 + 20 + 2 + 2 = 244px
This surprises many beginners.
box-sizing: border-box (Recommended Default)
With border-box, width/height includes padding and border.
*,
*::before,
*::after {
box-sizing: border-box;
}Benefits:
- easier layout math
- fewer overflow surprises
- more predictable component sizing
In modern projects, global border-box is a standard baseline.
Tip
Set It Once Globally
Adding global border-box early prevents many layout bugs later in Flexbox and Grid chapters.
Width, Max-Width, and Responsive Boxes
Avoid rigid fixed widths for content-heavy components.
.container {
width: min(960px, 100% - 2rem);
margin-inline: auto;
}Or:
.card {
width: 100%;
max-width: 420px;
}This keeps components fluid on small screens while preserving comfortable limits on large screens.
Margin Collapse (Important Vertical Behavior)
Vertical margins between block elements can collapse into one margin instead of adding together.
Example:
- First element has
margin-bottom: 24px - Next element has
margin-top: 16px - Final gap may become
24px(not40px)
This behavior often appears with paragraphs, headings, and stacked sections.
Ways to avoid confusion:
- use padding on parent containers
- use flex/grid with
gap - control vertical rhythm consistently from one side
Centering with Margin Auto
A common pattern:
.wrapper {
width: min(900px, 100% - 2rem);
margin: 0 auto;
}auto horizontal margins center block elements when width is constrained.
Box Model in DevTools
DevTools usually shows a visual box model panel:
- content size
- padding values
- border widths
- margin values
When spacing looks wrong, inspect the element and confirm:
- is the gap from margin or padding?
- is border adding unexpected width?
- is
box-sizingwhat you expect?
This saves time compared to blind CSS edits.
Practical Example Component
.profile-card {
box-sizing: border-box;
width: min(420px, 100%);
padding: 1rem 1.25rem;
border: 1px solid #cbd5e1;
border-radius: 14px;
margin: 1rem auto;
background: #fff;
}This combines predictable sizing, comfortable internal spacing, and controlled outer separation.
Common Box Model Mistakes
| Mistake | Why it hurts | Better approach |
|---|---|---|
| Using only margin for all spacing | Hard-to-control component internals | Use padding inside, margin outside |
| Forgetting default content-box math | Unexpected overflow | Use global border-box |
| Fixed width without max-width | Breaks on small screens | Use fluid width constraints |
| Random mixed spacing values | Inconsistent rhythm | Define spacing scale tokens |
| Ignoring margin collapse | Unexpected vertical gaps | Use gap or single-direction spacing strategy |
Mini Exercise
- Create a card with
width,padding,border, andmargin - Switch between
content-boxandborder-box - Measure actual rendered width in DevTools
- Add two stacked sections and observe margin collapse
- Replace vertical margins with parent
display: grid; gap: ... - Compare readability and maintainability
FAQ
Why is my element wider than the width I set?
Because default content-box excludes padding and border from declared width.
Should I always use border-box?
For most modern projects, yes as a global default. It simplifies layout calculations significantly.
When should I use padding instead of margin?
Use padding for inner space inside a component; use margin for distance between sibling components.
Is margin collapse a bug?
No, it is defined CSS behavior for vertical margins in normal flow.
What is the fastest way to debug spacing issues?
Inspect the element in DevTools box model view and identify whether the gap is margin, padding, border, or size constraint.