Skip to content

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.

18 min read0/4 questions

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:

HTML + Tailwind
<style type="text/tailwindcss"> @theme { --spacing: 0.25rem; }</style><div class="max-w-xs rounded-xl border border-slate-200 p-6"> <h2 class="text-lg font-semibold text-slate-900">Weekly digest</h2> <p class="mt-2 text-sm text-slate-600">Three new recipes and one kitchen trick.</p> <div class="mt-4 flex gap-3"> <button class="rounded-lg bg-slate-900 px-4 py-2 text-sm text-white">Read</button> <button class="rounded-lg border border-slate-300 px-4 py-2 text-sm">Later</button> </div></div>

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.

Predict the outputHTML

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">
Need a nudge? Hints walk you through it.

Padding, margin and gap

The box model from earlier in the course maps neatly onto classes.

One element's box, from outside in
  1. m-4 / mt-8 / mx-automargin: space outside the border
  2. border / border-2the border itself
  3. p-6 / px-4 py-2padding: space inside the border
  4. w-64 / max-w-md / h-40the content box size

Margin pushes neighbours away. mx-auto centres a block that has a width.

1/4
PatternSidesExample
p-*, m-*all fourp-6
px-*, py-*inline (left and right), block (top and bottom)px-4 py-2
pt-*, pr-*, pb-*, pl-*one physical sidemt-8
ps-*, pe-*, ms-*, me-*start and end, which flip for right-to-left languagesms-2
gap-*, gap-x-*, gap-y-*between flex or grid childrengap-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?

HTML + Tailwind
<p class="text-sm font-semibold text-slate-700">space-x-3 with wrapping</p><div class="mt-2 flex w-56 flex-wrap space-x-3 border border-dashed border-slate-300 p-2"> <span class="rounded-full bg-sky-100 px-3 py-1 text-sm">pasta</span> <span class="rounded-full bg-sky-100 px-3 py-1 text-sm">vegetarian</span> <span class="rounded-full bg-sky-100 px-3 py-1 text-sm">quick</span> <span class="rounded-full bg-sky-100 px-3 py-1 text-sm">budget</span></div> <p class="mt-6 text-sm font-semibold text-slate-700">gap-3 with wrapping</p><div class="mt-2 flex w-56 flex-wrap gap-3 border border-dashed border-slate-300 p-2"> <span class="rounded-full bg-sky-100 px-3 py-1 text-sm">pasta</span> <span class="rounded-full bg-sky-100 px-3 py-1 text-sm">vegetarian</span> <span class="rounded-full bg-sky-100 px-3 py-1 text-sm">quick</span> <span class="rounded-full bg-sky-100 px-3 py-1 text-sm">budget</span></div>

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.

Quick check
A row of tags uses flex flex-wrap space-x-2. When the tags wrap onto a second line, they touch the line above. What is the best fix?
Need a nudge? A hint rules out a wrong answer.

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:

HTML + Tailwind
<article class="max-w-xs overflow-hidden rounded-2xl border border-slate-200"> <div class="h-24 bg-linear-to-r from-teal-400 to-sky-500"></div> <div class="px-5 pb-5"> <div class="-mt-10 flex size-20 items-center justify-center rounded-full border-4 border-white bg-amber-200 text-xl font-bold text-amber-900"> JK </div> <h2 class="mt-3 text-lg font-semibold">Jonas Kim</h2> <p class="text-sm text-slate-500">Bakes bread, fixes bikes</p> </div></article>

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

ClassCSSUse it for
w-64width: 16rema fixed sidebar
w-1/2, w-2/3width: 50%, 66.67%fractions of the parent
w-fullwidth: 100%fill the parent
w-screenwidth: 100vwthe whole viewport (rarely needed)
w-fitwidth: fit-contentshrink to the content
size-10width and height 2.5remicons, avatars, squares
h-screenheight: 100vholder full-height pattern
min-h-dvhmin-height: 100dvha 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 the pairs0/5 matched

Match each class to its job.

Choose an item, then its match on the other side.

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-5xl stops the content growing wider than 64rem on large screens.
  • mx-auto centres that box by splitting the leftover space into equal margins.
  • px-4 keeps 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.

HTML + Tailwind
<div class="grid grid-cols-3 gap-3"> <figure> <img class="aspect-square w-full object-cover" alt="A red circle on orange" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='600' height='300'%3E%3Crect width='600' height='300' fill='%23f59e0b'/%3E%3Ccircle cx='300' cy='150' r='110' fill='%23be123c'/%3E%3C/svg%3E" /> <figcaption class="mt-1 text-xs text-slate-500">object-cover</figcaption> </figure> <figure> <img class="aspect-square w-full bg-slate-100 object-contain" alt="A red circle on orange" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='600' height='300'%3E%3Crect width='600' height='300' fill='%23f59e0b'/%3E%3Ccircle cx='300' cy='150' r='110' fill='%23be123c'/%3E%3C/svg%3E" /> <figcaption class="mt-1 text-xs text-slate-500">object-contain</figcaption> </figure> <figure> <img class="aspect-square w-full object-fill" alt="A red circle on orange" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='600' height='300'%3E%3Crect width='600' height='300' fill='%23f59e0b'/%3E%3Ccircle cx='300' cy='150' r='110' fill='%23be123c'/%3E%3C/svg%3E" /> <figcaption class="mt-1 text-xs text-slate-500">object-fill</figcaption> </figure></div>

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. border is 1px, border-2 and border-4 are thicker, and border-t or border-b draw one side. divide-y draws 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) and rounded-full for 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-xs to shadow-2xl lift an element off the page. Tint them with a colour and opacity, like shadow-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.

HTML + Tailwind
<!-- 1 container: max-w-sm, rounded-2xl, border, overflow-hidden --><!-- 2 media: aspect-video, w-full, object-cover --><!-- 3 spacing: p-5 inside, gap between text blocks --><!-- 4 detail: divide-y list, shadow, hover shadow --><article class="max-w-sm overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm hover:shadow-lg"> <div class="aspect-video w-full bg-linear-to-br from-lime-200 to-emerald-400"></div> <div class="flex flex-col gap-3 p-5"> <p class="text-xs font-semibold uppercase tracking-wider text-emerald-700">30 minutes</p> <h2 class="text-xl font-bold text-slate-900">Herby green orzo</h2> <p class="text-sm leading-relaxed text-slate-600">Lemon, peas, spinach and a lot of parsley, all in one pan.</p> <ul class="divide-y divide-slate-100 rounded-lg border border-slate-200 text-sm"> <li class="flex justify-between px-3 py-2"><span>Serves</span><span class="font-medium">4</span></li> <li class="flex justify-between px-3 py-2"><span>Calories</span><span class="font-medium">520</span></li> </ul> <button class="mt-1 w-full rounded-lg bg-emerald-700 py-2.5 font-semibold text-white hover:bg-emerald-800">Cook this</button> </div></article>

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 it in order4 steps

Put the steps of building a card in a sensible order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.
Key terms
--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) or aspect-square.
object-cover
Makes an image fill its box and crops the overflow, keeping its proportions.