Utility Classes — Deep Dive
In Chapter 1 you learned what utility classes are. Now it's time to go deep — every major category of Tailwind utilities with real code, visual diagrams, and the naming patterns you need to write Tailwind confidently. By the end of this chapter you'll know spacing, sizing, typography, colors, flexbox, grid, borders, shadows, effects, and the anatomy of every class name Tailwind has.
Class Name Anatomy
Every Tailwind class follows a predictable pattern. Once you internalize it, you can guess class names without looking them up. There are two forms — a base utility and a variant utility.
m-auto mx-4 gap-4
max-w-lg min-h-0
leading-relaxed truncate
text-gray-700
border-red-300
items-center
justify-between
col-span-2
place-items-center
shadow-lg ring-2
transition duration-300
/* BASE UTILITY — [property]-[value] */
text-xl /* font-size: 1.25rem (20px) */
p-4 /* padding: 1rem (16px) on all sides */
bg-cyan-500 /* background-color: #06b6d4 */
rounded-xl /* border-radius: 0.75rem */
/* VARIANT UTILITY — [variant]:[property]-[value] */
hover:bg-cyan-600 /* apply on :hover */
md:text-2xl /* apply at ≥768px screens */
dark:bg-gray-900 /* apply in dark mode */
md:hover:bg-cyan-500 /* on hover, at ≥768px */
/* ARBITRARY VALUE — any valid CSS in square brackets */
w-[340px] /* width: 340px */
bg-[#06b6d4] /* exact hex color */
grid-cols-[1fr_2fr] /* custom grid template (use _ for spaces) */
Spacing — Padding, Margin & Gap
Tailwind uses a 4px base unit. Every number multiplies by 4px —
p-1 = 4px, p-4 = 16px,
p-8 = 32px. This creates a consistent rhythm across your
entire UI without custom values.
<!-- PADDING — all sides / axis / individual -->
<div class="p-4"> all sides 16px</div>
<div class="px-6 py-3"> x=24px y=12px</div>
<div class="pt-2 pr-4 pb-6 pl-8"> individual sides</div>
<!-- MARGIN -->
<div class="m-auto"> center (auto margin)</div>
<div class="mx-auto max-w-xl"> centered container</div>
<div class="-mt-4"> negative margin — pull up 16px</div>
<!-- GAP (inside flex / grid containers) -->
<div class="flex gap-4"> 16px between all children</div>
<div class="grid grid-cols-3 gap-x-6 gap-y-4"> separate x/y gaps</div>
<!-- SPACE BETWEEN siblings (adds margin-left on all but first) -->
<div class="flex space-x-4"> horizontal spacing</div>
<div class="flex flex-col space-y-3"> vertical spacing</div>
gap works for both flex and grid, handles wrap correctly, and is the modern
standard.
Sizing — Width, Height, Min & Max
Tailwind's sizing utilities cover fixed values from the 4px scale, fractional widths
(w-1/2), viewport units, and special keywords
like w-fit — all without a line of custom CSS.
| Class | CSS Value | Use Case |
|---|---|---|
| w-full | width: 100% | Span full parent width |
| w-screen | width: 100vw | Full viewport width |
| w-1/2 | width: 50% | Half — fractions: 1/3, 2/3, 1/4, 3/4… |
| w-auto | width: auto | Browser determines width |
| w-fit | width: fit-content | Shrink-wraps to content |
| w-max | width: max-content | Widest without wrapping |
| h-screen | height: 100vh | Full viewport height — hero sections |
| h-full | height: 100% | Full height of parent (parent must have height) |
| min-h-screen | min-height: 100vh | At least full viewport tall |
| max-w-sm | max-width: 24rem | Constrain width — sm/md/lg/xl/2xl/prose |
| max-w-prose | max-width: 65ch | Ideal reading line length |
| size-12 | width: 3rem; height: 3rem | Square shorthand (v3.3+) |
<!-- Centered page container -->
<div class="max-w-6xl mx-auto px-6">...</div>
<!-- Full-page hero section -->
<section class="min-h-screen flex items-center justify-center">...</section>
<!-- Square avatar -->
<div class="size-12 rounded-full bg-cyan-500">...</div>
<!-- Two-column layout -->
<div class="flex gap-6">
<div class="w-1/2">Left</div>
<div class="w-1/2">Right</div>
</div>
<!-- Arbitrary value for exact size -->
<div class="w-[340px] h-[220px]">exact size</div>
Typography Utilities
Tailwind ships a complete type scale, font-weight system, line-height, letter-spacing, text alignment, decoration, and overflow utilities. All composable with any variant.
<!-- FONT WEIGHT -->
<p class="font-thin">100</p> <p class="font-normal">400</p> <p class="font-medium">500</p>
<p class="font-semibold">600</p> <p class="font-bold">700</p> <p class="font-black">900</p>
<!-- LINE HEIGHT -->
<p class="leading-tight">1.25 — headings</p>
<p class="leading-normal">1.5 — default</p>
<p class="leading-relaxed">1.625 — body text</p>
<p class="leading-loose">2 — airy</p>
<!-- LETTER SPACING -->
<h2 class="tracking-tight">tight</h2>
<span class="tracking-widest uppercase text-xs">LABEL</span>
<!-- TEXT DECORATION & TRANSFORM -->
<a class="underline decoration-cyan-500 decoration-2">link</a>
<span class="line-through">strikethrough</span>
<p class="uppercase">ALL CAPS</p>
<!-- TEXT OVERFLOW -->
<p class="truncate">Long text cut off with ellipsis…</p>
<p class="line-clamp-3">Clamp to exactly 3 lines</p>
Color System
Tailwind ships 22 named color palettes, each with 11 shades
(50–950). Every color works with bg-,
text-, border-,
ring-, and more. Use the
/opacity modifier for translucency.
<!-- Background -->
<div class="bg-cyan-500">solid cyan</div>
<div class="bg-cyan-500/20">cyan at 20% opacity</div>
<!-- Text -->
<p class="text-gray-900 dark:text-white">adaptive</p>
<p class="text-cyan-500 hover:text-cyan-700">interactive</p>
<!-- Border -->
<div class="border border-gray-200 dark:border-gray-700">themed</div>
<!-- Ring (accessible focus outline) -->
<button class="focus:ring-2 focus:ring-cyan-400 focus:ring-offset-2 focus:outline-none">
Accessible button
</button>
<!-- Arbitrary colors -->
<div class="bg-[#06b6d4] text-[#fff]">exact hex</div>
Flexbox & Grid
Flexbox and Grid are the two layout systems you'll use for almost every UI component. Tailwind maps both CSS specifications to clean, intuitive class names.
<!-- ── FLEXBOX ────────────────────────────────────── -->
<div class="flex"> <!-- display: flex -->
<div class="flex flex-col"> <!-- column direction -->
<div class="flex flex-wrap"> <!-- wrap to next line -->
<!-- Alignment -->
<div class="flex items-center"> <!-- align-items: center -->
<div class="flex justify-between"> <!-- space between children -->
<div class="flex justify-center"> <!-- centered -->
<!-- Flex children -->
<div class="flex-1"> <!-- grow to fill space -->
<div class="flex-none shrink-0"> <!-- won't grow or shrink -->
<!-- ── GRID ────────────────────────────────────────── -->
<div class="grid grid-cols-3 gap-6"> <!-- 3 columns -->
<div class="grid grid-cols-12 gap-4"> <!-- 12-col system -->
<!-- Responsive grid -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
<!-- Spanning -->
<div class="col-span-2"> spans 2 columns
<div class="row-span-2"> spans 2 rows
<div class="place-items-center"> center items in cells
Borders, Shadows & Effects
Every visual detail — border width, color, radius, shadows, opacity, blur, and cursor — is covered by a Tailwind utility, all composable with hover and dark variants.
<!-- BORDER WIDTH -->
<div class="border"> 1px
<div class="border-2 border-cyan-400"> 2px cyan
<div class="border-4 border-dashed border-gray-300"> dashed
<div class="border-t border-gray-200"> top only
<div class="divide-y divide-gray-100"> dividers between children
<!-- BORDER RADIUS -->
<div class="rounded"> 4px <div class="rounded-md"> 6px
<div class="rounded-lg"> 8px <div class="rounded-xl"> 12px
<div class="rounded-2xl"> 16px <div class="rounded-full"> pill
<!-- SHADOWS -->
<div class="shadow-sm"> <div class="shadow-md"> <div class="shadow-lg">
<div class="shadow-xl"> <div class="shadow-2xl"> <div class="shadow-inner">
<div class="shadow-cyan-500/30"> coloured glow
<!-- OPACITY, BLUR, CURSOR -->
<div class="opacity-50"> half-visible
<div class="backdrop-blur-md bg-white/20"> glass effect
<button class="cursor-pointer"> pointer hand
<div class="cursor-not-allowed opacity-50"> disabled
Transitions, Transforms & Animations
Combine transitions with state variants to create smooth, interactive hover effects without a single line of custom CSS.
<!-- TRANSITIONS -->
<btn class="transition"> all props 150ms
<btn class="transition-all duration-300 ease-in-out">
<btn class="transition-colors duration-200"> colors only
<!-- TRANSFORMS -->
<div class="hover:scale-105 transition-transform"> grow
<div class="hover:-translate-y-1"> lift
<div class="hover:rotate-6 transition"> rotate
<div class="scale-x-[-1]"> flip horizontally
<!-- Full interactive card -->
<div class="bg-white rounded-2xl p-6 shadow
hover:shadow-xl hover:-translate-y-2 hover:shadow-cyan-500/20
transition-all duration-300 cursor-pointer">
Hover me!
</div>
<!-- ANIMATIONS -->
<div class="animate-spin"> loader icon
<div class="animate-ping"> notification dot
<div class="animate-pulse"> skeleton shimmer
<div class="animate-bounce"> scroll indicator
Responsive Prefixes
Tailwind is mobile-first. Every utility applies to all screen sizes by default. Add a breakpoint prefix to restrict it to that size and above.
<!-- Responsive heading -->
<h1 class="text-2xl sm:text-3xl md:text-4xl lg:text-5xl xl:text-6xl font-black">
<!-- Stack on mobile, row on desktop -->
<div class="flex flex-col md:flex-row gap-6">
<!-- Responsive grid: 1 → 2 → 4 columns -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
<!-- Show/hide at breakpoints -->
<div class="hidden md:block">desktop only
<div class="block md:hidden">mobile only
<!-- Responsive sidebar layout -->
<div class="grid grid-cols-1 lg:grid-cols-[280px_1fr] gap-8">
<aside class="hidden lg:block">Sidebar</aside>
<main>Content</main>
</div>
- Class anatomy: every Tailwind class follows
[variant:]property-value. Variants stack —md:hover:bg-cyan-500means "on ≥768px screens, apply this background on hover." - Spacing uses a 4px base unit —
p-1=4px,p-4=16px,p-8=32px. Padding, margin, and gap all share this consistent scale. - Sizing covers fixed 4px-scale values, fractions (
w-1/2), viewport units (h-screen), content-based (w-fit), andmax-w-*constraints. - Typography scales from
text-xs(12px) totext-9xl(128px), with font-weight, line-height, letter-spacing, decoration, and overflow classes. - Color system: 22 palettes × 11 shades. Use
bg-cyan-500/20opacity modifier for transparency. - Flexbox & Grid —
flex items-center justify-betweenandgrid grid-cols-3 gap-6cover 90% of layout needs. - Responsive is mobile-first. Prefix any class:
sm: md: lg: xl: 2xl:to apply it at that breakpoint and above. - Arbitrary values
[]let you write any CSS value as a class — the last resort. If you're using them often, add a theme token instead.
Quick Check — Questions & Answers
Click a question to reveal the detailed answer.
p-6 equals 24px of padding on all four sides. Tailwind
uses a 4px base unit — every number is multiplied by 4px. So
p-6 = 6 × 4px = 24px. This applies to all spacing:
m-6 = 24px, gap-6 = 24px, space-x-6 = 24px.
The scale goes from p-0 (0px) to p-96 (384px), with
p-px for exactly 1px.
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">•
grid-cols-1 — 1 column on all screens (mobile default)•
sm:grid-cols-2 — 2 columns at ≥640px•
lg:grid-cols-4 — 4 columns at ≥1024pxNo media queries needed — just prefix any class.
gap-4 uses the CSS gap property — works
on flex and grid containers. Handles wrapping correctly and is the
modern approach. Use this.space-x-4 adds margin-left: 1rem on every
child except the first, using the * + * selector. Only works on
single-line flex — breaks with flex-wrap.Rule: always prefer
gap-* for flex and grid layouts.
bg-cyan-500/30 → background at 30% opacitytext-blue-600/75 → text at 75% opacityborder-gray-300/50 → border at 50% opacityArbitrary opacity:
bg-cyan-500/[.15]. This is cleaner than the older
bg-opacity-30 utilities (which still exist for backwards
compatibility).
w-[340px] — use for one-off values
used once or twice in your project (a specific modal width, an unusual
image crop).Extend the theme — when the value is used in 3+ places or represents a design token:
// tailwind.config.jstheme: { extend: { width: { sidebar: '280px' } } }Then use:
w-sidebar — readable, centralised, easy to change. If you
find yourself copying the same arbitrary value multiple times, that's the signal to
make it a theme token.
MCQ Quiz — Test Yourself
Select the best answer for each question. The correct option is revealed after you click.
p-8
apply as padding?text-cyan-500/30 do?
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.