Sub topic 3
Padding versus margin: spacing inside and outside
This chapter assumes you know the CSS box model, basic selectors, and rem units. You should also know that a spacing scale is a fixed set of values. A later chapter builds one with custom properties, so the examples here use plain rem values.
Two kinds of space
Every element is a box with four layers: content, padding, border, and margin. Padding and margin both add empty space, but they place it on different sides of the border.
- Padding is space between the content and the element's own border. It sits inside the element.
- Margin is space outside the border. It pushes other elements away.
The difference matters for grouping. Padding belongs to the element, so the background color, border, and click area all include it. Margin belongs to no element. It is a gap in the layout, and the parent's background shows through it.
What each one communicates
Padding tells the reader that the content is one unit. A card with generous padding reads as a self-contained object. Margin tells the reader how far apart two units are. Combined with the proximity principle, small margins join items into a group and large margins separate groups.
A useful rule of thumb: space between peers is margin or gap, and space inside a container is padding.
A worked example
This card uses padding for its inner space and a margin for its distance from the next block:
<style>
:root { font-size: 100%; }
.card {
padding: 1.5rem;
border: 1px solid #ccc;
background: #f7f7f7;
margin-bottom: 2.5rem;
}
.card h2 { margin: 0 0 0.5rem; }
.card p { margin: 0; }
</style>
<article class="card">
<h2>Delivery</h2>
<p>Orders ship within two days.</p>
</article>
<article class="card">
<h2>Returns</h2>
<p>You can return items for 30 days.</p>
</article>The padding (1.5rem) keeps text away from the card's edge, and the gray background fills that space. The heading and paragraph are separated by a small 0.5rem margin because they belong together. The 2.5rem margin between cards is five times larger, so the cards read as separate groups.
Notice that the browser's default margins on h2 and p are reset. Default margins are a frequent source of uneven spacing, because they differ per element and per browser.
Common errors
- Using margin to space content inside a box. The box's background will not cover the margin, so the content looks detached. Use padding.
- Using padding to separate two boxes. Each box's background stretches into the padding, so the boxes merge visually or the borders touch the wrong way. Use margin or gap.
- Adding margin to every child. The last child leaves extra space at the bottom of the container. Put spacing on the parent with
gap, or only between siblings. - Mixing many values. Using 13px here and 17px there weakens the grouping signal. The reader cannot tell which differences are meaningful.
- Expecting margins to always add up. Adjacent vertical margins can combine in normal flow. A later chapter covers this.
Quick comparison
| Property | Space is | Takes the background | Counts toward clickable area |
|---|---|---|---|
| padding | inside the border | yes | yes |
| margin | outside the border | no | no |
Exercise
A button has a blue background, but the label text sits right against its edge. A teammate adds margin: 0.75rem 1.5rem to fix it. Why does this not work, and what should they use instead?
Show answer
Margin adds space outside the button's border, so the blue background stays the same size and the text still touches its edge. Margin also pushes neighboring elements away. They should use padding: 0.75rem 1.5rem, which adds space between the text and the edge inside the blue area.