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) and that you have met the spacing scale, where one number such as 4 maps to a length (1rem in the default theme, because each step is 0.25rem). Here you apply that scale to padding and margin.
The basic pattern
Padding is the space inside an element's border. Margin is the space outside it. Tailwind names both with a short prefix followed by a scale value.
| Class | CSS |
|---|---|
| `p-4` | `padding: 1rem` |
| `m-4` | `margin: 1rem` |
| `p-0` | `padding: 0` |
| `p-px` | `padding: 1px` |
Axis and side variants
Add a letter after p or m to target part of the box.
| Letter | Meaning | Example | CSS |
|---|---|---|---|
| `x` | left and right | `px-6` | `padding-left` and `padding-right: 1.5rem` |
| `y` | top and bottom | `py-4` | `padding-top` and `padding-bottom: 1rem` |
| `t` | top | `mt-8` | `margin-top: 2rem` |
| `r` | right | `pr-2` | `padding-right: 0.5rem` |
| `b` | bottom | `mb-4` | `margin-bottom: 1rem` |
| `l` | left | `ml-3` | `margin-left: 0.75rem` |
<div class="mx-auto mt-8 -mb-2 px-6 py-4">
Content
</div>This block has 1.5rem of horizontal padding and 1rem of vertical padding. It has 2rem of top margin and is centered horizontally. Its -mb-2 is explained below.
Auto margins
mx-auto sets margin-left and margin-right to auto. In a normal block layout, the browser then splits the leftover horizontal space equally, which centers the element. This only works when the element has a width narrower than its parent, for example max-w-md. A block element with no width limit already fills the parent, so there is no space to split.
In a flex container, ml-auto pushes an item to the far right, because the auto margin absorbs all free space on that side.
Negative margins
Put a dash before the class name to get a negative margin: -mb-2 is margin-bottom: -0.5rem. Negative margins pull an element toward its neighbor or outside its parent's box. They exist for margins only. Padding cannot be negative in CSS, so -p-4 does not exist.
<div class="p-6">
<img src="banner.jpg" alt="Banner" class="-mx-6 w-auto max-w-none" />
</div>The parent adds 1.5rem of padding on each side. The image's -mx-6 cancels it, so the image reaches the parent's edges.
Common errors
- Using
mx-autoon an element with no width limit. Nothing visibly changes. - Expecting vertical margins to always add up. Adjacent block margins can collapse into the larger of the two, and Tailwind does not change that browser behavior.
- Setting
p-4andpx-8together and assuming source order decides. Both are utility classes of similar specificity, and Tailwind's generated stylesheet ordersp-*beforepx-*, so the more specific axis class wins. Avoid the conflict by not combining them on the same element when you can. - Writing
m-[-4]. Use-m-4for scale values. Square brackets take real CSS values, such asm-[-4px].
Exercise
A card needs 1rem of padding on all sides, then the card must be centered in a wide page and have 2rem of space above it. The card has class max-w-sm. Write the classes to add.
Show answer
p-4 mx-auto mt-8. The p-4 gives 1rem of padding, mx-auto centers the card because max-w-sm limits its width, and mt-8 gives 2rem of top margin.