Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท Utility Basics

Spacing: Padding, Margin and Gap

Master Tailwind v4 spacing: the 0.25rem scale, padding and margin utilities for every side, negative margins, space-x and space-y, and gap.

Spacing is the single most common thing you do in Tailwind. Padding pushes content away from an element's own edge, margin pushes neighbors away, and gap spaces the children of a flex or grid container. All of them share one number scale, so once you learn it you can read and write spacing everywhere.

The spacing scale

Tailwind v4 is based on one variable, --spacing: 0.25rem (4px at the default font size). A spacing utility multiplies it by the number you write:

Class Value Pixels
p-1 0.25rem 4px
p-2 0.5rem 8px
p-4 1rem 16px
p-6 1.5rem 24px
p-8 2rem 32px
p-12 3rem 48px
p-px 1px 1px

In v4 any number works because it is just a multiplication: p-13, p-22, p-3.5 are valid and follow the same rule. In v3 only a fixed list of values existed. Mental shortcut: multiply by 4 to get pixels, divide by 4 to get rems.

The spacing scale as bars

The last bar, w-13 (3.25rem), is not in the v3 scale but works in v4. The same scale powers width, height, margin, padding, gap, inset and translate.

Padding and margin

Both follow the pattern {side}-{size}. The property is p or m. The side letter picks the edges:

Letter Edges Example
none all four p-4
x left and right px-4
y top and bottom py-2
t r b l one side pt-6, ml-3
s e start and end (logical) ps-4, me-2

s and e follow the text direction, so ms-4 means left margin in English and right margin in Arabic or Hebrew. Prefer them when your site supports right-to-left languages.

Margin has two extras. auto soaks up free space, and negative values pull an element toward or over its neighbor:

  • mx-auto centers a block that has a width.
  • ml-auto pushes an element to the right inside a flex row.
  • -mt-4 moves an element up by 1rem, and -mx-6 extends a child beyond its parent's padding.
Padding, margin and negative margin

Notice how p-2 makes the box bigger and its background grows with it, while margin never shows a background: it is outside the box.

Margin collapsing and the one-direction rule

Vertical margins between two stacked blocks collapse: the larger of the two wins instead of adding up. Preflight keeps that standard behavior. To avoid surprises, apply spacing in one direction: for example give every element mt-* and never mb-*. Better still, let the parent control spacing with space-y-* or gap-*, so children do not need margins at all.

space-x and space-y

space-y-4 on a parent adds 1rem of top margin between every child, except the first. space-x-4 does the same horizontally. They are perfect for stacks of items:

HTML
<ul class="space-y-3">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
</ul>

Use space-x-reverse or space-y-reverse when you reverse a flex direction with flex-row-reverse. Limit them to simple blocks: space-* uses margins, so it behaves oddly with flex-wrap and hidden elements. For flex and grid, use gap instead.

gap

gap-* sets the space between children of a flex or grid container. It never adds space on the outside edges, never fights margin collapse and works when items wrap. You can set each axis on its own with gap-x-* and gap-y-*:

gap versus space

divide: lines between children

A close relative: divide-y puts a 1px border between stacked children, divide-x between side-by-side ones. Pair it with a color such as divide-slate-200. It is the cleanest way to draw a list with separators, and you will use it again in Borders, Rings and Outlines.

Choosing spacing in practice

  • Inside a component: padding (p-4, px-6 py-3).
  • Between siblings in a stack: space-y-* or a flex flex-col gap-*.
  • Between items in a row or grid: gap-*.
  • Centering a block: mx-auto.
  • Responsive spacing: add a variant, for example p-4 md:p-8. Chapter 5 covers responsive variants.

Pick a few scale steps for your project (such as 2, 4, 6, 8, 12, 16) and stick to them. Consistency looks more professional than perfect numbers.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How many pixels is p-6 at the default font size?

    One step is 0.25rem = 4px, so 6 steps are 1.5rem = 24px.
  2. 2. Which class centers a block that has a fixed width?

    mx-auto sets left and right margins to auto, which centers a block with a width. text-center only aligns inline content.
  3. 3. Which pair of classes puts 1rem of space between flex items without adding space around the outside?

    gap only applies between children of a flex or grid container.
  4. 4. What does -mt-4 do?

    A leading minus gives a negative value, which pulls the element upward.
  5. 5. Why prefer ps-4 over pl-4 in a multilingual site?

    ps (padding-inline-start) flips automatically in right-to-left languages.

Summary

  • One scale rules spacing: a number times 0.25rem, and in v4 any number is valid.
  • p/m take x, y, t, r, b, l, s, e; mx-auto centers; negative margins pull elements.
  • Prefer gap-* for flex and grid; use space-x/y-* for simple stacks and divide-* for separators.
  • Space in one direction and let the parent control spacing.

Next: Sizing: Width and Height