Skip to main content
Quick Reference

Tailwind CSS Cheatsheet

Tailwind v4 on one page — setup, the spacing scale, layout, flex and grid, type, color, borders and shadows, responsive and state variants, theme tokens, and the class-name traps that show up in reviews.

10 sections 9 tables 17 code snippets

Install & First Classes

Vite + Tailwind v4

npm install tailwindcss @tailwindcss/vite

# vite.config.ts
# import tailwindcss from "@tailwindcss/vite"
# plugins: [tailwindcss()]

Global CSS

@import "tailwindcss";

A button

<button class="rounded-lg bg-indigo-600 px-4 py-2 text-sm font-semibold text-white hover:bg-indigo-700">
  Save
</button>

Utilities are the API. You style in the markup, and Tailwind generates only the CSS for classes it finds. v4 discovers files automatically. You do not list content paths unless a folder is being skipped.

Spacing, Sizing & the Scale

Padding, margin, size

<div class="mx-auto mt-8 max-w-3xl px-4 py-6">
  <div class="h-16 w-16"></div>
  <div class="size-10"></div>
</div>

The number scale

ClassCSSTypical use
p-00Reset
p-10.25rem (4px)Tight icon padding
p-20.5remSmall controls
p-41remCards and buttons
p-82remSection padding
px-4 py-2horizontal / verticalAxis shortcuts
ps-4 pe-4padding-inlineLogical, follows writing direction
w-full / h-screen100% / 100vhFill the parent or the viewport

The number is a step on a 0.25rem scale, not pixels. 4 means 4 steps, so 1rem. Negative margin is -mt-4. Arbitrary values look like w-[32rem] or top-[13px].

Display, Position & Overflow

Display and position

<div class="relative">
  <span class="absolute top-2 right-2">New</span>
  <img class="block h-auto w-full" alt="" />
</div>

Common layout utilities

ClassMeaning
block / inline / inline-block / hiddenDisplay, or remove from layout
relative / absolute / fixed / stickyPosition. Absolute is relative to the nearest positioned ancestor
inset-0Stretch to all four edges
z-10Stacking order
overflow-hidden / overflow-autoClip, or scroll when needed
sr-onlyHide visually, keep it for screen readers

Flexbox & Grid

Row with space between

<header class="flex items-center justify-between gap-4">
  <h2 class="text-lg font-semibold">Courses</h2>
  <a class="text-sm text-indigo-600" href="/courses">All</a>
</header>

Responsive card grid

<ul class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
  <li class="col-span-1 rounded-xl border p-4">Card</li>
</ul>

Alignment

ClassMeaning
flex / inline-flexTurn on flexbox
flex-col / flex-rowMain axis direction
flex-wrapAllow wrapping
items-centerCross-axis alignment
justify-between / justify-centerMain-axis distribution
gap-4Space between children. Prefer this over margins
flex-1Grow and shrink. Basis 0%
grid-cols-12 + col-span-8A 12-column grid and a span

Typography

A heading and a paragraph

<h1 class="text-3xl font-bold tracking-tight text-slate-900 sm:text-5xl">
  Learn. Practice. Conquer.
</h1>
<p class="mt-3 text-base leading-relaxed text-slate-600">
  Short lessons with practice on every page.
</p>

Type scale

ClassUse
text-xs … text-sm text-base text-lgBody sizes. text-base is 1rem
text-xl … text-6xlHeadings
font-medium / font-semibold / font-boldWeight 500 / 600 / 700
leading-tight / leading-relaxedLine height
tracking-tight / tracking-wideLetter spacing
text-center / text-leftAlignment
truncateOne line, then an ellipsis
line-clamp-3Clamp to three lines

Color, Background & Opacity

Text and surface

<article class="bg-white text-slate-800 dark:bg-slate-950 dark:text-slate-100">
  <p class="text-indigo-600">Label</p>
  <div class="bg-indigo-600/10"></div>
</article>

How a color class is built

PieceExample
Propertytext bg border ring from to
Nameslate gray indigo red emerald
Shade50 lightest through 950 darkest. 500 is the middle
Opacitybg-black/50 is 50% black
Gradientbg-linear-to-r from-indigo-500 to-violet-500

