Sort Tailwind classes automatically with the official Prettier plugin (Tailwind v4 and v3 setup), include class helpers, and keep class lists readable.
.cursor/rules/tailwind-css-class-sorting.mdc. Downloaded files need to be placed in that repository path.---
description: "Sort Tailwind classes automatically with the official Prettier plugin (Tailwind v4 and v3 setup), include class helpers, and keep class lists readable."
globs: "**/*"
alwaysApply: false
---
Apply these rules to Tailwind class ordering.
## Principles
- Class order should be consistent and produced by a tool, not by hand, so diffs stay small and reviewers can scan class lists.
- Use the official `prettier-plugin-tailwindcss`. It orders classes the way Tailwind generates CSS: classes from the base and components layers first, then utilities grouped by the property they affect, with modifier variants (`hover:`, `focus:`, `md:`, `dark:`) after the unprefixed classes. Do not maintain a different manual order.
## Setup
```json
{
"plugins": ["prettier-plugin-tailwindcss"],
"tailwindStylesheet": "./src/app/globals.css",
"tailwindFunctions": ["clsx", "cn", "cva", "tw"]
}
```
- Tailwind v4: set `tailwindStylesheet` to the CSS entry file that contains `@import "tailwindcss"`.
- Tailwind v3: use `tailwindConfig` pointing at `tailwind.config.js` instead.
- List the helper functions the project uses in `tailwindFunctions` so classes inside `clsx(...)`, `cn(...)` and `cva(...)` are sorted too; use `tailwindAttributes` for non-`class` attributes.
- The plugin requires Prettier 3. If the project also uses other Prettier plugins that format the same files, check the plugin README for compatibility.
- Run `prettier --check` in CI so unsorted classes fail the build.
## Naming and custom classes
- Use Tailwind's utilities and theme tokens; add custom utilities with `@utility` (v4) or a plugin instead of ad-hoc CSS classes that compete with utilities.
- Extract repeated class lists into a component or a variant helper (`cva`) first. Use `@apply` only where you cannot change the markup (third-party HTML, CMS content).
## Readability
- When an element's class list gets hard to read, extract a component or split variants out; the number of classes alone is not a problem.
## Pitfalls
- Hand-sorted classes that drift from the tool's order.
- Building class names by string concatenation (`bg-${color}-500`): Tailwind cannot detect them, and the sorter cannot sort them. Map props to complete class names instead.
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
coding
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A Tailwind v4 + React project has hand-ordered class lists in three different styles and many classes inside cn() calls.
Using the Tailwind CSS Class Sorting rule, add automatic sorting. CSS entry: src/styles/app.css; helpers: cn (clsx + tailwind-merge) and cva.
A Prettier config adding prettier-plugin-tailwindcss with tailwindStylesheet ./src/styles/app.css and tailwindFunctions ["cn", "cva"], a package.json format/check script, a CI step running prettier --check, and a note to replace dynamic class-name concatenation with complete class names.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/tailwind-css-class-sorting.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "Tailwind CSS Class Sorting project conventions"
globs: "**/*"
alwaysApply: false
---
Apply these rules to Tailwind class ordering.
## Principles
- Class order should be consistent and produced by a tool, not by hand, so diffs stay small and reviewers can scan class lists.
- Use the official `prettier-plugin-tailwindcss`. It orders classes the way Tailwind generates CSS: classes from the base and components layers first, then utilities grouped by the property they affect, with modifier variants (`hover:`, `focus:`, `md:`, `dark:`) after the unprefixed classes. Do not maintain a different manual order.
## Setup
```json
{
"plugins": ["prettier-plugin-tailwindcss"],
"tailwindStylesheet": "./src/app/globals.css",
"tailwindFunctions": ["clsx", "cn", "cva", "tw"]
}
```
- Tailwind v4: set `tailwindStylesheet` to the CSS entry file that contains `@import "tailwindcss"`.
- Tailwind v3: use `tailwindConfig` pointing at `tailwind.config.js` instead.
- List the helper functions the project uses in `tailwindFunctions` so classes inside `clsx(...)`, `cn(...)` and `cva(...)` are sorted too; use `tailwindAttributes` for non-`class` attributes.
- The plugin requires Prettier 3. If the project also uses other Prettier plugins that format the same files, check the plugin README for compatibility.
- Run `prettier --check` in CI so unsorted classes fail the build.
## Naming and custom classes
- Use Tailwind's utilities and theme tokens; add custom utilities with `@utility` (v4) or a plugin instead of ad-hoc CSS classes that compete with utilities.
- Extract repeated class lists into a component or a variant helper (`cva`) first. Use `@apply` only where you cannot change the markup (third-party HTML, CMS content).
## Readability
- When an element's class list gets hard to read, extract a component or split variants out; the number of classes alone is not a problem.
## Pitfalls
- Hand-sorted classes that drift from the tool's order.
- Building class names by string concatenation (`bg-${color}-500`): Tailwind cannot detect them, and the sorter cannot sort them. Map props to complete class names instead.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.