Tailwind CSS Cheatsheet - Utility Class Reference

The hard part of utility-first CSS is not memorizing class names but accepting its constraints: classes live in markup, so variants such as breakpoints, dark mode, and states can only be expressed as prefixes, and class names cannot be assembled at runtime. This table groups everything by layout, flexbox, grid, spacing and sizing, typography, color and background, responsive and interactive states, and borders and effects, and it lays out the pairs people mix up most often, such as gap versus space-x, flex-1 versus grow, and translate utilities versus inset utilities. The judgment test is whether the solution fits an existing component without adding a stylesheet: if a requirement forces custom CSS, more often than not the utility expression has not been found yet. Reach for it when building responsive grids, wiring design tokens for a dark theme, coordinating parent and child states with group-hover and peer, or implementing hover animation and clipping. If you mix it with semantic class names, settle that convention before the component folder doubles in styles.

Web Services·61 commands·Last updated 2026-07-21
tailwindcssFrontendFrameworkutility

Typical Use Case

For front-end engineers building interfaces with Tailwind who need them to hold up in both light and dark themes. The sheet arranges utility classes along eight lines from layout to borders and effects, and pins down four judgements that decide most code: Flex for one dimension, Grid for two, gap instead of per-child margins, and the 50-900 colour scale where higher means darker, switched in dark mode with the dark prefix. Breakpoints are mobile-first, so sm means 640px and up. When styles do not apply or contrast is poor in dark mode, go straight to the pitfalls section.

Layout 7

container
Responsive container, auto-centered with max-width
block / inline-block / inline
Display types
hidden
Hide element (display: none)
relative / absolute / fixed / sticky
Positioning
overflow-auto / overflow-hidden
Overflow handling
visible / invisible
Visibility control
z-10 / z-50 / z-[-1]
Z-index stacking order

Flexbox 7

flex
Enable flexbox layout
flex-row / flex-col
Flex direction (row/column)
flex-wrap / flex-nowrap
Wrap behavior
justify-start / justify-center / justify-between / justify-end
Main axis alignment
items-start / items-center / items-end
Cross axis alignment
flex-1 / flex-auto / flex-none
Flex grow/shrink
gap-4 / gap-x-4 / gap-y-4
Gap between items

Grid 7

grid
Enable grid layout
grid-cols-3 / grid-cols-12
Define column count
grid-rows-3
Define row count
col-span-2 / col-span-full
Column spanning
row-span-2 / row-span-full
Row spanning
place-items-center / place-content-center
Grid alignment
gap-4
Grid gap

Spacing & Sizing 8

p-4 / px-4 / py-2 / pt-4
Padding
m-4 / mx-auto / my-2 / mt-4
Margin (mx-auto for horizontal centering)
space-x-4 / space-y-4
Child spacing
w-64 / w-1/2 / w-full / w-screen
Width
h-64 / h-1/2 / h-full / h-screen
Height
max-w-lg / max-w-7xl / max-w-screen-xl
Max width
min-h-screen / min-h-0
Min height
size-8 / size-16
Width and height together (Tailwind 3.4+)

Typography 9

text-xs / text-sm / text-base / text-lg / text-xl / text-2xl ... text-9xl
Font size
font-thin / font-normal / font-medium / font-bold / font-black
Font weight
leading-none / leading-tight / leading-relaxed / leading-loose
Line height
tracking-tight / tracking-wide / tracking-wider
Letter spacing
text-left / text-center / text-right / text-justify
Text alignment
underline / line-through / no-underline
Text decoration
uppercase / lowercase / capitalize
Text transform
truncate
Text overflow ellipsis (single line)
whitespace-nowrap / break-words
Whitespace handling

Colors & Background 7

text-red-500 / text-blue-600 / text-gray-900
Text color (50-900 scale)
bg-white / bg-gray-100 / bg-blue-500
Background color
border-gray-300 / border-red-500
Border color
ring-2 ring-blue-500
Ring color and width (focus indicator)
shadow-sm / shadow-md / shadow-lg / shadow-xl
Box shadow
opacity-50 / opacity-100
Opacity
bg-gradient-to-r from-blue-500 to-purple-500
Gradient background

Responsive & Interaction 9

sm: / md: / lg: / xl: / 2xl:
Responsive breakpoint prefixes (mobile-first)
hover:bg-blue-500
Hover state
focus:outline-none focus:ring-2
Focus state
active:scale-95
Active/click state
dark:bg-gray-800 dark:text-white
Dark mode
transition / transition-all / transition-colors
Transition animation
hover:scale-110 / hover:rotate-6
Transform animation
animate-spin / animate-ping / animate-pulse / animate-bounce
Built-in animations
cursor-pointer / cursor-not-allowed
Cursor style

Border & Effects 7

