1---2name: tailwindcss-best-practices3description: Tailwind CSS v4 implementation and review guidance. Use when installing Tailwind, styling UI, building responsive or dark-mode layouts, extending CSS-first themes and utilities, diagnosing class detection, or migrating from v3. Covers stable v4.3 behavior; inspect the project's installed version before applying version-specific advice.4---56# Tailwind CSS v4 Best Practices78> Documentation snapshot: Tailwind CSS v4.3.3, verified against the npm `latest` tag and official documentation on 2026-08-29. Recheck https://tailwindcss.com/docs and the npm tag when the user asks for the latest version.910Tailwind v4 is CSS-first: `@theme` defines design tokens, `@utility` registers utilities, and `@custom-variant` registers variants. Legacy JavaScript configuration is still available through `@config`, but it is not detected automatically and some v3 options are unsupported.1112## Working Method13141. **Inspect the project before editing.** Identify the installed `tailwindcss` version, package manager, build integration, main CSS entry point, legacy config or plugins, source roots, and existing design tokens/components. Do not upgrade a project merely because this skill documents a newer version.152. **Choose the integration that matches the stack.** Prefer the framework's official guide when one exists. Use `@tailwindcss/vite` for Vite-based stacks, `@tailwindcss/postcss` for PostCSS-based stacks such as Next.js, `@tailwindcss/webpack` as a webpack loader, or `@tailwindcss/cli` for a standalone build. Read [installation](references/core-installation.md) before changing build configuration.163. **Use the smallest styling mechanism that fits.** Prefer an existing utility or component; use an arbitrary value for a true one-off; add an `@theme` token when a design value is reused; add `@utility` only for a reusable CSS capability; use custom CSS for third-party overrides or rules that are clearer as CSS.174. **Keep candidates statically detectable.** Map props and states to complete class strings. Do not construct fragments such as `` `bg-${color}-500` ``. Read [source detection](references/core-source-detection.md) for monorepos, external packages, exclusions, and safelists.185. **Preserve the existing design system.** Reuse its spacing, color, typography, radius, component, and class-merging conventions. Avoid introducing arbitrary values when a matching token already exists.196. **Verify the observable result.** Run the relevant build/type/lint/tests, confirm expected utilities appear in compiled CSS when detection is in question, and inspect responsive, state, dark-mode, focus, and reduced-motion behavior in the rendered UI when applicable.2021## v4.3 Additions2223| Capability | Use |24|---|---|25| Scrollbars | `scrollbar-auto`, `scrollbar-thin`, `scrollbar-none`, `scrollbar-thumb-*`, `scrollbar-track-*`, and `scrollbar-gutter-*`; see [scrollbars](references/effects-scrollbars.md) |26| Size containers | `@container-size` or `@container-size/{name}` when queries or container units need block-size information; see [responsive design](references/core-responsive.md) |27| Zoom | `zoom-*`, `zoom-[value]`, and `zoom-(--property)` for CSS `zoom`; see [transforms](references/transform-base.md) |28| Tab size | `tab-*`, `tab-[value]`, and `tab-(--property)` for rendered tab width; see [typography](references/typography-font-text.md) |29| Variants in CSS | `@variant hover:focus` for stacked variants and `@variant hover, focus` for a shared block |30| Functional utilities | `--default(...)` inside `--value(...)` or `--modifier(...)` when the bare utility needs a fallback; see [functions and directives](references/features-functions-directives.md) |3132## Critical v4 Migration Gotchas3334These are the most common mistakes when working with Tailwind v4. If you're migrating from v3 or using v4 for the first time, read the [upgrade guide](references/features-upgrade.md) — but here are the top trip-ups:3536| v3 (old) | v4 (correct) | Why it changed |37|----------|-------------|----------------|38| `!bg-red-500` | `bg-red-500!` | Important modifier moved from prefix to suffix |39| `bg-opacity-75` | `bg-red-500/75` | Opacity modifiers removed; use slash syntax on the color |40| `shadow` | `shadow-sm` | Shadow scale shifted down one step |41| `shadow-sm` | `shadow-xs` | Shadow scale shifted down one step |42| `rounded` | `rounded-sm` | Border radius scale shifted down one step |43| `rounded-sm` | `rounded-xs` | Border radius scale shifted down one step |44| `ring` | `ring-3` | Default ring width changed from 3px to 1px |45| `outline-none` | `outline-hidden` | Renamed for clarity |46| `flex-shrink-0` | `shrink-0` | Shorter alias is now the only form |47| `flex-grow` | `grow` | Shorter alias is now the only form |48| `overflow-ellipsis` | `text-ellipsis` | Renamed for consistency |49| `blur` | `blur-sm` | Blur scale shifted down one step |50| `@tailwind base/components/utilities` | `@import "tailwindcss"` | Single CSS import replaces three directives |51| Automatically detected `tailwind.config.js` | CSS-first directives, or explicit `@config` for compatibility | JS config is no longer auto-detected |52| `darkMode: 'class'` | `@custom-variant dark (&:where(.dark, .dark *));` | Dark mode config moves to CSS |53| `bg-[--var]` | `bg-(--var)` | CSS variable arbitrary values use parentheses |54| `theme(screens.xl)` | `theme(--breakpoint-xl)` | Theme function uses CSS variable names |55| `@layer utilities { }` | `@utility name { }` | Custom utilities use dedicated directive |56| `start-*` / `end-*` | `inset-s-*` / `inset-e-*` | Deprecated in v4.2 |5758## Core References5960| Topic | Description | Reference |61|-------|-------------|-----------|62| Installation | Vite, PostCSS, Webpack, CLI, and CDN setup | [core-installation](references/core-installation.md) |63| Utility Classes | Understanding Tailwind's utility-first approach | [core-utility-classes](references/core-utility-classes.md) |64| Theme Variables | Design tokens, `@theme` directive, theme variable namespaces | [core-theme](references/core-theme.md) |65| Responsive Design | Mobile-first breakpoints, responsive variants, container queries | [core-responsive](references/core-responsive.md) |66| Variants | Conditional styling with state, pseudo-class, media query, and pointer variants | [core-variants](references/core-variants.md) |67| Preflight | Tailwind's base styles and how to extend or disable them | [core-preflight](references/core-preflight.md) |68| Source Detection | How Tailwind detects classes, `@source`, `@source not`, `@source inline()` | [core-source-detection](references/core-source-detection.md) |6970## Layout7172### Display & Flexbox & Grid7374| Topic | Description | Reference |75|-------|-------------|-----------|76| Display | flex, grid, block, inline, hidden, sr-only, flow-root, contents | [layout-display](references/layout-display.md) |77| Flexbox | flex-direction, justify, items, gap, grow, shrink, wrap, order | [layout-flexbox](references/layout-flexbox.md) |78| Grid | grid-cols, grid-rows, gap, place-items, col-span, row-span, subgrid | [layout-grid](references/layout-grid.md) |79| Aspect Ratio | Controlling element aspect ratio for responsive media | [layout-aspect-ratio](references/layout-aspect-ratio.md) |80| Columns | Multi-column layout for magazine-style or masonry layouts | [layout-columns](references/layout-columns.md) |8182### Positioning8384| Topic | Description | Reference |85|-------|-------------|-----------|86| Position | Controlling element positioning with static, relative, absolute, fixed, and sticky | [layout-position](references/layout-position.md) |87| Inset | Placement of positioned elements with inset, logical inset (`inset-s-*`, `inset-bs-*`), and deprecated `start-*`/`end-*` | [layout-inset](references/layout-inset.md) |8889### Sizing9091| Topic | Description | Reference |92|-------|-------------|-----------|93| Width | Setting element width with spacing scale, fractions, container sizes, viewport units | [layout-width](references/layout-width.md) |94| Height | Setting element height with spacing scale, fractions, viewport units | [layout-height](references/layout-height.md) |95| Min & Max Sizing | min-width, max-width, min-height, max-height constraints | [layout-min-max-sizing](references/layout-min-max-sizing.md) |96| Logical Sizing | Writing-mode-aware sizing: `inline-*`, `block-*`, `min-inline-*`, `max-block-*` (v4.2) | [layout-logical-properties](references/layout-logical-properties.md) |9798### Spacing99100| Topic | Description | Reference |101|-------|-------------|-----------|102| Margin | Margins with spacing scale, negative values, logical properties (`mbs-*`, `mbe-*`) | [layout-margin](references/layout-margin.md) |103| Padding | Padding with spacing scale, logical properties (`pbs-*`, `pbe-*`) | [layout-padding](references/layout-padding.md) |104105### Overflow106107| Topic | Description | Reference |108|-------|-------------|-----------|109| Overflow | Controlling how elements handle content that overflows | [layout-overflow](references/layout-overflow.md) |110111### Images & Replaced Elements112113| Topic | Description | Reference |114|-------|-------------|-----------|115| Object Fit & Position | Controlling how images and video are resized and positioned | [layout-object-fit-position](references/layout-object-fit-position.md) |116117### Tables118119| Topic | Description | Reference |120|-------|-------------|-----------|121| Table Layout | border-collapse, table-auto, table-fixed | [layout-tables](references/layout-tables.md) |122123## Transforms124125| Topic | Description | Reference |126|-------|-------------|-----------|127| Transform Base | Base transform utilities, hardware acceleration, custom transform values | [transform-base](references/transform-base.md) |128| Translate | Translating elements on x, y, z axes with spacing scale and percentages | [transform-translate](references/transform-translate.md) |129| Rotate | Rotating elements in 2D and 3D space | [transform-rotate](references/transform-rotate.md) |130| Scale | Scaling elements uniformly or on specific axes | [transform-scale](references/transform-scale.md) |131| Skew | Skewing elements on x and y axes | [transform-skew](references/transform-skew.md) |132133## Typography134135| Topic | Description | Reference |136|-------|-------------|-----------|137| Font & Text | Font size, weight, color, line-height, letter-spacing, decoration, truncate, `wrap-break-word`, `wrap-anywhere` | [typography-font-text](references/typography-font-text.md) |138| Text Align | Controlling text alignment with left, center, right, justify | [typography-text-align](references/typography-text-align.md) |139| List Style | list-style-type, list-style-position for bullets and markers | [typography-list-style](references/typography-list-style.md) |140141## Visual142143| Topic | Description | Reference |144|-------|-------------|-----------|145| Background | Background color, gradient, image, size, position | [visual-background](references/visual-background.md) |146| Border | Border width, color, radius, divide, ring, block border utilities (`border-bs-*`, `border-be-*`) | [visual-border](references/visual-border.md) |147| Effects | Box shadow, opacity, mix-blend, backdrop-blur, filter, colored drop shadows | [visual-effects](references/visual-effects.md) |148| SVG | fill, stroke, stroke-width for SVG and icon styling | [visual-svg](references/visual-svg.md) |149| Text Shadow | Text shadow sizes, colors, and opacity modifiers (v4.1) | [effects-text-shadow](references/effects-text-shadow.md) |150| Mask | Composable mask utilities with gradient and radial masks (v4.1) | [effects-mask](references/effects-mask.md) |151| Scrollbars | Width, thumb/track colors, and stable gutter utilities (v4.3) | [effects-scrollbars](references/effects-scrollbars.md) |152153## Effects & Interactivity154155| Topic | Description | Reference |156|-------|-------------|-----------|157| Transition & Animation | CSS transitions, animation keyframes, reduced motion | [effects-transition-animation](references/effects-transition-animation.md) |158| Visibility & Interactivity | Visibility, cursor, pointer-events, user-select, z-index | [effects-visibility-interactivity](references/effects-visibility-interactivity.md) |159| Form Controls | accent-color, appearance, caret-color, resize | [effects-form-controls](references/effects-form-controls.md) |160| Scroll Snap | scroll-snap-type, scroll-snap-align for carousels | [effects-scroll-snap](references/effects-scroll-snap.md) |161162## Features163164### Dark Mode165166| Topic | Description | Reference |167|-------|-------------|-----------|168| Dark Mode | Dark mode with `dark:` variant, `@custom-variant`, class and data-attribute strategies | [features-dark-mode](references/features-dark-mode.md) |169170### Migration171172| Topic | Description | Reference |173|-------|-------------|-----------|174| Upgrade Guide | Migrating from v3 to v4, all renamed/removed utilities, scale shifts, config migration | [features-upgrade](references/features-upgrade.md) |175176### Customization177178| Topic | Description | Reference |179|-------|-------------|-----------|180| Custom Styles | Adding custom styles, utilities with `@utility`, variants with `@custom-variant`, arbitrary values | [features-custom-styles](references/features-custom-styles.md) |181| Functions & Directives | Tailwind's CSS directives (`@theme`, `@utility`, `@custom-variant`, `@source`) and functions | [features-functions-directives](references/features-functions-directives.md) |182| Content Detection | How Tailwind detects classes, `@source` configuration, safelisting with `@source inline()` | [features-content-detection](references/features-content-detection.md) |183184## Best Practices185186| Topic | Description | Reference |187|-------|-------------|-----------|188| Utility Patterns | Managing duplication, conflicts, important modifier, when to use components | [best-practices-utility-patterns](references/best-practices-utility-patterns.md) |189190## Key Recommendations191192- Prefer utilities and existing components over new abstraction layers.193- Define reusable design tokens with `@theme`; use `:root` for CSS variables that should not generate utilities.194- Treat unprefixed responsive utilities as the mobile baseline and breakpoint variants as overrides.195- Keep full candidate strings in source; map dynamic inputs to static class lists.196- Use `@custom-variant` to define a variant and `@variant` to apply an existing variant inside CSS.197- Prefer CSS-first configuration for new v4 work; retain `@config` or `@plugin` only when compatibility requires it.198- Use consistent units for breakpoints; the official defaults use `rem`, and mixed units can produce unexpected ordering.199- Do not use Play CDN in production or add Sass/Less/Stylus in front of Tailwind v4.