Menu
CoddyTech

Tailwind CSS Cheat Sheet

Last updated

Learn Tailwind CSS free

Spacing: padding, margin and gap

Spacing classes multiply --spacing (0.25rem by default) by their number, so p-4 is 1rem and any whole number works.

What it doesClass
Padding on all sidesp-4
Horizontal and vertical paddingpx-6 py-2
Padding on one side (pt, pr, pb, pl)pt-8
Padding at the start and end of the line (flips in right-to-left)ps-4 pe-2
Margin on all sidesm-4
Center a block horizontallymx-auto
Negative margin-mt-2
Vertical space between childrenspace-y-4
Gap between flex or grid itemsgap-4
Different column and row gapsgap-x-8 gap-y-4
One pixel, or zerop-px m-0
An exact valuemt-[13px]

Sizing: width and height

What it doesClass
Width from the spacing scalew-64
A fraction of the parent's widthw-1/2
Full width of the parentw-full
Width and height at oncesize-10
Heighth-12
Full viewport height, mobile-safeh-dvh
At least the viewport's heightmin-h-screen
Minimum and maximum widthmin-w-0 max-w-md
A readable line length for textmax-w-prose
As wide as the contentw-fit
Aspect ratioaspect-video
An exact widthw-[350px]

Typography

What it doesClass
Font size (text-xs to text-9xl)text-lg
Font size with a line heighttext-lg/7
Font weightfont-semibold
Font familyfont-mono
Line heightleading-relaxed
Letter spacingtracking-tight
Align texttext-center
Align to the start of the line (follows the text direction)text-start
Uppercaseuppercase
Italic and underlineditalic underline
Underline offset and thicknessunderline-offset-4 decoration-2
Cut one line with an ellipsistruncate
Limit text to three linesline-clamp-3
Balance the lines of a headingtext-balance
Break long words to avoid overflowwrap-break-word
Keep text on one linewhitespace-nowrap
Bulleted listlist-disc list-inside

Colors

Every palette color has shades from 50 to 950, defined in OKLCH.