border / border-2 / border-4 / border-b-2
Border width
border-solid / border-dashed / border-dotted / border-none
Border style
rounded-sm / rounded / rounded-lg / rounded-xl / rounded-full
Border radius
ring / ring-2 ring-inset
Ring outline for focus states
shadow / shadow-md / shadow-lg / shadow-xl / shadow-2xl
Shadow size
outline / outline-2 outline-offset-2
Outline
blur-sm / blur-lg / brightness-50 / contrast-125 / grayscale
Filter effects

Parameter matrix

ParameterEffectExample
containerApply a utility only from that breakpoint upward, which is the mobile-first way to scale a layout.<div class="container">
flex / gridScope a utility to an interaction state instead of writing a separate hover rule.<div class="flex">
flex-row / flex-colCentre a block element horizontally by setting equal auto margins on both sides.class="flex flex-col"
justify-between / justify-centerApply a utility inside the dark colour scheme, driven by the prefers-color-scheme media query by default.class="flex justify-between"
items-centerSize an element as a fraction or fill its container; max-w-* caps the size at a named container width.class="flex items-center"
flex-1 / flex-noneAdd margin between sibling elements, targeting every child except the first.class="flex-1"
flex-wrapClip text to one line with an ellipsis, combining overflow-hidden, text-ellipsis and whitespace-nowrap.class="flex flex-wrap"
gap-4 / gap-x-4 / gap-y-4Draw an outline outside the element, which is the accessible replacement for the removed outline-none.class="flex gap-4"
grid-cols-3 / grid-cols-12Style descendants when a marked ancestor is hovered, without any JavaScript.class="grid grid-cols-3"
col-span-2 / col-span-fullInject a value the scale does not cover, so a one-off CSS value does not force a config change.class="col-span-full"
place-items-centerInline a set of utilities into a custom class inside a CSS file, for repeated compositions.class="grid place-items-center"
p-4 / px-4 / pt-4Create the tailwind.config.js that declares the content globs, theme extensions and plugins.class="px-4 pt-2"
mx-auto外边距水平自动,块级元素水平居中class="mx-auto"
space-x-4 / space-y-4仅给直接子元素加相邻间距class="space-y-4"
w-full / w-64 / w-1/2宽度:撑满 / 固定 16rem / 一半class="w-full"
h-64 / h-full / min-h-screen固定高 / 撑满父级 / 至少一屏class="min-h-screen"
max-w-lg / max-w-7xl限制最大宽度class="max-w-7xl"
size-8同时设置宽高(Tailwind 3.4+)class="size-8"
text-xs ... text-2xl字号阶梯 xs/sm/base/lg/xl/2xlclass="text-lg"
font-normal / font-medium / font-bold字重class="font-bold"
leading-tight / leading-relaxed行高:紧凑 / 宽松class="leading-relaxed"
tracking-tight / tracking-wider字间距class="tracking-wider"
truncate单行溢出显示省略号class="truncate"
whitespace-nowrap / break-words禁止换行 / 长词断行class="whitespace-nowrap"
uppercase / capitalize文本大小写转换class="uppercase"
text-gray-900 / bg-gray-100文本色 / 背景色(50-900 层级)class="text-gray-900"
ring-2 ring-blue-500聚焦环宽度与颜色class="focus:ring-2 ring-blue-500"
shadow-sm / shadow-lg阴影层级class="shadow-lg"
opacity-50整体透明度class="opacity-50"
bg-gradient-to-r from-blue-500 to-purple-500横向渐变背景class="bg-gradient-to-r from-blue-500 to-purple-500"
sm: / md: / lg: / xl: / 2xl:响应式断点前缀(移动优先)class="grid-cols-1 md:grid-cols-3"
hover:bg-blue-500鼠标悬停态样式class="hover:bg-blue-500"
focus:outline-none focus:ring-2聚焦态样式,常用于表单可访问性class="focus:outline-none"
active:scale-95点击激活态缩放反馈class="active:scale-95"
dark:bg-gray-800 dark:text-white暗色模式专用样式class="dark:bg-gray-800"
transition / transition-colors过渡动画(全属性 / 仅颜色)class="transition-colors"
hover:scale-110 / hover:rotate-6悬停变换动画class="hover:scale-110"
animate-spin / animate-ping / animate-pulse / animate-bounce内置循环动画class="animate-spin"
rounded / rounded-lg / rounded-full圆角:小 / 大 / 全圆class="rounded-full"
border / border-2 / border-b-2边框宽度class="border-2"
border-dashed / border-dotted虚线 / 点线边框class="border-dashed"
blur-sm / grayscale / contrast-125滤镜特效class="grayscale"

Common pitfalls

SymptomA class renders with no styles and the browser shows the class is present in the HTML but absent from the stylesheet.

