Why You Should Never Build Spacing Into Your Components

Leave outer spacing out of reusable components and let layouts decide.

There are times when you build a component, give it a nice margin-bottom so it looks right on the first page you use it on, and move on. Then it shows up in a grid, a sidebar and a modal, and every one of those places needs different spacing. Now you're fighting your own component.

The fix is simple: a reusable component shouldn't decide how far it sits from the things around it. Leave that out and let the place using it add the right spacing for that layout.

A component owns the space inside it. The parent owns the space between components.

This is often called "layout isolation", or keeping your components "margin-free".

What counts as spacing?

Not all spacing is the problem. Internal padding is part of how a component looks. A button needs padding around its label, and a card needs padding around its content. That stays.

What doesn't belong in a reusable component:

  • Outer margins (margin-top, margin-bottom and so on)
  • Padding that's only there to push the component away from its neighbors, like a section with a big padding-top that's really acting as a margin

The right gap between two things depends on where they are. A component can't know that, so it shouldn't guess.

The problem

Here's a Card with spacing built in:

// components/Card.js
export function Card({ title, children }) {
return (
<div className="card">
<h3>{title}</h3>
{children}
</div>
)
}
.card {
padding: 24px;
border-radius: 8px;
box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
margin-bottom: 24px;
}

In a single column this looks fine. Put the cards in a grid with a gap and you get extra space under every row, plus 24px of empty space at the bottom of the grid that pushes the next section down.

So the workarounds start:

.card:last-child {
margin-bottom: 0;
}
.sidebar .card {
margin-bottom: 12px !important;
}

Or someone adds a className prop to override it, and now the result depends on which stylesheet loads last. Every new layout adds another exception, and nobody wants to touch the original margin-bottom because they don't know what will break.

The fix: remove the margin

Take the margin out of the component and keep only the internal styles:

.card {
padding: 24px;
border-radius: 8px;
box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
}

Now the card looks the same everywhere, and it sits flush with whatever is around it. Spacing becomes the job of the layout that holds it.

Let the parent control spacing with gap

The easiest way for a parent to space its children is gap. It only adds space between items, never before the first or after the last, so there's nothing to undo.

.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
gap: 24px;
}
.sidebar {
display: flex;
flex-direction: column;
gap: 12px;
}

The same Card now has 24px between cards in the grid and 12px in the sidebar, without a single override.

gap on grid has been around for a while. gap on flexbox was the missing piece until Safari added it in version 14.1 in April 2021, so it now works in all major browsers and is safe to use.

A Stack component

Most layouts are just things stacked on top of each other with some space between them. Instead of writing a new wrapper class each time, make that a component:

// components/Stack.js
export function Stack({ gap = 4, children }) {
return (
<div
style={{
display: "flex",
flexDirection: "column",
gap: `var(--space-${gap})`,
}}
>
{children}
</div>
)
}

Using it looks like this:

<Stack gap={8}>
<PageHeader title="Projects" />
<Stack gap={4}>
<Card title="Website redesign">...</Card>
<Card title="Mobile app">...</Card>
</Stack>
</Stack>

Stack is the only thing that knows about spacing here. PageHeader and Card don't have any margins, so you can drop them into any other layout and they'll behave. Some design systems call the prop space instead of gap. Either works.

Here's where this pays off. A Tabs component might sit at the top of a page, or inside a card. It's the same component in both places, but the page wants a lot of room around it and the card wants very little. Because Tabs has no margin of its own, each Stack decides:

You can build an Inline component the same way with flexDirection: "row" and flexWrap: "wrap" for rows of buttons or tags.

Use spacing tokens

The gap prop maps to values from a spacing scale instead of taking any number of pixels. I covered setting one up in 10 design tips for developers:

:root {
--space-1: 4px;
--space-2: 8px;
--space-3: 12px;
--space-4: 16px;
--space-6: 24px;
--space-8: 32px;
--space-12: 48px;
--space-16: 64px;
}

So gap={6} is always 24px. This keeps spacing consistent across the app, and when a layout looks too tight the fix is one step up the scale instead of a random margin-top: 13px.

The internal padding in your components should use the same tokens:

.card {
padding: var(--space-6);
}
.button {
padding: var(--space-2) var(--space-4);
}

What about one-off spacing?

Sometimes you need a little extra space above one element on one page. Wrap it or add it in the page's own styles, not in the component:

<div style={{ marginTop: "var(--space-12)" }}>
<Card title="Danger zone">...</Card>
</div>

The exception stays in the one place that needs it, and the component stays the same everywhere else.

Keep your components margin-free and let layouts own the space between things. Your components get easier to reuse, and you'll stop writing !important to undo them.

Have some feedback on this post? .