Sub topic 5
Margin: space outside the border
Margin is the transparent space outside an element's border. It pushes neighbouring boxes away and sets the gap between an element and whatever surrounds it. This chapter assumes you know basic HTML structure, how to write CSS rules, and common length units such as px, rem, and %.
What margin does
Margin is the outermost layer of the box model. It has no background or border of its own. The element's background color stops at the border edge, so a margin always shows whatever is behind the parent.
Margin does not change the element's own border box. It changes how much space the element claims in the layout. With the default content-box sizing, this card has a 350px-wide border box (300 content + 40 padding + 10 border) and occupies 370px of horizontal space including its margins:
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}Margin shorthand
You can set each side with margin-top, margin-right, margin-bottom, and margin-left. The shorthand sets them together. Four values go clockwise from the top. Two values set vertical, then horizontal.
/* top right bottom left */
.box {
margin: 10px 20px 30px 40px;
padding: 8px 16px;
}| Values | Meaning |
|---|---|
| `margin: 10px` | All four sides |
| `margin: 10px 20px` | Top and bottom, then left and right |
| `margin: 10px 20px 30px` | Top, left and right, bottom |
| `margin: 10px 20px 30px 40px` | Top, right, bottom, left |
Negative and auto margins
Unlike padding, margin accepts negative values. A negative margin pulls an element toward its neighbour or lets it overlap them. Use it sparingly, because it makes layouts harder to reason about.
The value auto tells the browser to share the leftover space. On a block-level element with a set width, margin-inline: auto centres it horizontally. Vertical auto margins resolve to 0 in normal block layout.
Margin collapsing
When two vertical margins touch, they can merge into one. This is margin collapsing. The gap equals the larger of the two margins, not their sum.
<style>
h2 { margin-bottom: 24px; }
p { margin-top: 16px; }
</style>
<h2>Heading</h2>
<p>Paragraph</p>Here the gap between the heading and the paragraph is 24px, not 40px.
Collapsing happens only between vertical margins of block-level boxes in normal flow. Horizontal margins never collapse. Collapsing also occurs between a parent and its first or last child when nothing separates their edges, such as padding, a border, or a new formatting context.
Preventing collapse
Making the container a new block formatting context keeps the child's margin inside it:
.container {
display: flow-root;
}
.container > h1 {
margin-top: 2rem;
}Logical margins
Physical names like margin-left assume a left-to-right, horizontal layout. Logical properties follow the writing direction instead:
.article {
max-inline-size: 40rem;
margin-inline: auto;
padding-block: 1rem;
}margin-inline covers the start and end of the text line. margin-block covers the start and end of the block axis. In English, these map to left/right and top/bottom.
Common errors
- Expecting two stacked margins to add up. Check whether they collapse.
- Setting vertical margin on an inline element such as
<span>. It has no effect on line layout. - Using
margin: autoto centre an element with no width set. A block withwidth: autoalready fills its container, so there is nothing to centre. - Using margin where you need a visible background. Use padding for that.
Exercise
A <div class="a"> with margin-bottom: 30px is followed directly by a sibling <div class="b"> with margin-top: 20px. Both are block elements in normal flow. What is the vertical gap between their borders, and what single change to the CSS would make the gap 50px?
Show answer
The gap is 30px, because the adjoining margins collapse to the larger value. To get 50px, stop the collapse, for example by wrapping one div in a container with display: flow-root or by replacing one margin with padding or a border-separated spacer. Alternatively, set a single margin of 50px on one of them.