CauseThe file containing that markup is not matched by the content globs in the Tailwind config, so it was never scanned.

FixAdd the directory to content (for example "./src/**/*.tsx") and restart the dev server so the JIT watcher rescans.

SymptomA margin or padding utility has no effect on a flex or grid child.

CauseA utility defined on a later class in the stylesheet loses to another utility in the cascade because both set the same property with equal specificity.

FixResolve the conflict explicitly by removing the competing utility, or use an arbitrary property or important modifier when the override is intentional.

SymptomThe dark theme never applies, or applies while the OS is in light mode.

CauseDark mode is driven by prefers-color-scheme by default, and the class strategy was configured in CSS without the matching dark variant, or the html class was never toggled.

FixEither respect the OS setting, or enable the class strategy in the config and toggle the class on <html> from your own theme code.

SymptomLayout works in development but a production build drops the styles.

CauseThe content globs matched absolute or non-existent paths, so Tailwind purged nearly every class as unused.

FixCheck the compiled CSS size after the build and confirm the globs resolve from the directory where the build runs, typically using paths relative to the config file.

SymptomTransitions or hover effects are not applied because outline-none removed the focus ring.

Causeoutline-none was used to hide the default focus indicator, which breaks keyboard accessibility.

FixReplace it with focus-visible:ring-2 or focus:outline-2 so keyboard users still see a focus state.

SymptomThe stylesheet keeps growing and the build gets slower as the project does.

CauseSafelist entries are too broad, or the content globs include a whole node_modules tree or build output.

FixNarrow content to your source directories, keep safelist entries to exact class names, and exclude generated output.

SymptomA preflight reset overrides the browser default styles and a form looks unstyled.

CauseTailwind injects preflight, which normalises margins, borders and box sizing across all elements.

FixSet corePlugins: { preflight: false } in the config to disable it, and add your own reset, or style the affected components explicitly.

Symptomabsolute 定位的元素跑到页面边缘或被裁掉,z-index 加了也没用

Causeabsolute 会相对最近的定位祖先(position 非 static)定位;若祖先没有 relative/absolute,元素就相对更外层甚至初始包含块定位,同时可能被 overflow-hidden 裁剪。

Fix给预期作为参照的父容器加 relative 定位,再调整 z-10 / z-50 的层叠顺序。

Troubleshooting

  1. 1Confirm the class actually exists in the generated CSS

    class="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 gap-4"

    Building the CSS to a file lets you grep it for the exact utility and see whether it was generated.

  2. 2See which layer a rule came from

    class="flex flex-col sm:flex-row gap-4" → 去掉 gap 换成 space-y-4 对比

    In DevTools, Styles shows the cascade and which utility won, which settles most override disputes.

  3. 3List every rule Tailwind generated for a file

    class="bg-white text-gray-900 dark:bg-gray-800 dark:text-white"

    If nothing matches, the class was purged because the source file is not covered by the content globs.

  4. 4Check which breakpoint currently applies

    class="truncate" / class="break-words" / 父级 "flex" + 子项 "min-w-0"

    This returns the boolean the responsive prefix is actually keyed on at the current viewport.

  5. 5Inspect the resolved Tailwind configuration

    class="transition-colors hover:bg-blue-500 active:scale-95"

    The help output confirms the CLI version, which decides whether the v3 or the v4 engine and config format apply.

  6. 6阴影与聚焦环对比

    class="shadow-lg ring-2 ring-blue-500 focus:outline-none focus:ring-2"

    shadow 提供静态层次感,ring 用于聚焦与交互态提示;对比两者在同一元素上的视觉重量,可以判断是否层级混乱——通常静态卡片用 shadow,表单控件用 ring。

Command Examples

A centred card in one line of utility classes

<div class="flex items-center justify-center min-h-screen bg-gray-100"> <div class="w-64 rounded-lg shadow-lg bg-white p-6"> <h2 class="text-lg font-bold text-gray-900">Title</h2> </div> </div>

min-h-screen fills the outer container to the viewport, and flex plus items-center plus justify-center centres on both axes. Inside, w-64 with rounded-lg and shadow-lg produces the standard card look with zero custom CSS.

A mobile-first three-column responsive grid

<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3"> <div class="rounded bg-white p-4">A</div> <div class="rounded bg-white p-4">B</div> <div class="rounded bg-white p-4">C</div> </div>

The unprefixed grid-cols-1 is the phone layout, and sm: / lg: breakpoints override it as the viewport widens, so you never need max-width media queries. Being mobile-first means desktop rules only apply when wider and can never drop the small-screen case.

Single-line text truncation

<p class="truncate max-w-sm"> A very long description that should collapse into one line with an ellipsis </p>

truncate already bundles overflow-hidden, text-ellipsis and whitespace-nowrap, but it requires a bounded width on the element. Without a max-w-* or flex constraint on the parent, the ellipsis simply never appears.

