Outer space
Margin pushes neighboring boxes away from the outside edge.
Margin is outside spacing. It separates one box from another and creates rhythm between sections, cards, paragraphs and controls.
.section + .section {
margin-block-start: 5rem;
}
Box Model
Margin lives outside the border. It does not make the component itself more comfortable; it changes the distance between that component and other boxes.
Vertical margins can collapse in normal flow, which surprises many beginners. Two adjacent margins may combine instead of simply adding together.
Professional CSS uses margin deliberately for layout rhythm and often prefers parent gap for repeated spacing between children.
Margin pushes neighboring boxes away from the outside edge.
Vertical margins in normal flow can collapse into one shared margin.
Useful for centering blocks or pushing flex items apart.
margin-block and margin-inline adapt better to writing direction.
Visual model
Layout debugging becomes much easier when you can point to the exact part of the box that creates the visual result: inner space, outer space, edge, size or overflow behavior.
Use margin-block to separate major page sections.
Use parent gap when many children need equal spacing.
Use auto margin to absorb available space in flex layouts.
Examples
.section + .section {
margin-block-start: 5rem;
}
.stack {
display: grid;
gap: 1rem;
}
.card { margin: 17px 0 43px 9px; }
.card h2 { margin-top: 51px; }
.card p { margin-bottom: 37px; }
Rules that matter
Box model CSS should make the interface easier to reason about. When a value cannot be explained, it usually becomes a future layout bug.
If the space is inside the component, it is probably padding.
Grid and flex gap avoid many margin edge cases.
Collapsed vertical margins can make spacing look smaller than expected.
margin-block and margin-inline describe direction more clearly.
Parent layouts should usually control spacing between children.
A block with width can be centered with margin-inline: auto.
Production thinking
Margin creates rhythm. Random margins create messy pages even when every individual component looks fine.
Clear spacing improves scanning and reduces accidental taps between controls.
Decide who owns spacing: parent layouts, component wrappers or page sections. Mixing all three causes drift.
Readable spacing helps visitors consume content and stay oriented on long pages.
Live code lab
The preview runs in an isolated iframe. Links and forms stay inside the practice area, so you can experiment without leaving the lesson.
Mini assignment
Practice assignment
Try it yourself
Self-check
Answer these questions before moving on. If the answer is vague, change the lab CSS and inspect the box again.
Senior audit upgrade
Vertical margins between blocks in normal flow can collapse into one margin. This is normal browser behavior, not a random bug.
The larger vertical margin can win instead of both margins adding together.
A first or last child margin can appear to escape its parent in normal flow.
Use padding, border, flow-root or gap-based layout when collapse is not desired.