Colors & the Box Model Lesson 8 of 33
CSS Margin and Padding — Spacing Made Simple
Understand the difference between padding (space inside) and margin (space outside), use the 1-, 2-, 3- and 4-value shorthands, and center a box with margin auto.
Good design needs breathing room. Text crammed against the edge of a box, or sections touching each other, look messy and are hard to read. CSS gives you two kinds of space:
- Padding — space inside the element, between its content and its border
- Margin — space outside the element, between its border and other elements
Think of a framed picture on a wall. The padding is the white mat between the photo and the frame. The margin is the empty wall between this frame and the next one.
<style>
.box {
background: #e3f1ec;
border: 3px solid #0f5c4d;
}
.padded { padding: 24px; }
.margined { margin: 24px; }
</style>
<div class="box">No padding, no margin. The text touches the border.</div>
<div class="box padded">padding: 24px — space inside, so the background grows.</div>
<div class="box margined">margin: 24px — space outside, so the box moves away from its neighbors.</div>
Notice: padding is inside the background color (the green area gets bigger), while margin is outside (it's always transparent).
Setting each side
Each side can be set separately:
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
margin-top: 0;
margin-bottom: 30px;
The shorthand: 1, 2, 3 or 4 values
Writing four lines is tiring, so padding and margin accept a shorthand. The pattern is the same for both:
| You write | Meaning |
|---|---|
padding: 10px; | All four sides 10px |
padding: 10px 20px; | Top & bottom 10px, left & right 20px |
padding: 10px 20px 30px; | Top 10px, left & right 20px, bottom 30px |
padding: 10px 20px 30px 40px; | Top, right, bottom, left (clockwise) |
<style>
.btn {
display: inline-block;
background: #0f5c4d;
color: white;
border-radius: 8px;
margin: 6px;
font-family: Arial, sans-serif;
}
</style>
<span class="btn" style="padding: 4px;">padding: 4px</span>
<span class="btn" style="padding: 10px 24px;">padding: 10px 24px</span>
<span class="btn" style="padding: 16px 40px;">padding: 16px 40px</span>
Buttons usually have more padding on the sides than on the top and bottom. padding: 10px 24px is a classic button size.
Centering a box with margin: auto
When a block element has a set width (or max-width), margin: 0 auto centers it horizontally. "auto" means "split the leftover space equally between left and right."
<style>
.container {
max-width: 300px;
margin: 0 auto;
background: #fdf3dc;
padding: 16px;
border: 2px solid #f2b33d;
}
</style>
<div class="container">I'm centered! Most websites put their main content in a centered container like this.</div>
Margin collapse
Here's something surprising: when two block elements are stacked, their top and bottom margins don't add up. The bigger one wins. A paragraph with margin-bottom: 20px above one with margin-top: 30px leaves 30px between them, not 50px. This is called margin collapse.
<style>
.a { margin-bottom: 20px; background: #e3f1ec; }
.b { margin-top: 30px; background: #fbe9e6; }
</style>
<p class="a">margin-bottom: 20px</p>
<p class="b">margin-top: 30px — the gap is 30px, not 50px</p>
It only happens with vertical margins (top and bottom), not left and right, and not inside Flexbox or Grid layouts. If your spacing looks smaller than expected, this is probably why.
Negative margins
Margins can be negative, which pulls an element toward its neighbor or even makes it overlap. Padding can never be negative. Negative margins are an advanced trick, so use them sparingly.
Default margins
Browsers give many elements margins by default: body has about 8px, and headings, paragraphs and lists have top and bottom margins. That's why there's space around your page even with no CSS. Many developers start their CSS by setting body { margin: 0; } to control the spacing themselves.
Practice: a product card
<style>
.card {
max-width: 260px;
margin: 10px auto;
padding: 20px;
border: 1px solid #e3e0d6;
border-radius: 12px;
font-family: Arial, sans-serif;
}
.card h3 { margin: 0 0 6px; }
.card p { margin: 0 0 16px; color: #5b6662; }
.card .price { font-size: 22px; font-weight: bold; color: #0f5c4d; margin-bottom: 0; }
</style>
<div class="card">
<h3>Kapeng Barako</h3>
<p>Strong Batangas coffee, 250g, freshly ground.</p>
<p class="price">₱180</p>
</div>
Try changing padding: 20px to 0 to see how much spacing matters.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04