Tailwind CSS Cheatsheet - Flex, Grid & Spacing
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.
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
containerblock / inline-block / inlinehiddenrelative / absolute / fixed / stickyoverflow-auto / overflow-hiddenvisible / invisiblez-10 / z-50 / z-[-1]Flexbox 7
flexflex-row / flex-colflex-wrap / flex-nowrapjustify-start / justify-center / justify-between / justify-enditems-start / items-center / items-endflex-1 / flex-auto / flex-nonegap-4 / gap-x-4 / gap-y-4Grid 7
gridgrid-cols-3 / grid-cols-12grid-rows-3col-span-2 / col-span-fullrow-span-2 / row-span-fullplace-items-center / place-content-centergap-4Spacing & Sizing 8
p-4 / px-4 / py-2 / pt-4m-4 / mx-auto / my-2 / mt-4space-x-4 / space-y-4w-64 / w-1/2 / w-full / w-screenh-64 / h-1/2 / h-full / h-screenmax-w-lg / max-w-7xl / max-w-screen-xlmin-h-screen / min-h-0size-8 / size-16Typography 9
text-xs / text-sm / text-base / text-lg / text-xl / text-2xl ... text-9xlfont-thin / font-normal / font-medium / font-bold / font-blackleading-none / leading-tight / leading-relaxed / leading-loosetracking-tight / tracking-wide / tracking-widertext-left / text-center / text-right / text-justifyunderline / line-through / no-underlineuppercase / lowercase / capitalizetruncatewhitespace-nowrap / break-wordsColors & Background 7
text-red-500 / text-blue-600 / text-gray-900bg-white / bg-gray-100 / bg-blue-500border-gray-300 / border-red-500ring-2 ring-blue-500shadow-sm / shadow-md / shadow-lg / shadow-xlopacity-50 / opacity-100bg-gradient-to-r from-blue-500 to-purple-500Responsive & Interaction 9
sm: / md: / lg: / xl: / 2xl:hover:bg-blue-500focus:outline-none focus:ring-2active:scale-95dark:bg-gray-800 dark:text-whitetransition / transition-all / transition-colorshover:scale-110 / hover:rotate-6animate-spin / animate-ping / animate-pulse / animate-bouncecursor-pointer / cursor-not-allowedBorder & Effects 7
border / border-2 / border-4 / border-b-2border-solid / border-dashed / border-dotted / border-nonerounded-sm / rounded / rounded-lg / rounded-xl / rounded-fullring / ring-2 ring-insetshadow / shadow-md / shadow-lg / shadow-xl / shadow-2xloutline / outline-2 outline-offset-2blur-sm / blur-lg / brightness-50 / contrast-125 / grayscaleParameter matrix
| Parameter | Effect | Example |
|---|---|---|
container | Responsive container that centers itself and sets a max-width. | <div class="container"> |
flex / grid | Enables flexbox / grid layout. | <div class="flex"> |
flex-row / flex-col | Main-axis direction: row / column. | class="flex flex-col" |
justify-between / justify-center | Main-axis alignment. | class="flex justify-between" |
items-center | Cross-axis center alignment. | class="flex items-center" |
flex-1 / flex-none | Flexible grow / no grow. | class="flex-1" |
flex-wrap | Wraps when space runs out. | class="flex flex-wrap" |
gap-4 / gap-x-4 / gap-y-4 | Child spacing (all / horizontal / vertical). | class="flex gap-4" |
grid-cols-3 / grid-cols-12 | Defines a 3-column / 12-column grid. | class="grid grid-cols-3" |
col-span-2 / col-span-full | Spans 2 columns / the full row. | class="col-span-full" |
place-items-center | Grid alignment as a whole (centers rows and columns together). | class="grid place-items-center" |
p-4 / px-4 / pt-4 | Padding: all sides / horizontal / top. | class="px-4 pt-2" |
mx-auto | Automatic horizontal margin, which centres a block element. | class="mx-auto" |
space-x-4 / space-y-4 | Spacing between adjacent direct children only. | class="space-y-4" |
w-full / w-64 / w-1/2 | Width: fill the parent, a fixed 16rem, or half of it. | class="w-full" |
h-64 / h-full / min-h-screen | Height: fixed, fill the parent, or at least one screen tall. | class="min-h-screen" |
max-w-lg / max-w-7xl | Clamp the maximum width. | class="max-w-7xl" |
size-8 | Set width and height in one class (Tailwind 3.4+). | class="size-8" |
text-xs ... text-2xl | The font-size scale, from xs up to 2xl. | class="text-lg" |
font-normal / font-medium / font-bold | Font weight. | class="font-bold" |
leading-tight / leading-relaxed | Line height, tight or relaxed. | class="leading-relaxed" |
tracking-tight / tracking-wider | Letter spacing. | class="tracking-wider" |
truncate | Clip a single overflowing line and show an ellipsis. | class="truncate" |
whitespace-nowrap / break-words | Forbid wrapping, or break inside a long word. | class="whitespace-nowrap" |
uppercase / capitalize | Change the case of the rendered text. | class="uppercase" |
text-gray-900 / bg-gray-100 | Text and background colour, on a 50 to 900 scale. | class="text-gray-900" |
ring-2 ring-blue-500 | Width and colour of the focus ring. | class="focus:ring-2 ring-blue-500" |
shadow-sm / shadow-lg | Shadow elevation. | class="shadow-lg" |
opacity-50 | Opacity of the element as a whole. | class="opacity-50" |
bg-gradient-to-r from-blue-500 to-purple-500 | A left-to-right gradient background. | class="bg-gradient-to-r from-blue-500 to-purple-500" |
sm: / md: / lg: / xl: / 2xl: | Responsive breakpoint prefixes, mobile first. | class="grid-cols-1 md:grid-cols-3" |
hover:bg-blue-500 | Styles applied while the pointer is over the element. | class="hover:bg-blue-500" |
focus:outline-none focus:ring-2 | Focus styles, normally used to keep forms accessible. | class="focus:outline-none" |
active:scale-95 | Scale feedback for the pressed state. | class="active:scale-95" |
dark:bg-gray-800 dark:text-white | Styles that only apply in dark mode. | class="dark:bg-gray-800" |
transition / transition-colors | Transition animation, over all properties or colour only. | class="transition-colors" |
hover:scale-110 / hover:rotate-6 | Transform animation on hover. | class="hover:scale-110" |
animate-spin / animate-ping / animate-pulse / animate-bounce | The built-in looping animations. | class="animate-spin" |
rounded / rounded-lg / rounded-full | Corner radius: small, large, or fully round. | class="rounded-full" |
border / border-2 / border-b-2 | Border width. | class="border-2" |
border-dashed / border-dotted | Dashed or dotted border. | class="border-dashed" |
blur-sm / grayscale / contrast-125 | Filter effects. | class="grayscale" |
Common pitfalls
SymptomFlex children have no spacing at all between them, even though a margin class was written.
CauseA margin on the parent only affects its relationship with the outside, not the gaps between children; child spacing needs gap or space-*.
FixWrite gap-4 on the flex container (or space-x-4 / space-y-4) instead of adding margin to each child.
SymptomStyles with an md: or lg: prefix do not take effect in the browser even though the window is clearly wide.
CauseBreakpoints follow mobile-first min-width semantics: unprefixed styles are the default below large screens, and prefixed styles only override at that width and above; also the class names must be spelled out completely.
FixVerify the prefix is spelled correctly (md:grid-cols-3, not md:grid-cols-three), and put the base single-column form earlier in the class list and the wide-screen form later.
SymptomIn dark mode text and background are nearly the same color and the whole page is unreadable.
CauseOnly the background was changed (e.g. dark:bg-gray-800) without changing the text color, so the default dark text disappears against the dark background.
FixSet background and text colors in pairs, e.g. class="bg-white text-gray-900 dark:bg-gray-800 dark:text-white".
SymptomLong text breaks out of cards or buttons and pushes the layout into a horizontal scrollbar.
CauseMissing single-line overflow or word-break handling, and flex children default to min-width:auto so they do not shrink below their content.
FixAdd truncate or break-words to the text container; add min-w-0 to the flex child when you need tight control.
Symptomhover effects do not respond on phones and the element looks broken.
Causehover is a pointer-device-specific pseudo-class; touchscreens have no stable hover state, so the style does not change on tap.
FixAdd an active: state as a touch fallback for key interactions, paired with transition-colors for visible feedback.
SymptomThe utility classes you wrote are not generated into the stylesheet at all—nowhere in the build output.
CauseTailwind generates class names from the file contents it scans; class names written in template-string concatenation, dynamic variables, or paths missing from content are not picked up.
FixWrite class names as complete static literals, or add the relevant paths to the content config so the scan covers that file.
SymptomTwo flex-1 children have misaligned widths, or one child is squeezed to zero width.
CauseThe parent has no flex-wrap and the children differ widely in content width; combined with the default min-width, the flex ratio is not what you expect.
FixAdd flex-wrap to the parent and min-w-0 to children that need to shrink, so their minimum content width does not break the layout.
SymptomAn absolutely positioned element escapes to the edge of the page or gets clipped, and raising z-index changes nothing.
Causeabsolute resolves against the nearest positioned ancestor; when no ancestor is relative or absolute it falls back to a further container or the initial containing block, and an overflow-hidden ancestor can clip it on the way.
FixGive the parent you meant to use as the reference position: relative, then order the layers with z-10 or z-50.
Troubleshooting
1Confirm the breakpoints from one to multiple columns take effect.
class="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 gap-4"Write all three breakpoints and narrow the window in turn; in the browser you will see the column count switch at md (≥768px) and lg (≥1024px). If it stays at one column, first check the prefix spelling and whether the class names are being scanned and generated.
2Pin down the root cause of "zero spacing".
class="flex flex-col sm:flex-row gap-4" -> drop gap-4, swap in space-y-4, then compareStart with gap-4 for uniform spacing; when you want whitespace only in the vertical layout and it to clear automatically in the horizontal one, switch to space-y-4 (which affects only adjacent children), avoiding extra outer margins at the ends.
3Set light and dark themes in pairs.
class="bg-white text-gray-900 dark:bg-gray-800 dark:text-white"Write the base (light) and dark: color values in pairs, and check that every background change has a matching text or border change; a missing one is where contrast collapses.
4Handle long-text overflow.
class="truncate" / class="break-words" / parent "flex" + child "min-w-0"Use truncate for single-line clipping and break-words to allow wrapping; flex children inside cards need min-w-0, otherwise their minimum content width bursts the container and causes horizontal scrolling.
5Hover and touch feedback.
class="transition-colors hover:bg-blue-500 active:scale-95"transition-colors makes state changes smooth; hover covers pointer devices and active covers the touch tap moment. Together they reveal whether a selector is missing or only failing on touch.
6Compare a shadow against a focus ring
class="shadow-lg ring-2 ring-blue-500 focus:outline-none focus:ring-2"A shadow gives static depth while a ring signals focus and interaction, so looking at both on one element tells you whether the hierarchy has become muddled. Cards usually want a shadow; form controls want a 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}-500never 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