CSS Margin
You can paint a box, draw its border, and round its corners. Next skill: push boxes apart. margin is the outer gap around an element — the breathing room outside the border.
What is margin?
Margin is empty space around the outside of an element’s border. It separates one box from its neighbors. It does not paint the background of the element into that gap — that gap is outside the filled area.
Margin vs padding (quick contrast)
Padding (next lesson) is inside the border — between content and edge. Margin is outside the border — between this box and the next. If you only remember one line today: margin = outer, padding = inner.
Shorthand: how many values?
You can pass one, two, three, or four lengths. Sides follow a fixed order.
📊 Margin shorthand value counts
| Values | Meaning | Everyday note |
|---|---|---|
| 1 | All four sides the same | Even breathing room around a card |
| 2 | Top+bottom, then left+right | Vertical and horizontal pairs |
| 3 | Top, left+right, bottom | Less common — know it exists |
| 4 | Top, right, bottom, left | Full control, clockwise from top |
Even space on every side
Start with one length for all sides.
Same margin on all sides
css.card {
margin: 16px;
border: 1px solid #cbd5e1;
padding: 1rem;
background-color: #ffffff;
}
Two values — vertical, then horizontal
Two lengths pair opposite sides: first is top and bottom; second is left and right.
Vertical then horizontal margin
css.panel {
margin: 24px 12px;
border: 1px solid #94a3b8;
padding: 1rem;
background-color: #f8fafc;
}
Four values — clockwise from the top
When each side needs its own gap, list top → right → bottom → left.
Four-sided margin shorthand
css.note {
margin: 8px 20px 32px 20px;
border: 1px solid #0f766e;
padding: 0.75rem;
background-color: #ecfdf5;
}
Center a block with auto
For a block-level box with a set width, margin-left: auto and margin-right: auto (often written margin: 0 auto) push equal leftover space to both sides — a classic way to center the box horizontally.
Horizontal centering with auto
css.box {
width: 280px;
margin: 0 auto;
border: 2px solid #0f766e;
padding: 1rem;
background-color: #f0fdfa;
}
Vertical margins can collapse
When two block elements stack, the bottom margin of the first and the top margin of the second often collapse into a single gap — usually the larger of the two, not their sum. This surprises beginners; expect it between stacked paragraphs and sections.
Stacked blocks with collapsing margins
css.block-a {
margin-bottom: 24px;
background-color: #ccfbf1;
padding: 0.5rem;
}
.block-b {
margin-top: 24px;
background-color: #99f6e4;
padding: 0.5rem;
}
/* Between them you often see about 24px, not 48px */
One side at a time
Longhands like margin-top are perfect when you only need space above a heading or below a button.
Top margin only
css.section-title {
margin-top: 2rem;
margin-bottom: 0.5rem;
margin-left: 0;
margin-right: 0;
}
Try it yourself
- Give two cards
margin: 16pxand watch the outer gap. - Rewrite one card as
margin: 24px 12pxand notice vertical vs horizontal. - Center a 280px-wide box with
margin: 0 auto. - Stack two colored blocks with
margin-bottomandmargin-topboth24px— measure whether the gap looks like 24px or 48px. - Space a heading with
margin-toponly.
Common mistakes
- Using margin when you meant padding (inner cushion for text)
- Expecting
margin: autoto center every flex/grid item the same way as a simple block (layout context matters) - Adding top and bottom margins and assuming they always add up (collapse may shrink the gap)
- Huge margins everywhere instead of a small, consistent scale
- Mixing up the four-value order (top, right, bottom, left)
Summary
- Margin is outer space outside the border
- Shorthand: 1, 2, or 4 values (clockwise from top for four)
margin: 0 autocenters a sized block horizontally- Vertical margins between stacked blocks can collapse
- Use per-side longhands when only one edge needs space
- Padding is the inner cousin — next lesson
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top