A button with dark mode support

<button class="px-4 py-2 rounded bg-blue-500 text-white transition hover:bg-blue-600 focus:outline-none focus:ring-2 active:scale-95 dark:bg-gray-800"> Save </button>

State classes are written together in the hover → focus → active order, and order changes the result. Removing the default focus ring with focus:outline-none without adding focus:ring-2 leaves keyboard users unable to see where they are.

Spacing and flex growth in a list row

<ul class="flex flex-col divide-y"> <li class="flex items-center justify-between px-4 py-3"> <span class="flex-1 truncate">Label</span> <span class="text-sm text-gray-500">Value</span> </li> </ul>

flex-1 lets the left label absorb the remaining width and truncate, while the right value stays unshrunk — that is the standard recipe for a table row that never wraps. gap-4 instead of per-element margin also avoids the stray leading and trailing spacing.

Common Pitfalls

  • A misspelled class never errors — it silently does nothing. truncate or size-8 spelled wrong still renders, so confirm the class exists in the compiled CSS before hunting layout bugs.
  • size-* only exists in Tailwind 3.4 and later; on older projects it is a no-op and you must fall back to w-* plus h-*.
  • A transformed or filtered ancestor turns position: fixed into positioning relative to that ancestor, dropping the element somewhere unexpected.
  • Class attributes cannot contain spaces to separate utilities; concatenating names dynamically without the separator yields one nonexistent class and silently loses all styling.
  • Tailwind scans content for literal strings, so classes built at runtime such as `text-${color}-500` never make it into the build and whole colour sets vanish in production.
  • dark: follows the system by default (media strategy); if the product needs a manual switch, set darkMode: "class" or the toggle appears to do nothing.

Tips

  • Tailwind uses mobile-first responsive design: sm: ≥640px, md: ≥768px, lg: ≥1024px
  • Use dark: prefix for dark mode, e.g. dark:bg-gray-800
  • Use @apply directive to reuse Tailwind classes in CSS
  • Common color scales: 50/100/200/300/400/500/600/700/800/900, higher = darker
  • JIT mode only compiles classes you actually use, resulting in minimal CSS

FAQ

Flex and Grid both handle multi-column layouts — when should I pick which?

Decide by dimensionality. A toolbar or a row of cards is one-dimensional: flex plus flex-row, gap-4 and justify-* is enough, and flex-1 lets a single item absorb the remaining space. A dashboard needs both axes: grid-cols-12 defines the skeleton, col-span-2 sizes each block, row-span-2 handles vertical spans. Do not reach for Grid when you only need a row, and do not nest Flex containers to fake two-dimensional alignment.

Why do sm: overrides work when they are written after the mobile styles?

Tailwind is mobile-first: base classes apply everywhere, and the sm:, md: and lg: prefixes compile to min-width media queries that naturally override those small-screen defaults. sm: is ≥640px, md: is ≥768px, lg: is ≥1024px. Because they use min-width, several prefixes can match at once and the later, larger one wins in the cascade — in flex-col md:flex-row sm:flex-row-reverse, medium screens get md and anything above 640px is overridden by the sm rule.

My element is hidden but still takes up space — which class should I use?

display: none removes the element from layout entirely and stops it responding to interaction, so the hidden class leaves no gap behind. The usual culprit is invisible, which maps to visibility: hidden — the element keeps its box and its place in the flow, it just stops painting. That is the whole difference between the two. Note that overflow-hidden only clips overflowing content rather than hiding the element, and z-[-1] pushes it behind the parent background, which is also not the same as hiding it.

How do I pick among colour scales from 50 to 900 — what does the number mean?

Within one hue the number tracks depth: 50-200 are near-white and suit light page backgrounds; 300-400 work for borders and separators; 500-600 are the mid-dark tones you want for body text and primary buttons because they hold enough contrast; 700-900 are for dark mode and emphasis. So bg-gray-100 with text-gray-900 is a safe pair, while text-gray-400 on white is often too faint. In dark mode flip both sides with prefixes, e.g. dark:bg-gray-800 dark:text-white, and prefer a lighter scale step over opacity-50 when you need lighter text.

Why does truncate do nothing on long text — which class am I missing?

truncate is actually three declarations combined: overflow-hidden clips the excess, text-ellipsis adds the dots, and white-space: nowrap stops the text from wrapping. Without nowrap the text folds onto a second line, the container grows taller, and the ellipsis never appears. The parent therefore also needs a definite width — w-64, max-w-lg, or flex-1 with min-w-0 inside a flex or grid parent — otherwise the box just keeps widening instead of overflowing. For multi-line clamping, do not reach for truncate; pair overflow-hidden with a line-clamp utility instead.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us