A card can look cramped inside while sitting too far from its neighbors. Fixing both problems with one property can make the layout harder to reason about. To control spacing in CSS, first identify where the empty space belongs: inside the card, outside its border, or between items in a shared layout.
Spacing CSS Can Give a Card Layout
Consider a page with a row of article cards. The HTML contains a parent and two cards, each with a heading and a paragraph:
<main class="page">
<div class="cards">
<article class="card"><h2>Plan</h2><p>Choose a route.</p></article>
<article class="card"><h2>Build</h2><p>Make the first version.</p></article>
</div>
</main>
* { box-sizing: border-box; }
.page { margin: 2rem; }
.cards { display: flex; flex-wrap: wrap; gap: 1rem; }
.card { flex: 1 1 15rem; padding: 1.5rem; border: 1px solid #555; }
.card h2 { margin: 0 0 0.5rem; }
.card p { margin: 0; }
The spacing CSS creates here has three jobs. The page margin separates the page box from its surroundings, card padding gives the text breathing room inside the border, and the container’s gap separates the cards. Resizing the viewport can wrap the cards onto another row; the same gap provides space between rows. The cards can grow to share available space, and each has a preferred flex basis of 15rem before flex sizing is resolved.
This CSS spacing arrangement keeps repeated relationships on the parent and each card’s interior on the card itself. It also avoids adding an extra outer margin to every card just to separate neighbors. Try changing one CSS spacing value at a time and watch which edges move: that is often faster than guessing from the finished page.
Padding belongs inside the border
Padding sits between content and border. The card background extends through that area, so increasing padding makes more of the card’s background visible around its text. A button uses padding for the same reason: it enlarges the area inside the button around its label. You can set individual sides or use shorthand, such as padding: 1rem 2rem for vertical and horizontal values.
When planning space with CSS, remember the box model. With the default content-box, a declared width describes the content box and padding and border add to the rendered width. The example uses border-box, so a declared width normally includes padding and border. This distinction matters when cards must fit within a narrow container.
CSS padding for spacing cannot be negative. It also affects the space available for text when the outer size is constrained, potentially causing lines to wrap sooner. If the goal is separation from a neighboring component rather than room inside the border, choose another property. Good CSS spacing starts with the boundary you want to move.
Margin belongs outside the border
In CSS, spacing created by margin sits outside an element’s border. It is useful for placing a component relative to surrounding content, or creating a deliberate offset for one item. A margin is transparent; the element’s background does not paint it. The margin: 2rem on .page applies on all four sides, while the heading’s bottom margin separates it from its paragraph.
The spacing CSS applies in normal block flow has an important wrinkle: adjacent vertical margins can collapse. If one paragraph has a 20px bottom margin and the next has a 30px top margin, the space between those two positive margins is generally 30px, not 50px. A parent’s margin can also collapse with a first or last in-flow child’s margin under the relevant conditions. Borders, padding, or a new block formatting context can prevent certain parent-child collapses.
Negative margins have different rules: a collapsed set combines the largest positive value with the most negative value. Margins of flex and grid items do not collapse with each other. When a vertical gap seems smaller than the sum you expected, inspect the boxes and their layout context before adding another margin. Understanding CSS spacing in normal flow prevents a chain of compensating offsets.
Gap describes a relationship
For layout spacing, CSS gap is set on a flex or grid container and creates gutters between items or tracks. It does not add a gutter before the first item or after the last one; add container padding if the layout also needs space at its edges. In the example, the card border remains distinct from the one-rem interval between cards.
In a grid, row-gap and column-gap can be set separately, or combined as gap: 1rem 2rem. Flex layouts also use gaps between items and between flex lines when wrapping. The visible distance can exceed the declared gap if margins or alignment such as justify-content: space-between add more space. Inspect those values before concluding that the gap itself is wrong.
For repeated cards, spacing in a CSS layout is easier to maintain when the parent owns the common interval. If a single card needs a special offset, an individual margin may be appropriate. The two properties can coexist, but document why both contribute to the same distance so the next change does not accidentally double it.
Text spacing is a separate control
letter-spacing adjusts spacing between characters; word-spacing adjusts spacing between words. Neither replaces the gap between cards or padding around a block. For example, a small positive letter-spacing on a short uppercase label may improve its appearance, while a large value can make ordinary text difficult to read. Negative values can cause characters or words to crowd or overlap.
Test text at the fonts and sizes your page actually uses, including zoomed views. The spacing of CSS text is influenced by font metrics and available width, so a value that looks balanced in a heading may be distracting in a paragraph. Leave normal text spacing alone unless a visible typographic problem calls for a change.
Inspect, then choose
To inspect spacing in your CSS, select a card in DevTools and open the Computed or Layout view to inspect its box model. Hover over the padding and margin areas, then select the parent flex container and enable its layout overlay. Compare the declared gap with the visible distance; check sibling margins and alignment if they differ. Toggle a declaration in Styles to see exactly which boundary moves.
For this card row, use padding to move text away from each card border, gap to separate the repeated cards, and margin to position the group among surrounding sections. If the problem is inside a line of text, consider letter or word spacing instead. That location-based choice makes spacing rules easier to explain and change.
