Utility Class Fundamentals
Learn the utility groups used daily: spacing, sizing, colors, typography, borders, and shadows.
Spacing: padding, margin, and gap
Tailwind's spacing utilities use a consistent scale for padding (p-*), margin (m-*), and layout gaps (gap-*). Directional forms include px-* for left/right padding, py-* for top/bottom, mt-* for top margin, and space-y-* for spacing between direct children in simple vertical stacks.
Prefer gap-* for grid and flex layouts because it expresses the relationship between items without adding margins to individual children. Use margin when an element needs external separation from surrounding content.
A useful spacing rhythm is more important than choosing the mathematically perfect number. Reusing a small set of spacing values makes an interface feel intentional.
p-4, px-6, py-2mt-4, mx-automx-auto commonly centers a constrained block.gap-3, gap-x-6space-y-4<section class="rounded-2xl border border-slate-200 bg-white p-6">
<div class="flex items-start gap-4">
<div class="grid size-12 shrink-0 place-items-center rounded-xl bg-blue-100 font-bold text-blue-700">TF</div>
<div class="min-w-0">
<h2 class="font-semibold">Weekly review</h2>
<p class="mt-1 text-sm leading-6 text-slate-600">Review completed work and choose next week's priorities.</p>
</div>
</div>
<div class="mt-6 flex flex-wrap gap-2">
<span class="rounded-full bg-slate-100 px-3 py-1 text-xs">Planning</span>
<span class="rounded-full bg-slate-100 px-3 py-1 text-xs">Team</span>
</div>
</section>p-6 pads the card. gap-4 separates the icon from the text. mt-1 creates a small heading-to-description gap, while mt-6 separates the metadata row from the main content. min-w-0 allows flex children to shrink rather than overflow because of long text.For consistent spacing between items in a flex row, which utility is usually the clearest choice?
Width, height, and sizing
Sizing utilities control the dimensions of an element. Common examples include w-full, w-1/2, max-w-xl, h-12, min-h-screen, and size-10. size-* sets width and height together, making it useful for square icons or avatars.
A fixed width is often a poor choice for content-heavy cards because text can overflow at narrow viewports. Prefer a responsive combination such as w-full max-w-md, or use grid columns that adapt to the available space. Also understand the difference between w-full (100% of the containing block) and max-w-* (an upper bound).
Use min-h-* when content may grow. A fixed h-* can clip content if the text wraps or the user's font size is larger.
<div class="mx-auto w-full max-w-3xl px-4">
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
<article class="min-h-40 rounded-xl bg-white p-5 shadow-sm">Tasks completed</article>
<article class="min-h-40 rounded-xl bg-white p-5 shadow-sm">Tasks remaining</article>
</div>
</div>max-w-3xl. The grid is one column by default and two columns from the sm breakpoint upward. min-h-40 provides a minimum card height without preventing content from expanding it.- Use
max-w-*to control readable line length and page width. - Prefer minimum dimensions for content that may grow.
- Use
size-*for square elements where supported by your Tailwind version.
Text colors, backgrounds, and opacity
Color utilities follow patterns such as text-slate-700, bg-blue-600, border-slate-200, and placeholder:text-slate-400. The number usually represents a shade in a palette, with lower and higher values representing different lightness levels. Do not assume every palette is equally suitable for text: test foreground/background contrast.
Opacity utilities and slash modifiers can express transparency, for example bg-black/40 for a translucent overlay. In modern Tailwind, opacity modifiers can be combined with many color utilities. Exact behavior depends on the color syntax and version.
Use semantic intent in your design system: primary actions, muted text, borders, success, warning, and danger should have consistent roles. Avoid choosing each color independently on every page.
A red or green color is not enough to communicate an error or success state. Include text, an icon with an accessible name when useful, or another clear cue.
<article class="rounded-xl border border-emerald-200 bg-emerald-50 p-5">
<p class="text-sm font-medium text-emerald-800">Deployment status</p>
<div class="mt-2 flex items-center gap-2">
<span class="size-2 rounded-full bg-emerald-600" aria-hidden="true"></span>
<h2 class="text-xl font-semibold text-emerald-950">All systems operational</h2>
</div>
<p class="mt-2 text-sm leading-6 text-emerald-900">The latest build passed its health checks.</p>
</article>