跪拜 Guibai
← All articles
Frontend

A Tailwind CSS Cheatsheet That Cuts Through the Noise

By 时光不负努力 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Tailwind's utility-class surface is large enough that a condensed lookup table saves real time during layout work. The v3-to-v4 migration notes are immediately useful for anyone upgrading a project and hitting changed shadow scales or the new opacity syntax.

Summary

Layout patterns boil down to three Flex and Grid combinations: centering, space-between, and equal-width columns. Spacing maps directly to a 4px scale, with `p-4` for 16px and `w-1/2` for 50% width. Text sizing, color opacity, border radius, and responsive breakpoints follow a mobile-first syntax where `lg:` targets laptops and the default state handles phones.

Version 4 syntax changes get a dedicated comparison table. Opacity moves from `bg-opacity-50` to a slash shorthand like `bg-black/50`, shadow scales shift down one level, and custom colors move from JS config into CSS `@theme` blocks. The import line collapses three directives into a single `@import "tailwindcss"`.

An escape hatch section covers arbitrary values in square brackets, and the whole thing ends with a rhyming mnemonic that encodes the core workflow: flex parents, mobile-first breakpoints, slash opacity, and bracket overrides.

Takeaways
Three Flex/Grid combos handle most layouts: centering, space-between, and equal-width columns.
Spacing numbers multiply by 4px; `p-4` is 16px, `gap-3` is 12px.
v4 opacity uses a slash shorthand (`bg-black/50`) instead of a separate `bg-opacity-50` utility.
v4 shadow utilities shifted: old `shadow-sm` is now `shadow-xs`, and `shadow-md` is the new `shadow-sm`.
Custom colors in v4 are defined in CSS via `@theme { --color-xxx: #fff; }` rather than `tailwind.config.js`.
Arbitrary values in square brackets (`w-[200px]`, `bg-[#f0f0f0]`) override any missing or calculated style.
Mobile-first responsive prefixes use `sm:` for 640px, `md:` for 768px, and `lg:` for 1024px.
Conclusions

The v4 opacity shorthand is a genuine ergonomic win that eliminates a long-standing class-bloat pattern.

Shadow scale renaming in v4 is a breaking change that will silently alter visual hierarchy on upgrade unless caught.

Moving theme tokens from JS config to CSS `@theme` aligns Tailwind more closely with native CSS custom properties and reduces framework-specific config knowledge.

Concepts & terms
Arbitrary values in Tailwind
Square-bracket syntax like `w-[200px]` or `bg-[#f0f0f0]` that lets developers use any CSS value directly in a class name without defining a custom utility, serving as an escape hatch when the default scale doesn't cover a need.
Tailwind v4 opacity shorthand
A new syntax in Tailwind v4 where color opacity is appended with a slash, e.g., `bg-black/50` for 50% opacity, replacing the older two-class pattern `bg-black bg-opacity-50`.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