Sub topic 3
Padding versus margin: spacing inside and outside
This chapter assumes you know basic HTML structure, CSS selectors, the box model, and rem and em units. It covers how to choose between padding and margin, what each one does to backgrounds, borders and click areas, and the errors that come from mixing them up. The next chapters build a spacing scale and then look at gap and margin collapsing.
Two kinds of space
Every element is a box with four layers. From the inside out, they are content, padding, border and margin.
- Padding is the space between the content and the border. It sits inside the element.
- Margin is the space outside the border. It separates the element from its neighbours.
The simplest way to remember the difference is to ask where the space belongs. Padding belongs to the element. Margin belongs to the relationship between the element and whatever is next to it.
What each one affects
The two properties differ in what they change visually and in how the browser treats them. These differences decide which one to use.
| Behaviour | Padding | Margin |
|---|---|---|
| Shows the element's background color or image | Yes | No, margin is always transparent |
| Sits inside the border | Yes | No, it sits outside |
| Counts toward the clickable area of the element | Yes | No |
| Can be negative | No | Yes |
| Collapses with neighbouring spacing | No | Yes, vertical margins in normal flow can |
| Can be `auto` | No | Yes, for centering and pushing |
| Percentage values refer to | Width of the containing block, on all four sides | Width of the containing block, on all four sides |
Two rows in this table cause the most real-world bugs: background and clickable area, and collapsing. Collapsing has its own chapter, so here we focus on the first.
Background and click area
Suppose you build a button as a link with a background color. If you use margin to make it bigger, the color does not grow, and the extra space is not clickable.
<style>
.btn-margin {
background: #1d4ed8;
color: white;
margin: 0.75rem 1.5rem;
}
.btn-padding {
background: #1d4ed8;
color: white;
padding: 0.75rem 1.5rem;
}
</style>
<a class="btn-margin" href="#">Margin button</a>
<a class="btn-padding" href="#">Padding button</a>The first link looks like a small blue strip of text. It sits among empty space that you cannot click. The second link has a larger blue block, and the whole block responds to the pointer. For any element that has a visible surface (a button, a card, an alert, a table cell) the space that makes it feel roomy should be padding.
One more detail: <a> is an inline element by default. Horizontal padding and margin work on inline elements, but vertical margin does not, and vertical padding grows the background without pushing neighbouring lines away. Set display: inline-block or use a block or flex container when you need the box to behave predictably.
Using the difference to group content
The proximity principle from the previous chapter says that elements placed close together read as related. Padding and margin are the two tools that set those distances, but they act at different levels.
- Padding sets the distance between a container's edge and its own contents. A card with generous padding gives its content breathing room and makes the card look like one unit.
- Margin sets the distance between separate units. Larger margins between cards than the padding inside each card tell the reader that the cards are distinct groups.
A reliable rule is that the space inside a group should be smaller than the space around it. If the padding inside a card is 1.5rem but the cards sit 0.5rem apart, the eye reads the gap between cards as part of one large surface, and the edges of the cards blur together.
<style>
:root {
--space-2: 0.5rem;
--space-4: 1.5rem;
--space-5: 2.5rem;
}
.panel {
background: #f3f4f6;
padding: var(--space-4);
margin-block-end: var(--space-5);
}
.panel h2 {
margin: 0 0 var(--space-2);
}
.panel p {
margin: 0;
}
</style>
<section class="panel">
<h2>Account</h2>
<p>Manage your email and password.</p>
</section>
<section class="panel">
<h2>Notifications</h2>
<p>Choose which messages you receive.</p>
</section>Here three distances form a clear hierarchy. The heading is 0.5rem from its paragraph, the content is 1.5rem from the panel edge, and the panels are 2.5rem apart. Each step is larger as the relationship gets weaker. The gray backgrounds only fill the padding area, so the margin between panels shows the page color and reads as a gap.
Choosing between them
When you are about to type padding or margin, work through these questions in order.
- Should the space show the element's background or be part of its click target? Use padding.
- Is the space between this element and its siblings? Use margin on the element, or better,
gapon the parent (covered in a later chapter). - Is the space between the element and the edge of its container? Use padding on the container, not margin on the child. A margin on the first child can collapse through the parent's top edge, which is a common surprise.
- Do you need to center a block or push it to one side? Use
margin-inline: auto, or in a flex container,margin-inline-start: auto. - Do you need the element to overlap its neighbour? A negative margin is the tool here. Use it sparingly.
Logical properties
The examples above use margin-block-end and margin-inline instead of margin-bottom and margin-left. These are logical properties. They follow the writing direction of the page instead of the physical screen. In a left-to-right language, inline means horizontal and block means vertical. In a vertical writing mode or a right-to-left language, they adapt automatically.
| Physical | Logical |
|---|---|
| `margin-top` | `margin-block-start` |
| `margin-bottom` | `margin-block-end` |
| `margin-left` | `margin-inline-start` |
| `margin-right` | `margin-inline-end` |
| `padding-top` and `padding-bottom` | `padding-block` |
| `padding-left` and `padding-right` | `padding-inline` |
Both styles work in current browsers. Pick one and use it consistently in a project.
Common errors
Using margin for space inside a box
A developer wants a card's text to sit away from the card edge and adds margin to the text. The text moves, but the margin of the first and last child may collapse through the card's edges, and the card's background does not behave as expected. Put the padding on the card.
Doubling up space
If every .card has margin: 1rem and the cards sit in a row, the space between two cards is 2rem (horizontal margins do not collapse), but the space at the outer edges is only 1rem. The layout looks uneven. Using gap on the parent gives equal spacing between items and none at the edges.
Forgetting `box-sizing`
By default, width applies to the content box, so adding padding makes the element wider than its declared width. A width: 100% input with padding: 1rem overflows its container.
*,
*::before,
*::after {
box-sizing: border-box;
}With border-box, the width includes padding and border. Margin is never included, because it is outside the element.
Percentage surprises
A percentage padding or margin is calculated from the width of the containing block, including padding-top and margin-top. So padding-top: 10% in a 600px wide parent gives 60px of space, regardless of the parent's height. This is occasionally useful for aspect-ratio tricks, but it makes spacing unpredictable. Prefer rem values from a scale.
Padding on inline elements
Vertical padding on an inline element such as <span> paints the background outside the line box but does not change the line spacing. Following lines can then overlap the painted area. If you need the vertical space to count, make the element inline-block.
Summary of the working model
- Padding is part of the element, so it takes the background, joins the click target and never collapses.
- Margin is outside the element, so it is transparent and separates the element from neighbours.
- Space inside a group should be smaller than the space between groups.
- Apply
box-sizing: border-boxglobally so padding does not change declared widths. - Choose from a fixed set of values so the hierarchy of distances stays consistent. The next chapter shows how to build that set.
Exercise
A .alert element has a yellow background and margin: 1rem 1.5rem. The text sits right against the edge of the yellow area, and there is empty white space around the alert. Which property should you change, what should you change it to, and why?
Show answer
Change margin to padding, for example padding: 1rem 1.5rem. Margin is outside the element's border and is always transparent, so it creates white space around the yellow box without moving the text away from the box edge. Padding sits inside the border, shows the background color, and pushes the text inward. You can keep a separate margin, such as margin-block-end: 1rem, if the alert needs space from the element below it.
Exercise
Two .card elements sit side by side in a flex row. Each has padding: 1rem and margin: 1rem. The page looks uneven: the gap between the cards is larger than the gap between a card and the page edge. Explain why, and name a cleaner approach.
Show answer
Horizontal margins on adjacent flex items do not collapse, so the space between the cards is 1rem plus 1rem, which is 2rem. The outer edges get only the 1rem margin of one card. A cleaner approach is to remove the margin from the cards and set gap: 1rem on the flex parent. The gap appears only between items, so the spacing is equal and no extra space is added at the outer edges.
Exercise
A page has cards with padding: 2rem and margin-block-end: 0.5rem. Readers say the cards look like one long block. What is wrong, and what values would fix it?
Show answer
The space inside each card (2rem) is larger than the space between cards (0.5rem). By the proximity principle, the content in different cards sits closer together than the content inside a single card, so the groups are not visible. Make the space between cards larger than the padding inside, for example keep padding: 1.5rem and set the margin or parent gap to 2.5rem.