Sub topic 7
Spacing Stacked Siblings with a Flow Rule
This chapter assumes you know the CSS box model, rem units, and the spacing scale custom properties (--space-1 to --space-6) from earlier chapters. It also assumes you know that adjacent vertical margins can collapse.
The problem with per-element margins
A common habit is to give every element its own bottom margin. That causes three problems:
- The last element in a container carries a margin it doesn't need, so the container ends with extra space below it.
- Moving an element to another container brings its old spacing along.
- Adjacent margins collapse, so the real gap is hard to predict.
A flow rule moves the decision from each element to the container. It puts space only between consecutive siblings, and it does not care what those siblings are.
The lobotomized owl selector
The rule uses the adjacent sibling combinator + and the universal selector *:
.flow > * + * {
margin-block-start: var(--flow-space, var(--space-3));
}Read it from right to left. It selects any direct child of .flow that has a previous sibling. The first child has no previous sibling, so it gets no margin. Every other child gets space above it. Nothing is added after the last child.
margin-block-start is the logical version of margin-top. It follows the writing direction, so the rule still works in vertical writing modes.
A custom property with a fallback
var(--flow-space, var(--space-3)) reads the custom property --flow-space. If no ancestor has defined it, the browser uses the second value, --space-3. A container can therefore change the spacing by setting the property:
.flow.tight {
--flow-space: var(--space-2);
}Custom properties inherit. Because .flow > * + * reads --flow-space on each child, a value set on the container reaches all of its children.
<div class="flow">
<h2>Delivery</h2>
<p>Orders ship within two working days.</p>
<p>You will get a tracking link by email.</p>
</div>
<div class="flow tight">
<h3>Tags</h3>
<p>Shipping, returns, sizing</p>
</div>The first container uses --space-3 (1rem) between its children. The second uses --space-2 (0.5rem). The h2 and h3 get no margin above them, so the container's own padding or its neighbors decide how far it sits from the rest of the page.
Why it works with margin collapsing
Flow spacing uses only margin-block-start. Each gap is made by one margin, not by a bottom margin meeting a top margin. If a child also has a larger top margin of its own, the two collapse and the larger one wins. That is predictable, because you control one value per gap.
Nesting and the child combinator
The rule uses > so it only affects direct children. A nested .flow sets its own spacing, and an unrelated list inside a .flow is not touched. Without >, the rule would add margin to every descendant that has a previous sibling, including list items.
.flow > * + * {
margin-block-start: var(--flow-space, var(--space-3));
}
.flow > h2 {
margin-block-start: var(--space-5);
}The second rule is an exception. A heading starts a new group, so it gets a larger gap above it. Compare this with the proximity principle: the heading is closer to the text below it (--space-3) than to the text above it (--space-5).
When not to use it
A flow rule suits text-like content stacked in one column. For rows of cards or items laid out in a grid or flex container, use gap, which spaces items in both directions. Do not apply .flow to a flex or grid container, because the margins would add to the gap.
Exercise
A .flow container holds an h2, a p and a ul. You want twice as much space between the items in one container, without writing a new selector rule. What do you change?
Show answer
Set --flow-space on that container, for example .flow.loose { --flow-space: var(--space-5); }, and add the loose class. The existing .flow > * + * rule reads the new value for each child.
Exercise
Why does .flow > * + * not leave extra space below the last child?
Show answer
It adds margin-block-start to children that follow a sibling. No margin is placed after any element, so the last child has nothing below it.