Tailwind CSS Utility-first CSS

Version 4.3 · source

Start here

npm i tailwindcss @tailwindcss/cli
Install
@import "tailwindcss";
Load it in your CSS
npx @tailwindcss/cli -i <in> -o <out> --watch
Build CSS
flex items-center gap-4
Layout in markup
p-4 text-lg font-bold
Space and type
md:flex-row
Responsive variant
hover:bg-sky-700
State variant
w-[17px]
One-off value

Concepts

  1. Your source files class names found
  2. input.css @import "tailwindcss"
  3. Tailwind CLI / Vite only used classes
  4. output.css linked in HTML

Setup and build

npm install tailwindcss @tailwindcss/cli
Install with the standalone CLI
npm install tailwindcss @tailwindcss/vite
Install the Vite plugin
@import "tailwindcss";
Pull in Tailwind (preflight, theme, utilities)
npx @tailwindcss/cli -i <in> -o <out>
Build the CSS once
npx @tailwindcss/cli -i <in> -o <out> --watch
Rebuild on change
npx @tailwindcss/cli -i <in> -o <out> --minify
Minified production build
@source "<path>";
Scan extra files for classes
@source not "<path>";
Exclude a path from scanning
@import "tailwindcss" prefix(tw);
Prefix all utilities (tw:flex)
@import "tailwindcss" important;
Mark every utility !important

Layout

flex
Flex container
flex-col
Stack children vertically
items-center
Align items on the cross axis
justify-between
Spread items along the main axis
gap-4
Gap between children (1 unit = 0.25rem)
grid grid-cols-3
Three-column grid
col-span-2
Span two grid columns
block / inline-block / hidden
Display values
relative / absolute / fixed / sticky
Positioning
inset-0
Pin to all four edges
z-10
Stacking order
overflow-hidden
Clip overflowing content
container mx-auto
Centred, width-capped container
flex-wrap
Let items wrap
grow / shrink-0
Flex grow and shrink
space-x-4
Horizontal space between children
place-items-center
Centre grid items both ways

Spacing and sizing

p-4
Padding on all sides
px-4 py-2
Horizontal and vertical padding
pt-2 / pr-2 / pb-2 / pl-2
Padding per side
m-4
Margin on all sides
mx-auto
Centre a block horizontally
-mt-2
Negative margin
w-full
Width 100%
h-screen
Height 100vh
w-64
Fixed width (16rem)
size-10
Width and height together
max-w-md
Cap the width
min-h-screen
At least full viewport height
w-1/2
Fractional width
h-dvh
Dynamic viewport height

Typography

text-lg
Font size (sm, base, lg, xl, 2xl...)
font-bold
Font weight
text-center
Align text
text-gray-700
Text colour
leading-tight
Line height
tracking-wide
Letter spacing
font-mono
Monospace font family
truncate
One line with ellipsis
line-clamp-3
Clamp to three lines
uppercase
Transform to upper case
underline
Underline text
whitespace-nowrap
Never wrap

Colour and effects

bg-white
Background colour
bg-blue-500
Palette colour (50 to 950)
bg-black/50
Colour with 50% opacity
opacity-50
Element opacity
border
1px border
border-2 border-gray-300
Border width and colour
rounded-lg
Rounded corners
rounded-full
Pill or circle
shadow-md
Box shadow
ring-2 ring-blue-500
Outline ring (focus styles)
bg-linear-to-r from-<c> to-<c>
Gradient left to right
transition
Animate property changes
duration-200
Transition length in ms
blur-sm / backdrop-blur
Blur element / backdrop
cursor-pointer
Pointer cursor

Variants

hover:bg-sky-700
On hover
focus:ring-2
On focus
active: / disabled:
Pressed / disabled state
focus-visible:outline-2
Keyboard focus only
sm: md: lg: xl: 2xl:
Min-width breakpoints (40/48/64/80/96rem)
max-md:hidden
Below the md breakpoint
md:max-lg:flex
Only between md and lg
min-[600px]:flex
Arbitrary breakpoint
dark:bg-gray-800
Dark mode
group / group-hover:underline
Style child on parent hover
peer / peer-checked:block
Style by a sibling's state
first: last: odd: even:
Position variants
data-[state=open]:block
Match a data attribute
has-[:checked]:bg-blue-50
Style by a descendant
@container / @md:flex-row
Container query on parent width
disabled:hover:bg-sky-500
Stack variants, left to right

Custom values and theme

w-[17px]
Arbitrary value
bg-[#316ff6]
Arbitrary colour
grid-cols-[24rem_1fr]
Underscore stands for a space
bg-(--my-var)
Use a CSS variable
[--gap:1rem]
Set a CSS variable via a class
bg-red-500!
Important modifier (trailing !)
@theme { --color-brand: #f00; }
Add a design token (bg-brand)
@theme { --breakpoint-sm: 30rem; }
Override a default value
@theme { --color-*: initial; }
Drop a whole namespace
@theme inline { --font-sans: var(--x); }
Token that references another variable
@apply <utilities>;
Inline utilities into your own CSS
@utility <name> { ... }
Define a custom utility
@custom-variant dark (&:where(.dark, .dark *));
Class-based dark mode
@plugin "@tailwindcss/typography";
Load a plugin
@reference "<css>";
Use theme/@apply in a Vue/Svelte style block
@layer components { ... }
Put custom CSS in a layer

Recipes

Minimal CLI project
npm i tailwindcss @tailwindcss/cliecho '@import "tailwindcss";' > src/input.cssnpx @tailwindcss/cli -i src/input.css -o dist/out.css --watch
Card with hover and dark mode
<div class="rounded-xl bg-white p-6 shadow-md  hover:shadow-lg dark:bg-gray-800">  <h2 class="text-lg font-bold">Title</h2></div>
Responsive grid
<ul class="grid grid-cols-1 gap-4  sm:grid-cols-2 lg:grid-cols-4">  <li>...</li></ul>
Brand token and class-based dark mode
@import "tailwindcss";@custom-variant dark (&:where(.dark, .dark *));@theme {  --color-brand: oklch(0.65 0.2 250);}/* use: bg-brand text-brand */