What it doesClass
Text colortext-gray-700
Background colorbg-sky-500
Border colorborder-red-300
A color at 50% opacitybg-black/50
An exact colorbg-[#1da1f2]
A color from a CSS variablebg-(--brand)
Use the current text colorborder-current
Transparent backgroundbg-transparent
White text on blacktext-white bg-black
Placeholder text colorplaceholder:text-gray-400
Selected text colorselection:bg-yellow-200
Caret and form accent colorcaret-pink-500 accent-pink-500
SVG fill and strokefill-blue-500 stroke-white

Backgrounds and gradients

What it doesClass
Linear gradient from left to rightbg-linear-to-r from-cyan-500 to-blue-500
Gradient with a middle colorfrom-indigo-500 via-purple-500 to-pink-500
Gradient at an anglebg-linear-45
Where the gradient stopsfrom-10% to-90%
Radial gradientbg-radial
Conic gradientbg-conic
Background imagebg-[url(/hero.jpg)]
Cover the element and center the imagebg-cover bg-center
Do not repeat the imagebg-no-repeat

Flexbox

What it doesClass
Lay out children in a rowflex
Lay out children in a columnflex flex-col
Center children both waysflex items-center justify-center
Push children to both endsjustify-between
Wrap onto new linesflex-wrap
Grow to fill the space leftflex-1
Never shrinkshrink-0
Align one child on its ownself-end
Reverse the orderflex-row-reverse
Move one child firstorder-first
Initial size of a flex itembasis-1/3
An inline flex containerinline-flex

Grid

What it doesClass
Three equal columnsgrid grid-cols-3
One column on mobile, three from mdgrid-cols-1 md:grid-cols-3
Span two columnscol-span-2
Span the full rowcol-span-full
Start and end linescol-start-2 col-end-4
Custom column sizesgrid-cols-[200px_1fr]
Header, content and footer rowsgrid-rows-[auto_1fr_auto]
As many card columns as fitgrid-cols-[repeat(auto-fill,minmax(12rem,1fr))]
Center items in their cellsplace-items-center
Fill columns before rowsgrid-flow-col
Inherit the parent's columnsgrid-cols-subgrid

Layout and position

What it doesClass
Hide an elementhidden
Inline blockinline-block
Position children relative to this elementrelative
Pin to the top right cornerabsolute top-0 right-0
Cover the positioned parentabsolute inset-0
Fixed bar at the top of the windowfixed inset-x-0 top-0
Stick to the top while scrollingsticky top-0
Stack above other elementsz-10
Clip overflowing contentoverflow-hidden
Scroll vertically when content overflowsoverflow-y-auto
Crop an image to fill its boxobject-cover
Centered page containercontainer mx-auto px-4
Hide visually, keep for screen readerssr-only
Split text into columnscolumns-3

Borders, rings and outlines

What it doesClass
A 1px borderborder
Border widthborder-2
Border on one sideborder-b
Dashed borderborder-dashed
Rounded cornersrounded-lg
Circle or pillrounded-full
Round the top corners onlyrounded-t-xl
Lines between childrendivide-y divide-gray-200
Ring around the elementring-2 ring-blue-500
Gap between the element and its ringring-offset-2
Ring inside the elementinset-ring inset-ring-gray-300
Outline with an offsetoutline-2 outline-offset-2 outline-sky-500
Hide the outline, keep it in forced colors modeoutline-hidden

Shadows and effects

What it doesClass
Box shadowshadow-md
Larger shadowshadow-xl
Colored shadowshadow-lg shadow-cyan-500/50
Inner shadowinset-shadow-sm
Text shadowtext-shadow-md
Drop shadow that follows the shapedrop-shadow-lg
Opacityopacity-50
Blurblur-sm
Frosted glassbackdrop-blur-md bg-white/30
Grayscalegrayscale
Blend with the backgroundmix-blend-multiply
Fade out the bottom halfmask-b-from-50%
Pointer cursorcursor-pointer
Ignore clickspointer-events-none
Prevent text selectionselect-none

Transitions, transforms and animation

What it doesClass
Animate color, opacity, shadow and transform changestransition
Animate colors onlytransition-colors
Duration and easingduration-300 ease-in-out
Delaydelay-150
Grow on hoverhover:scale-105
Rotaterotate-45
Move up by half its height-translate-y-1/2
Center an absolutely positioned elementabsolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2
Transform originorigin-top-left
Rotate in 3D (the parent sets a perspective)rotate-x-45 perspective-distant
Spinneranimate-spin
Loading placeholderanimate-pulse
Animate only without reduced motionmotion-safe:animate-bounce
Turn off transitions for reduced motionmotion-reduce:transition-none
Fade in when the element first appearstransition-opacity starting:opacity-0

Responsive breakpoints

Breakpoints are mobile first: an unprefixed class applies at every width, and md: applies from that width up.

What it doesClass
From 40rem (640px) upsm:flex
From 48rem (768px) upmd:grid-cols-2
From 64rem (1024px) uplg:px-8
From 80rem (1280px) upxl:text-6xl
From 96rem (1536px) up2xl:max-w-7xl
Below a breakpoint onlymax-md:hidden
Between two breakpointsmd:max-lg:flex
A one-off breakpointmin-[900px]:grid-cols-4
Hide on mobile, show from mdhidden md:block
Stack on mobile, row from mdflex flex-col md:flex-row
Landscape orientationlandscape:flex-row
When printingprint:hidden

Hover, focus and other states

What it doesClass
On hoverhover:bg-sky-700
On keyboard focusfocus-visible:outline-2
On focusfocus:border-blue-500
While pressedactive:scale-95
When disableddisabled:opacity-50
Invalid after the user has edited the fielduser-invalid:border-red-500
Checked checkbox or radiochecked:bg-blue-600
First and last childfirst:pt-0 last:pb-0
Striped rowsodd:bg-white even:bg-gray-50
Every child except the firstnot-first:mt-4
Style all direct children from the parent*:rounded-md
When the parent marked group is hoveredgroup-hover:underline
A named groupgroup-hover/item:visible
When the sibling marked peer is invalidpeer-invalid:visible
When the element contains a checked inputhas-checked:bg-indigo-50
ARIA statearia-expanded:rotate-180
Data attributedata-[state=open]:bg-gray-100
Content before or after the elementafter:content-['*'] after:text-red-500
Open details or dialogopen:bg-white
Mark a class importantbg-red-500!

Dark mode

dark: follows the operating system's setting by default. To switch with a class, add the @custom-variant dark rule from the directives table below.

What it doesClass
Background in dark modedark:bg-gray-900
Text color in dark modedark:text-gray-100
Hover in dark modedark:hover:bg-gray-800
Native controls and scrollbars in dark colorsscheme-light dark:scheme-dark

Container queries

Mark a parent with @container, then size its children by the container's width instead of the window's.

What it doesClass
Make an element a container@container
From a container width of 28rem up@md:flex-row
Below a container width@max-md:hidden
Between two container widths@sm:@max-lg:grid-cols-2
Name a container@container/sidebar
Query the named container@lg/sidebar:block
An exact container width@min-[475px]:flex
Half the container's widthw-[50cqw]

Arbitrary values and properties

What it doesClass
Any valuetop-[117px]
Spaces in a value become underscoresgrid-cols-[1fr_500px_2fr]
A calculationw-[calc(100%-2rem)]
A CSS variablefill-(--brand-color)
A CSS variable with a type hinttext-(length:--title-size)
Any CSS property[mask-type:luminance]
Style matching children[&>p]:mt-2
Style when the element has a class[&.is-active]:font-bold

Theme variables and directives

Tailwind CSS 4 is configured in CSS, not in tailwind.config.js.

What it doesCSS
Load Tailwind CSS in your stylesheet@import "tailwindcss";
Add a color (creates bg-brand, text-brand and the rest)@theme { --color-brand: oklch(0.62 0.19 259); }
Add a font (creates font-display)@theme { --font-display: "Satoshi", sans-serif; }
Add a breakpoint (creates 3xl:)@theme { --breakpoint-3xl: 120rem; }
Change the spacing scale@theme { --spacing: 0.3rem; }
Replace the whole color palette@theme { --color-*: initial; --color-ink: #111827; --color-paper: #ffffff; }
Add an animation (creates animate-wiggle)@theme { --animate-wiggle: wiggle 1s ease-in-out infinite; @keyframes wiggle { 0%, 100% { rotate: -3deg; } 50% { rotate: 3deg; } } }
Dark mode with a dark class@custom-variant dark (&:where(.dark, .dark *));
Dark mode with a data attribute@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
A custom utility@utility content-auto { content-visibility: auto; }
Use utilities inside your own CSS.btn { @apply rounded-lg px-4 py-2 font-semibold; }
Use a variant inside your own CSS.card { @variant dark { background: black; } }
Read a theme value in CSS.note { color: var(--color-gray-500); }
Prefix every class (tw:flex)@import "tailwindcss" prefix(tw);
Scan a folder Tailwind would skip@source "../node_modules/@acme/ui";
Never scan a folder@source not "./legacy";
Always generate a class@source inline("underline");

Setup and tools

What it doesCommand or code
Install for Vitenpm install tailwindcss @tailwindcss/vite
Add the Vite plugin in vite.config.tsplugins: [tailwindcss()]
Install for PostCSSnpm install tailwindcss @tailwindcss/postcss postcss
The PostCSS config in postcss.config.mjsexport default { plugins: { "@tailwindcss/postcss": {} } };
Install the command line toolnpm install tailwindcss @tailwindcss/cli
Build and watch with the command line toolnpx @tailwindcss/cli -i ./src/input.css -o ./src/output.css --watch
Build minified CSS for productionnpx @tailwindcss/cli -i ./src/input.css -o ./dist/output.css --minify
Upgrade a Tailwind CSS 3 projectnpx @tailwindcss/upgrade

Renamed in Tailwind CSS 4

Some version 3 names still work but now mean a different size, such as shadow-sm; others are kept only for compatibility, and some, such as bg-opacity-50, are gone. npx @tailwindcss/upgrade makes these changes for you.

Tailwind CSS 3Tailwind CSS 4
shadow-smshadow-xs
shadowshadow-sm
rounded-smrounded-xs
roundedrounded-sm
blur-smblur-xs
drop-shadow-smdrop-shadow-xs
outline-noneoutline-hidden
ringring-3
bg-opacity-50bg-black/50
flex-shrink-0shrink-0
flex-growgrow
bg-gradient-to-rbg-linear-to-r
overflow-ellipsistext-ellipsis
decoration-clonebox-decoration-clone
!font-boldfont-bold!
bg-[--brand]bg-(--brand)

The Tailwind CSS classes you reach for most, in copy-paste tables: spacing, sizing, typography, color, flexbox, grid, borders and effects first, then breakpoints, hover and focus states, dark mode, container queries, and @theme with the other CSS directives.

Everything here is Tailwind CSS 4 syntax, and every Tailwind CSS 4 class and CSS snippet in the tables was built with the Tailwind CSS 4.1 compiler to check that it exists. Coming from version 3? The last table lists the renamed utilities. For the CSS properties underneath, see the CSS cheat sheet.

Tailwind CSS cheat sheet FAQ

Is this Tailwind CSS cheat sheet free?

Yes. It is free, with no sign-up. Bookmark it and come back whenever you need a class name, a breakpoint or a directive.

What changed in Tailwind CSS 4?
Configuration moved into CSS: you write @import "tailwindcss"; and add design tokens in @theme, with no tailwind.config.js needed. Content files are found automatically, colors use OKLCH, and container queries, 3D transforms and starting: are built in. Some utilities were renamed, such as shadow-sm to shadow-xs; the last table lists them.
How do I switch dark mode with a class in Tailwind CSS 4?
By default dark: follows the operating system's prefers-color-scheme. To control it yourself, add @custom-variant dark (&:where(.dark, .dark *)); to your CSS and put the dark class on the html element; a small script adds or removes the class and remembers the choice.
Do I still need tailwind.config.js?
No. Tailwind CSS 4 is configured in CSS with @theme, @utility, @custom-variant and @source. A version 3 JavaScript config still works if you load it with @config "./tailwind.config.js";, which helps during a migration.
What is the difference between breakpoints and container queries?
Breakpoints such as md: respond to the width of the browser window. Container queries such as @md: respond to the width of the nearest parent marked @container, so a card can change its layout to fit the space it gets, wherever it is placed.
Can I practice Tailwind CSS online?

Yes. Coddy's free Tailwind CSS course has you add utility classes to real pages and see each change in a live preview, and its checks read the rendered page at the screen widths a task names. It runs on the website, with nothing to install.

Coddy programming languages illustration

Learn Tailwind CSS with Coddy

Get started