v4 gradient utilities are bg-linear-to-r, bg-radial, and bg-conic. The older bg-gradient-to-r name is the v3 spelling.

Borders, Radius, Shadow & Rings

A card

<div class="rounded-xl border border-slate-200 bg-white p-6 shadow-sm">
  <h3 class="font-semibold">Java</h3>
</div>

Focus ring

<input class="w-full rounded-lg border px-3 py-2 outline-hidden focus:border-indigo-500 focus:ring-2 focus:ring-indigo-500" />

Pieces

ClassMeaning
border1px border. In v4 the default color is currentColor
border-slate-200Set the border color
rounded-lg / rounded-fullRadius. full is a pill or circle
shadow-sm / shadow-lgDrop shadow. v4 shadow-sm is the old shadow
ring-2 ring-indigo-500A box-shadow outline that does not change layout
outline-hiddenRemove the outline but keep it for forced-colors mode

Responsive, Hover, Dark & Focus

Mobile first

<section class="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-4">
  <a class="underline-offset-4 hover:underline focus-visible:ring-2" href="/gate">
    GATE
  </a>
</section>

Group hover

<a class="group block" href="/java">
  <h3 class="group-hover:text-indigo-600">Java</h3>
</a>

Variants

VariantApplies when
(none)Every viewport. This is the mobile style
sm: md: lg: xl: 2xl:That width and up. sm is 40rem
max-md:Below the md breakpoint
hover: focus: focus-visible:Pointer, focus, and keyboard focus
disabled: aria-expanded:State and ARIA attributes
dark:By default, prefers-color-scheme: dark
group-hover:A parent with class="group" is hovered

Stack variants left to right: dark:md:hover:bg-slate-800. The breakpoint does not mean “only on small screens.” Unprefixed classes already apply there.

Theme, Arbitrary Values & @apply

Design tokens in CSS

@import "tailwindcss";

@theme {
  --color-brand: #4f46e5;
  --font-sans: "Inter", sans-serif;
  --breakpoint-3xl: 120rem;
}

Use the token

<p class="bg-brand font-sans text-white 3xl:text-xl">
  Brand
</p>

What `--` namespaces create

TokenUtilities
--color-brandbg-brand text-brand border-brand
--font-sansfont-sans
--breakpoint-3xl3xl: variant
--spacingThe multiplier behind p-4 and gap-4
@utility badge { ... }A custom class you can use like a utility

Prefer utilities in HTML. Use @apply only inside a component class you truly repeat. A dynamic name such as ` bg-${color}-500 ` is invisible to the scanner, so the CSS is never generated. Write the full class name.

Interview Traps

Traps worth memorising

TrapWhat happensPrefer
` bg-${name}-500 `Class is built in JS, so Tailwind never sees itA map of complete class strings
Thinking md: is “tablets only”It applies from md upwardUnprefixed for mobile, md: to override
border with no color in v4Border uses currentColor, often blackAdd border-slate-200
Copying v3 shadow-smv4 shifted the scale. Old shadow-sm is shadow-xsLook at the v4 shadow name
outline-none for a custom focusv4 removes the outline, including forced-colorsoutline-hidden, then your own ring
!bg-red-500 from v3 docsv4 puts important at the endbg-red-500!
space-x-4 on a gridMargins between children, not grid gapgap-4
w-full inside a flex itemThe item may refuse to shrinkAdd min-w-0 on the flex child
Hover-only buttonsKeyboard users get no feedbackAdd focus-visible:
Dark colors with no dark: pairUnreadable on a dark system themePair bg-white with dark:bg-slate-950
@apply for every ruleYou rebuild a second stylesheetKeep utilities in the markup
Spaces inside []w-[32 rem] is invalidw-[32rem] or w-[32_rem] when a space is required

Keep every class complete

const tones = {
  info: "bg-sky-50 text-sky-800",
  danger: "bg-red-50 text-red-800",
};

<span class={tones[tone]}>Note</span>

Let a flex child shrink

<div class="flex">
  <p class="min-w-0 flex-1 truncate">
    A long title that should ellipsize
  </p>
</div>

How to use this page: skim the jump chips, then restyle one card using only utilities: spacing, a responsive grid, type, a border, and a hover: plus focus-visible: state. If a class does nothing, the name was probably built with string concatenation.