CardKit
Small tools for website editing and template work

BLOG

CSS Padding vs. Margin vs. Gap: The Visual Explanation You Actually Need

All three properties add space. That is exactly the problem: from the outside they look interchangeable, and choosing the wrong one produces either nothing at all or a page where everything shifted by 24 pixels.

Start from the complaint, not the definition

Nobody wakes up wanting to understand the box model. People arrive at this topic because something on their site looks wrong:

  • "My card feels cramped - the text is touching the edges."
  • "There is too much air between my sections, but not enough between my headings."
  • "I changed the spacing and every page moved, not just this one."
  • "I set the gap on my grid and nothing happened."

All four are spacing problems, and each one has a different culprit. The definitions matter only because they tell you which value to change. So here they are, defined by what you see rather than by what they are.

The picture to keep in your head

Every element on a page is a box. Three kinds of space can be added to it, and they sit in different places:

        MARGIN       space OUTSIDE the box, pushing neighbours away
   +-----------------------------------+
   |          BORDER                   |
   |   +---------------------------+   |
   |   |        PADDING            |   |  space INSIDE the box,
   |   |   +-------------------+   |   |  around the content
   |   |   |     CONTENT       |   |   |
   |   |   +-------------------+   |   |
   |   +---------------------------+   |
   +-----------------------------------+

   GAP        space BETWEEN sibling boxes in a grid or flex row
              (it belongs to the parent, not to the children)

The single most useful sentence on this page: padding grows the box, margin moves the box, gap spaces the children.

Padding: what you see

More room inside the card. Text moves away from the edges, and the card gets bigger in both directions. If the card has a background colour or border, that colour extends into the new space - this is the tell that tells padding apart from margin at a glance.

PADDING 8PXtight, text nearly touches the edge PADDING 28PXthe card grows, colour extends with it

Studio visit

Two hours, materials included. Small groups of six.

Book

Studio visit

Two hours, materials included. Small groups of six.

Book

Notice the coloured area grew, not just the whitespace. That is padding.

Padding is also what you want when a clickable target feels too small. A button with padding: 6px 10px is legally a button and practically a missed tap on a phone; the same button at padding: 14px 28px is comfortable, and it is still one line of CSS.

Margin: what you see

More room outside the card. The card itself does not change size - it is pushed away from whatever sits next to it. The background colour stops exactly where it did before.

MARGIN 4PXcards crowd each other MARGIN 24PXcards unchanged, distance grew
Card one
Card two
Card three
Card one
Card two
Card three

Same cards, same size. Only the distance changed. That is margin.

Two margin behaviours catch people out, and both are worth knowing before you reach for it:

  • Vertical margins collapse. Put 30px under one element and 30px above the next, and you get 30px between them, not 60px. The browser keeps the larger of the two. This is why "I set the margin and the gap doubled" sometimes never happens - and why it suddenly does when you switch to padding.
  • Margin can be negative. margin-top: -20px pulls an element upward. It is the standard way to tuck a heading over an image, and the fastest way to make a layout that breaks on a different screen size.

Gap: what you see

Even space between the children of a grid or flex container - and nowhere else. Gap never adds space at the outer edges, which is precisely why it is the right tool for grids: you get consistent rows and columns without the classic "first card has a stray margin" problem.

The catch is that gap only works on a parent that is actually a grid or a flex container. Set it on a plain div and nothing happens. So:

.my-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 24px;          /* the space between the cells */
  padding: 24px;      /* the space inside the container's own edges */
}

That pairing - gap for between, padding for around - is the pattern behind almost every tidy card grid you have ever admired on someone else's site.

The four failure patterns, and which value is wrong

1. "My card still feels cramped" - padding is too small

The text is too close to the border. This is padding, nearly always. If the coloured or bordered area also looks too tight against its neighbours, that second problem is margin or gap, and it is a separate fix. People often bump the padding until the outer spacing looks right, which leaves the card enormous and the layout worse.

2. "Everything moved, not just this" - margin on a shared class

You added margin-top: 40px to a class that appears on every page, and every page gained 40px. Padding would have had the same reach, so the real lesson is not padding versus margin - it is that the selector determines the blast radius, not the property. This is the same scope problem covered in Why Copy-Pasting CSS Into Squarespace Sometimes Breaks Your Site.

3. "I set the gap and nothing happened" - the parent is not a grid

gap is silently ignored on a block container. Add display: grid (or display: flex) to the parent first. If the parent is not yours to edit - common in a Squarespace theme - fall back to margin on the children, and accept that the last child will have a stray margin you may want to remove.

4. "The spacing is wrong on mobile only" - one value, two screen sizes

32px of section padding is generous on a desktop and enormous on a phone. The honest fix is a smaller value inside a media query, or a relative unit like clamp(). CardKit does not write responsive rules for you; what it does is let you see the section at desktop width before you decide whether a smaller mobile value is even needed.

The decision table

When you are staring at a spacing problem, work down this list:

  • Text touching the edge of a box? Padding on that box.
  • Box too close to the box next to it? Gap if they are grid or flex children; margin if they are not.
  • Uneven space between stacked items? Margin is fighting collapsing. Gap on a flex column is cleaner and never collapses.
  • Whole section too close to the section above? Padding on the section (top/bottom) rather than margin on the heading - sections are containers, and container padding survives theme changes better.
  • Something is too far from everything and you cannot find why? Look for margin on a shared class, and for a negative margin you forgot about.

Real scenarios

The Etsy seller with the crowded product grid. Her three-column grid had margin: 10px on every card, which meant 20px between cards but only 10px at the outer edges, and the row of cards was visibly off-centre. Replacing the card margins with gap: 20px on the grid and padding: 20px on the container made every gap identical in one change.

The consultant whose pricing cards looked cheap. He had generous margin between cards and almost no padding inside them, so the text sat against the border while the cards floated in a sea of whitespace. Swapping the ratio - less margin, more padding - cost four minutes and changed the impression completely.

The blogger who could not fix her heading spacing. She kept adding margin-top to her headings and the spacing refused to grow, because the paragraph above had a larger bottom margin and the two were collapsing. Moving to gap on the text column gave her exactly the number she typed.

Questions people ask

Why did my 30px margin produce less than 30px of visible space?

Adjacent vertical margins collapse to the larger of the two. Add margin to only one side of the pair, or use gap inside a flex column, and the number you type is the number you get.

Can I use padding instead of margin to be safe?

Often yes, and it is more predictable, because padding never collapses and never goes negative. The difference is visual: padding grows the coloured area of the box. If the box has no background and no border, the two look identical and padding is the safer habit.

Is gap supported everywhere now?

Yes, in every current browser, for both grid and flex. The only reason to avoid it is if you must support very old browsers, which for a small business site in 2026 is rarely a real constraint.

Do I need to learn the box model properly?

Not to make these changes. You need the one picture at the top of this page and the sentence underneath it. Everything else can be discovered by changing a value and looking.

The short version

Padding grows the box. Margin moves the box. Gap spaces the children. Most spacing complaints are a mismatch between the space you wanted to change and the property you changed - and once you can see which is which, the fix is one number.

If you want to see it happen rather than read about it, paste the section into the CSS Code Editor, change the value and watch the box. For the rest of the values that matter visually - radius, shadow, line-height, width - see What Those CSS Numbers Actually Do, and for the order to make changes in, see A Safer Workflow for Editing Squarespace CSS.