Lesson 35 of 52Tailwind CSS
Tailwind Sizing and Spacing
The spacing scale as a system, padding, margin and gap, widths and heights, readable line lengths, images, borders, radius and shadows, ending in a card built step by step.
Ask a designer what separates a tidy page from a messy one and they will often say "spacing". The text and colours can be identical, but if the gaps between things are random, the page feels off. Tailwind gives you a spacing scale so every gap, padding and width comes from the same small set of steps. This lesson treats that scale as a system, then covers sizing elements (widths, heights, readable text columns, images) and the finishing touches of borders, rounded corners and shadows. At the end you will build a card one layer at a time and see how each class earns its place.
The spacing scale is one variable
In version 4, every spacing utility is calculated from a single theme variable:
@theme {
--spacing: 0.25rem; /* the default: 4px */
}p-4 becomes padding: calc(var(--spacing) * 4), which is 1rem or 16px. Any whole number works (p-13 is fine in version 4), and so do halves like p-0.5, p-1.5 and p-2.5 for fine adjustments. p-px is exactly 1px.
Because everything multiplies one variable, you can make a whole interface more compact or more airy by changing that one value. Try it:
Preview
Change --spacing to 0.2rem, then 0.35rem. Every padding, margin and gap moves together, and the proportions stay intact. You will rarely change this on a real project, but it shows why the scale works: sizes are related, not random.
In practice, a page uses only a handful of steps. A good default rhythm: 1 to 2 inside small controls, 3 to 4 between related items, 6 to 8 inside cards, 12 to 24 between page sections. Larger gaps signal "these things are separate", smaller gaps signal "these belong together". That grouping is how readers understand a layout before they read a word.
With the default spacing of 0.25rem and a 16px root font size, how many pixels of padding does each side of this box get? Type a number.
<div class="p-13">
Padding, margin and gap
The box model from earlier in the course maps neatly onto classes.
Margin pushes neighbours away. mx-auto centres a block that has a width.
| Pattern | Sides | Example |
|---|---|---|
p-*, m-* | all four | p-6 |
px-*, py-* | inline (left and right), block (top and bottom) | px-4 py-2 |
pt-*, pr-*, pb-*, pl-* | one physical side | mt-8 |
ps-*, pe-*, ms-*, me-* | start and end, which flip for right-to-left languages | ms-2 |
gap-*, gap-x-*, gap-y-* | between flex or grid children | gap-x-6 gap-y-2 |
Use the start and end versions (ms-2 instead of ml-2) for things like an icon beside a label, so the layout mirrors correctly in Arabic or Hebrew.
Prefer gap over space-x and space-y
There are two ways to put space between children. space-y-4 adds a margin to every child except the last. gap-4 on a flex or grid parent uses the CSS gap property. Both look identical in a simple column, so why prefer gap?
Preview
With space-x-3 the wrapped rows touch and the tag at the end of a line still carries a margin, because margins know nothing about lines. With gap-3 every tag is spaced evenly in both directions. gap also lives on the parent only, so a hidden child or a reordered item never leaves a stray margin. Reach for space-y only in simple stacks that are not flex or grid, such as paragraphs inside an article.
flex flex-wrap space-x-2. When the tags wrap onto a second line, they touch the line above. What is the best fix?Negative margins
Put a minus sign before a margin class to pull an element the other way: -mt-12 is margin-top: calc(var(--spacing) * -12). It is the classic way to overlap things, like an avatar half over a banner:
Preview
Change -mt-10 to mt-4 to see the avatar drop below the banner. The white border on the avatar hides the seam. Use negative margins sparingly; when you find yourself fighting with many of them, grid or positioning is usually cleaner.
Width and height
| Class | CSS | Use it for |
|---|---|---|
w-64 | width: 16rem | a fixed sidebar |
w-1/2, w-2/3 | width: 50%, 66.67% | fractions of the parent |
w-full | width: 100% | fill the parent |
w-screen | width: 100vw | the whole viewport (rarely needed) |
w-fit | width: fit-content | shrink to the content |
size-10 | width and height 2.5rem | icons, avatars, squares |
h-screen | height: 100vh | older full-height pattern |
min-h-dvh | min-height: 100dvh | a page that fills the screen |
Two notes. w-screen is a common trap: 100vw includes the scrollbar on desktop browsers, so it can cause a sideways scroll. w-full is almost always what you want. And on phones, 100vh is measured with the browser toolbar hidden, so a h-screen section can be taller than what you actually see. The dynamic viewport unit dvh follows the toolbar as it appears and disappears, so min-h-dvh is the modern choice for full-height layouts. It works in all current browsers.
Match each class to its job.
Readable widths and the container pattern
Long lines are tiring: the eye loses its place jumping back to the start. Aim for about 60 to 75 characters per line. max-w-prose (65ch) does exactly that for text. For layout, the max-w-* scale runs from max-w-xs (20rem) through max-w-md (28rem), max-w-2xl (42rem) and max-w-5xl (64rem) up to max-w-7xl (80rem).
Nearly every site wraps its content in the same three-class pattern:
<div class="mx-auto max-w-5xl px-4">...</div>max-w-5xlstops the content growing wider than 64rem on large screens.mx-autocentres that box by splitting the leftover space into equal margins.px-4keeps text off the screen edges on phones, where the max width is never reached.
Tailwind also has a container class, but in version 4 it only sets a width and a max width per breakpoint. It does not centre or add padding on its own, so most people write the explicit pattern above.
Images: aspect ratio and object-fit
Images from users come in every shape. To make a grid of cards line up, give the image box a fixed aspect ratio and tell the image how to fill it. aspect-video is 16:9, aspect-square is 1:1, and version 4 accepts any ratio like aspect-3/2. object-cover fills the box and crops the overflow; object-contain fits the whole image inside, leaving empty space.
Preview
The source image is twice as wide as it is tall. object-cover keeps the circle round and crops the sides, object-contain shows it all with grey bands, and object-fill squashes it into an oval. Change all three to aspect-video. For real photos, object-cover is almost always the right choice.
Borders, radius and shadows
- Borders.
borderis 1px,border-2andborder-4are thicker, andborder-torborder-bdraw one side.divide-ydraws a line between stacked children, perfect for lists. Always give a colour:border-slate-200. - Radius.
rounded-sm(4px),rounded-md,rounded-lg(8px),rounded-xl,rounded-2xl(16px) androunded-fullfor circles and pills. Pick one or two radii for a project and stick to them. Nested corners look best when the inner radius is smaller than the outer one. - Shadows.
shadow-xstoshadow-2xllift an element off the page. Tint them with a colour and opacity, likeshadow-lg shadow-sky-500/20, for a softer glow on coloured backgrounds. A shadow is decoration, so pair it with a border or background to keep the edge visible in high contrast modes.
A card, built step by step
Here is a recipe card. Read the comments for the order the classes were added, then experiment.
Preview
Experiments: remove overflow-hidden and see the top image poke out of the rounded corners. Change gap-3 to gap-6, then gap-1, and decide which feels right. Swap rounded-2xl for rounded-none and the whole mood changes. Move w-full off the button so it shrinks to its text.
Put the steps of building a card in a sensible order.
- Tap the steps below in the right order.
- --spacing
- The theme variable every spacing utility multiplies. 0.25rem (4px) by default.
- gap
- Space between flex or grid children, set on the parent. Preferred over margin-based
space-*. - logical properties
- Start and end sides (
ms-*,pe-*) that flip automatically for right-to-left languages. - dvh
- Dynamic viewport height. Tracks the visible screen on phones as browser toolbars appear and hide.
- container pattern
mx-auto max-w-5xl px-4: a centred, width-limited content column with side padding.- aspect ratio
- The width-to-height shape of a box, such as
aspect-video(16:9) oraspect-square. - object-cover
- Makes an image fill its box and crops the overflow, keeping its proportions.