To put space between two buttons, use the parent's layout system first: in flexbox, set gap on the container; in CSS Grid, also use gap; for inline-block buttons, control spacing with margins or the parent's font-size because HTML whitespace between tags renders as a space. Reach for button margins only when the container gives you no gap support or you need asymmetric spacing, and switch to a padding wrapper when you need a larger invisible hit area or a full-width stacked layout. The right method depends on which layout context the buttons sit in, not on the buttons themselves, which is why the same margin-left that works in one component collapses or doubles in another. The rest of this guide walks through each context in a workable order, with checks for verifying the result.
Start by identifying the parent layout
Buttons don't own their own spacing; the container that holds them does. Before writing any CSS, answer three questions about the immediate parent element:
Is it a flex container (does it have display: flex or inline-flex)? Is it a grid container? Is it plain block or inline flow, where buttons sit as inline-level boxes?
You can confirm this in the browser's developer tools by selecting the parent and reading the computed display value, or by checking whether gap already has any effect in the inspector. This matters because gap is honored by flex and grid containers and by multi-column layouts, but it does nothing on an ordinary block container. In plain flow, spacing comes from whitespace characters and margins instead.
There's a fourth scenario worth naming early: buttons rendered as separate block elements, either because you set display: block on them or because each one sits in its own wrapper. That case behaves like any stacked block layout and uses margins or the parent's flow spacing.
Getting the context right prevents the most common failure: adding gap to a container that ignores it, seeing no change, then piling on margins that later conflict with a media query. Identify the context, pick the matching method, and verify with the element inspector.
Method 1: Use gap in a flex container
Flexbox is the most common home for side-by-side buttons, and gap is the cleanest tool because it applies space between items only. It never adds space before the first button or after the last, and it doesn't collapse or double the way adjacent margins can.
Here's a minimal, working structure. First the HTML:
Then the CSS:
The two buttons sit side by side with exactly 12 pixels of space between them and nothing on the outside edges. If you want the pair pushed to the right side of a card, add justify-content: flex-end; the gap stays between the buttons regardless of alignment.
Two conditions make this reliable. First, the parent must actually be a flex container; gap on a block parent is ignored in older browser versions and still has no effect in plain flow today. Second, watch flex-wrap. When the container wraps to a second line, gap applies both between columns and between rows, so a single gap: 12px gives you 12 pixels in both directions. If you want different vertical and horizontal spacing, use the two-value form: gap: 8px 12px sets row gap to 8 and column gap to 12.
For button groups where the buttons should stay visually attached, a negative-margin trick is often unnecessary; a row of buttons with gap and shared border radii reads cleanly. Reserve attached styling for segmented controls, and even then, gap is usually the simpler starting point.
Check success: inspect the container and confirm the computed display is flex and the computed gap matches your value. The space should appear only between the buttons.
Method 2: Use gap in a CSS Grid container
Grid buttons show up in toolbars, dialog footers, and form action rows that need precise column placement. The gap property works the same way conceptually, with one useful difference: you can define explicit tracks and let the gap separate them.
grid-auto-flow: column places the buttons in a single row of auto-sized columns, and the 10-pixel gap separates each column. If you prefer explicit tracks, declare them:
The advantage over flex here is predictability when buttons have different widths, since grid columns can be sized independently and the spacing is guaranteed uniform between tracks. The trade-off is slightly more setup for a simple two-button row, so flex is usually the lighter choice unless you're already in a grid layout or need column alignment.
The same two-value gap syntax applies: gap: 6px 10px gives 6 pixels between rows (relevant if the grid wraps into multiple rows) and 10 between columns. On very old browsers that predate grid gap support, the property may be ignored entirely, so if you must support them, fall back to margins. Current evergreen browsers handle gap in both flex and grid without issue.
Check success: the element inspector shows a grid overlay with visible gaps between the track lines, and no gap appears outside the button pair.
Method 3: Control spacing for inline-block buttons
Buttons are inline-block by default in most user-agent stylesheets, so a container without flex or grid leaves them in normal inline flow. Here, gap does nothing. The visible space between two tags comes from the whitespace character between them in the HTML source, which the browser collapses to a single space whose width depends on the parent's font-size and font.
This produces two common surprises. First, the spacing looks arbitrary: on a parent with font-size: 16px, the gap is a font-dependent space, maybe 4 to 5 pixels, and changing the font changes the gap. Second, developers who remove the whitespace to compact their markup often find the buttons suddenly touch.
You have three practical options:
1: Set an explicit margin on one side. Apply margin-right: 8px to the first button (or margin-left to the second), then remove the trailing margin on the last child with a selector such as .actions button:last-child { margin-right: 0; }. This is deterministic and doesn't depend on font metrics. 2: Use the parent's font-size: 0 trick and restore it on the buttons. Set font-size: 0 on the container to collapse the whitespace to nothing, then set an explicit font-size back on the buttons themselves and add margins for spacing. This is workable but fragile: child elements that inherit font size for icons or text need their own resets. 3: Switch the container to flex. If you're already editing the component, converting the parent to display: flex and using gap is usually less code than fighting inline whitespace.
For a two-button case, option 1 is the most maintainable. Just remember the last-child reset so the pair doesn't have a stray trailing margin that shifts alignment inside centered or right-aligned layouts.
Check success: the gap stays constant when you change the page's font size or font family, which confirms you're not relying on a whitespace character.
Method 4: Use margins where the layout leaves them to you
Margins are the universal fallback. Block-level buttons stack with margin-bottom between them; inline or inline-block buttons use margin-right or margin-left on one side.
A symmetric approach uses adjacent-sibling and last-child resets:
This produces uniform space between buttons and no trailing space after the last one. It's especially useful in older codebases or when a CMS renders button markup you can't restructure.
The classic margin pitfall is margin collapsing between vertically stacked block buttons. Two block elements with margin-bottom: 16px and margin-top: 16px respectively don't produce 32 pixels; the margins collapse to the larger of the two, so you get 16 pixels. If you need guaranteed combined spacing, use gap in a flex column or switch to padding. Collapsing applies to block-level boxes in normal flow, not to flex or grid items, which is another reason flex containers are friendlier for button rows.
Also watch for doubled horizontal margins when both buttons carry margin-left and margin-right. Pick one side per element, and reset on the edge-most child.
Check success: measure the computed margin in the inspector and confirm the last button's outer edge aligns flush with the container's content edge.
Method 5: Larger touch targets with a padding wrapper
Sometimes the visual gap between two buttons needs to be smaller than the tappable area around each one, especially on mobile. Settings that call for at least 24 CSS pixels of target size, and many design systems target larger, mean the button's clickable zone can extend beyond its painted edge.
Wrapping each button in a clickable region gives you both: visible spacing can look tight while the hit area stays generous.
The wrapper's padding adds to the tappable zone without changing the visual gap between the buttons' visible edges. Keep the padding symmetric so the target doesn't favor one side, and make sure the visible button inside still has sufficient contrast.
A related but distinct technique is used for full-width stacked buttons in mobile layouts. Here you want each button on its own line with a consistent vertical gap:
This avoids the collapsed-margin problem and gives you a single value to adjust, and it pairs well with media queries that switch from a row on wide screens to a column on narrow ones.
Check success: on a narrow viewport, verify each button spans the container and the space between them is uniform. In the inspector, the wrapper's padding box should be visible around the clickable area, and clicking inside the padding should activate the button when the wrapper is the interactive element.
Spacing inside a responsive row
A single gap value rarely works across every screen size. A 12-pixel gap that looks right on desktop can feel cramped on a small phone, and a generous gap on mobile can overflow two buttons on a narrow card.
The supported pattern is to set a base gap and adjust it in a media query, or to let the gap scale with the layout:
When buttons are allowed to wrap, the row gap from a single-value gap also applies between wrapped lines. If you stack at narrow widths, switch flex-direction to column in the same query; the gap property then measures vertical distance between the stacked rows.
For right-aligned action rows in dialogs, justify-content: flex-end with a gap keeps the pair together at the edge. For evenly spread buttons, justify-content: space-between distributes the extra space, but note that it interacts with the gap: the gap is a minimum between buttons and the free space is added on top, so the visible spacing won't equal your gap value.
Check success: resize the viewport and confirm the visible gap matches the value set for the active breakpoint, and that a wrapped row still separates buttons vertically.
A worked example: a dialog action row
Suppose a dialog footer contains Cancel and Save, sits at the right edge on desktop, and stacks full width on phones. Put the prerequisites in place first: a parent container with a class, buttons with type="button", and a stylesheet you can edit.
1: Set the parent to flex with a base gap and right alignment:
2: On narrow screens, stack the buttons and let them fill the width:
column-reverse puts Save visually first on mobile, which matches common dialog conventions; adjust to your design. The gap handles the spacing in both orientations.
3: Verify: on desktop, the buttons sit side by side right-aligned with 12 pixels between them. On mobile, they stack full width with 8 pixels of vertical space and nothing collapsing or doubling. In the inspector, the computed display is flex and the computed gap matches each breakpoint.
The arithmetic here is straightforward: the row total is the sum of the two button widths plus 12 pixels, and the column total is the two heights plus 8 pixels. If the buttons overflow on small screens, the fix is usually flex-wrap: wrap or the column switch above, not a larger margin.
Common mistakes and how to reverse them
The failures below account for most "spacing won't change" reports, ordered from the checks that are easiest to undo to the ones that require more restructuring.
Symptom: adding gap to the parent does nothing. Likely cause: the parent isn't a flex or grid container. Check the computed display. Reversible fix: add display: flex to the parent if the layout allows, or switch to margins.
Symptom: extra space appears outside the buttons after adding margins. Likely cause: the trailing margin on the last child. Add a :last-child reset. Reversible fix: remove the margin and rely on gap.
Symptom: vertical spacing between stacked buttons is smaller than expected. Likely cause: margin collapsing on block-level buttons. Confirm by inspecting adjacent margins. Fix: switch the parent to a flex column and use gap, or replace margins with padding.
Symptom: gap changes when the page font changes. Likely cause: inline-block buttons relying on whitespace between tags. Confirm by changing the font size in the inspector. Fix: use explicit margins or convert the parent to flex.
Symptom: the gap looks right on desktop but buttons overflow on mobile. Likely cause: a fixed gap plus fixed-width buttons exceeding the viewport. Fix: allow wrapping or switch to a column layout at narrow widths.
Symptom: buttons in a right-aligned row touch the edge. Likely cause: a trailing margin on the last button. Fix: the same :last-child reset.
Symptom: only one gap value is set and both row and column spacing look wrong after wrapping. Likely cause: the single value applies to both axes. Fix: use the two-value gap: row column form.
If you've verified the display context, checked for collapsing and trailing margins, and the spacing still behaves unexpectedly, the next diagnostic is to inspect the button's own default styles. User-agent button styles include borders and padding that can make spacing look larger or smaller than intended. Normalize the button with box-sizing: border-box and explicit padding before assuming the layout is at fault. If the problem persists in a component library or framework, check that library's own margin resets, which sometimes set button margins to zero with higher specificity than your rules. Beyond that, the work involves auditing the component hierarchy against your design system's spacing scale rather than any CSS property trick, so it's worth pausing before layering more overrides on top.