Tailwind CSS Basics: Style with Utility Classes
Spacing and Sizing
In this lesson you will
- Use padding, margin and gap utilities
- Convert spacing numbers to real units
- Size elements with width, height, fractions and max-width
Introduction
Almost every layout depends on space: space inside elements (padding), between elements (margin and gap) and the size of elements themselves. Tailwind gives you one spacing scale for all of them, which keeps your designs consistent.
The spacing scale
Spacing utilities use a number. The number multiplies the base spacing, which is 0.25rem by default (I checked: --spacing: 0.25rem in the generated CSS).
| Number | Value | Pixels at 16px base |
|---|---|---|
| 1 | 0.25rem | 4px |
| 2 | 0.5rem | 8px |
| 4 | 1rem | 16px |
| 8 | 2rem | 32px |
| 16 | 4rem | 64px |
So p-4 means padding of 1rem. Because the scale is a multiplier, any number works, such as p-5 or p-7.
Padding and margin
| Class | Effect |
|---|---|
p-4 | padding on all sides |
px-4 | left and right padding |
py-2 | top and bottom padding |
pt-4, pr-4, pb-4, pl-4 | one side |
m-4, mx-auto, mt-2, -mt-4 | margin, centring with auto, negative margin |
HTML
<button class="rounded-lg bg-blue-600 px-4 py-2 text-white">
Save
</button>mx-auto on a block element with a set width centres it horizontally. A leading minus sign makes a negative margin. I compiled -mt-4 and it generated a negative margin from the same scale.
Quick quiz
How much padding does px-8 add on the left and right with default settings?
Gap
gap creates space between children of a flex or grid container. It is usually better than adding margins to every child.
HTML
<div class="flex gap-4">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>gap-x-4 and gap-y-2 set horizontal and vertical gaps separately.
Width and height
| Class | Meaning |
|---|---|
w-64 | width of 16rem (64 times 0.25rem) |
w-1/2 | half of the parent, compiled as calc(1 / 2 * 100%) |
w-full | 100% of the parent |
w-screen | 100% of the viewport width |
h-dvh | 100% of the dynamic viewport height |
size-10 | width and height both 2.5rem |
min-h-screen | at least the viewport height |
size-10 is a shortcut for squares such as avatars and icons. It replaces w-10 h-10.
Maximum widths and readability
Long lines of text are hard to read. max-w-prose limits width to a comfortable reading measure, and sizes such as max-w-md, max-w-2xl and max-w-7xl come from the theme. A common page container is:
HTML
<main class="mx-auto max-w-7xl px-4">
...
</main>The container class also exists. It sets width: 100% and a different max-width at each breakpoint, but most designs use mx-auto max-w-7xl px-4 because it is easier to control.
Space between children
space-x-4 and space-y-4 add space between children, and divide-y adds lines between them. They work, but gap on a flex or grid parent is simpler and handles wrapping better.
Common mistakes
- Using
marginfor the space between flex children instead ofgap. - Forgetting that padding adds to the element's size unless Tailwind's border-box sizing is in effect. Preflight sets
box-sizing: border-boxon everything, so padding is included inw-*widths. - Using fixed pixel widths that break on small screens. Prefer fractions,
max-w-*and responsive classes (covered later).
Try it yourself
Build a card: mx-auto max-w-sm rounded-xl bg-white p-6 shadow-sm. Inside, stack a title and paragraph with mt-2 between them, and a button with mt-4 px-4 py-2. Then change the spacing numbers and see how the layout changes.
Summary
- One spacing scale drives padding, margin, gap, width and height, and each step is 0.25rem by default.
- Use
p,m,gap,w,handsizeutilities with number steps, fractions and keywords likefullandscreen. - Prefer
gapover margins between flex and grid children, andmax-w-*withmx-autofor readable page widths.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading