Sub topic 3
Padding and margin: p-, m-, axis and side variants, negative and auto margins
This chapter assumes you know the CSS box model (content, padding, border, margin), can read px, rem, and % values, and have a project where Tailwind classes already work. It also assumes you have seen the spacing scale, where one number maps to one length. By default, 1 equals 0.25rem, so 4 is 1rem (16px in most browsers).
The core idea
Padding is space inside an element, between its content and its border. Margin is space outside the border, between the element and its neighbors. Tailwind gives each one a family of utilities built from three parts:
- A property letter:
pfor padding,mfor margin. - An optional side or axis letter.
- A value from the spacing scale, or a special keyword.
So p-4 means padding: 1rem, and m-2 means margin: 0.5rem.
Axis and side variants
Without a side letter, a utility applies to all four sides. Add a letter to narrow it.
| Letter | Applies to | CSS properties set |
|---|---|---|
| (none) | all four sides | `padding` / `margin` |
| `x` | left and right | `padding-inline` / `margin-inline` |
| `y` | top and bottom | `padding-block` / `margin-block` |
| `t` | top | `padding-top` / `margin-top` |
| `r` | right | `padding-right` / `margin-right` |
| `b` | bottom | `padding-bottom` / `margin-bottom` |
| `l` | left | `padding-left` / `margin-left` |
| `s` | inline start | `padding-inline-start` / `margin-inline-start` |
| `e` | inline end | `padding-inline-end` / `margin-inline-end` |
The exact CSS output for x and y differs by version. Tailwind v3 emits padding-left plus padding-right, and v4 emits the logical properties. The visual result in a left-to-right layout is the same.
The s and e utilities follow the writing direction, which matters if you support right-to-left languages such as Arabic. They need Tailwind v3.3 or later. In a left-to-right page, ps-4 pads the left side, and in a right-to-left page it pads the right side. The physical l and r utilities never flip.
<div class="mx-auto mt-8 -mb-2 px-6 py-4">
Content
</div>Reading this class list piece by piece:
px-6sets left and right padding to1.5rem.py-4sets top and bottom padding to1rem.mt-8sets top margin to2rem.-mb-2sets bottom margin to-0.5rem.mx-autosets left and right margin toauto.
Combining general and specific utilities
You can mix a broad utility with a narrow one to describe a shape in few classes. For example, px-6 pt-2 gives 1.5rem on the sides and 0.5rem on top, with no bottom padding.
Be careful about relying on class order inside the class attribute. It has no effect. What decides the winner is the order of rules in the generated stylesheet, which you do not control. If you write p-4 px-8, the result is usually what you expect, because Tailwind sorts its output so that axis utilities come after the all-sides utility. Conflicts between two utilities of the same kind, such as p-4 p-8, are unpredictable. Pick one.
Special values
Besides numbers from the scale, a few keywords are available.
| Class | CSS | Notes |
|---|---|---|
| `p-0`, `m-0` | `0` | Removes the space. |
| `p-px`, `m-px` | `1px` | A single pixel, useful for hairline offsets. |
| `m-auto` | `margin: auto` | Margin only. |
| `mx-auto` | left and right `auto` | Centers a block with a constrained width. |
| `p-0.5`, `p-1.5`, `p-2.5`, `p-3.5` | `0.125rem`, `0.375rem`, `0.625rem`, `0.875rem` | Half steps exist only for small values. |
auto exists only for margin. Padding cannot be auto in CSS, so p-auto does nothing and Tailwind does not generate it.
Negative margins
Prefix a margin utility with a minus sign to pull an element in the opposite direction. -mt-4 sets margin-top: -1rem, which moves the element up and can make it overlap what is above it. -mx-6 extends an element 1.5rem past its container on both sides.
Negative values are a margin feature only. CSS does not allow negative padding, so -p-4 does not exist.
A common use is letting a child break out of a padded parent, for example a full-bleed image inside a card:
<div class="mx-auto max-w-md p-6 shadow">
<img src="/cover.jpg" alt="Cover" class="-mx-6 -mt-6 mb-4 max-w-none" />
<p>Text stays inside the padding.</p>
</div>The image's negative margins cancel the card's p-6 on the top and sides. Note max-w-none. Without it, Tailwind's base styles cap images at max-width: 100%, so the extra 3rem of width would be refused and the image would not reach the edges. Check this if a negative-margin element refuses to grow.
Centering with auto margins
mx-auto works through the CSS rule that auto side margins split the free horizontal space equally. Two conditions must hold:
- The element must have a width smaller than its container. A block element with no width already fills the line, so there is no free space and
mx-autoseems to do nothing. - The element must be a block-level box (or a flex or grid item). It will not center an
inlineelement such as aspan.
<div class="mx-auto w-64 bg-gray-100 p-4">
Centered, 16rem wide
</div>Auto margins also work inside a flex container, where they absorb free space on the main axis. This is a handy way to push one item away from the others:
<nav class="flex items-center gap-4">
<a href="/">Home</a>
<a href="/docs">Docs</a>
<a href="/login" class="ml-auto">Log in</a>
</nav>Here ml-auto takes all remaining space to the left of the last link, so it sits at the right edge.
Margin collapse and other gotchas
Vertical margins of adjacent block elements can collapse. If one paragraph has mb-4 and the next has mt-6, the gap is 1.5rem (the larger one), not 2.5rem. Margins do not collapse in flex or grid containers, and padding never collapses.
A second surprise is that percentage padding and margin resolve against the width of the containing block, even for top and bottom. That matters only with arbitrary values such as pt-[56.25%], since the default scale uses rem.
Third, padding adds to the size of an element unless box-sizing: border-box is set. Tailwind's preflight base styles set border-box on every element, so w-64 p-4 still produces a 16rem-wide box, with the padding inside it. Margin is always outside the box.
Responsive variants
Spacing utilities accept any variant prefix. Unprefixed classes apply at all widths, and a prefix such as md: applies from that breakpoint upward:
<section class="p-4 md:p-8 lg:p-12">
<p class="text-lg md:text-xl">Padding and text grow at larger breakpoints.</p>
</section>Below md the padding is 1rem, from md it is 2rem, and from lg it is 3rem. Negative margins take the prefix before the minus sign: md:-mt-4.
Choosing padding or margin
| Question | Use |
|---|---|
| Should the space show the element's background color? | Padding |
| Should clicks in the space hit the element? | Padding |
| Should the space separate this element from siblings? | Margin (or `gap` in flex and grid) |
| Do you need to center a fixed-width block? | `mx-auto` |
| Do you need to overlap or pull an element out? | Negative margin |
A button is a good example. Its padding makes the clickable area larger, and its margin only keeps neighbors away. For spacing between many siblings, the next chapter's gap and space-y utilities usually beat putting a margin on every child.
Common mistakes
- Using
mx-autoon an element without a width ormax-w-*. Nothing visible changes. - Writing
p-autoor-p-4. Neither exists. - Using
mt-4on a flex child when the parent could usegap-4. - Adding
ml-4for an RTL-supporting app. Usems-4. - Building class names dynamically, such as `
mt-${n}. Tailwind scans source files for complete class strings, so the generated class may never be created. Write the whole name, for example in a lookup object that mapsnto"mt-4"`.
Exercise
A card has p-6. You want its header image to touch the card's left, right, and top edges. Which classes do you put on the image, and what extra class may be needed?
Show answer
Use -mx-6 -mt-6 to cancel the padding on the sides and top. Add max-w-none if the image is capped by the base max-width: 100% rule, so that it can grow wider than its parent's content box.
Exercise
You wrote <span class="mx-auto w-40">Label</span> and it does not center. Why, and what is the smallest fix?
Show answer
A span is inline, so width and auto margins do not apply as they do for block boxes. Add block (or inline-block with a centered parent using text-center). <span class="mx-auto block w-40"> centers it.