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/rules/tailwind-css-best-practices.mdc. Downloaded files need to be placed in that repository path.---
description: "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."
globs: "**/*"
alwaysApply: false
---
Apply these rules when styling with Tailwind CSS. They target Tailwind v4; if the project is on v3 (it has a `tailwind.config.js` and `@tailwind` directives), keep its existing config style.
## Class order
- Let `prettier-plugin-tailwindcss` sort classes instead of ordering them by hand. Do not put comments inside `className` strings: in JSX, `{/* ... */}` inside a string literal is emitted as class names.
- Long class lists are a signal to extract a component, not to add comments.
## Responsive design
- Mobile first: unprefixed utilities apply to every width; `sm:`, `md:`, `lg:`, `xl:`, `2xl:` apply from that breakpoint up.
```tsx
<div className="grid grid-cols-1 gap-4 p-4 sm:grid-cols-2 sm:gap-6 sm:p-6 lg:grid-cols-3 lg:gap-8 lg:p-8 xl:grid-cols-4">
```
- Use container queries (`@container` on the parent, `@sm:`/`@md:` on children) for components whose layout should depend on their container rather than the viewport.
## Component variants
```tsx
import { cva, type VariantProps } from 'class-variance-authority';
import { twMerge } from 'tailwind-merge';
export const buttonVariants = cva(
'inline-flex items-center justify-center rounded-md font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 disabled:pointer-events-none disabled:opacity-50',
{
variants: {
variant: {
default: 'bg-primary text-primary-foreground hover:bg-primary/90',
destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90',
outline: 'border border-input bg-background hover:bg-accent hover:text-accent-foreground',
ghost: 'hover:bg-accent hover:text-accent-foreground',
},
size: {
default: 'h-10 px-4 py-2',
sm: 'h-9 px-3 text-sm',
lg: 'h-11 px-8 text-lg',
icon: 'size-10',
},
},
defaultVariants: { variant: 'default', size: 'default' },
}
);
export type ButtonVariantProps = VariantProps<typeof buttonVariants>;
export const buttonClass = (props: ButtonVariantProps & { className?: string }) =>
twMerge(buttonVariants(props), props.className);
```
- Merge caller overrides with `tailwind-merge` so `className="px-2"` replaces the variant's padding instead of conflicting with it.
## Dark mode
- By default `dark:` follows the operating system (`prefers-color-scheme`). For a user toggle, switch `dark:` to a class in your main CSS file:
```css
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
```
```tsx
<div className="border-gray-200 bg-white text-gray-900 dark:border-gray-700 dark:bg-gray-900 dark:text-white">
```
- The toggle sets or removes `dark` on `<html>`, stores the choice (for example in `localStorage`), falls back to the system preference when nothing is stored, and applies the class before first paint (an inline script in `<head>`) to avoid a flash. Give icon-only toggle buttons an `aria-label`.
## Configuration (v4)
- Configure the theme in CSS with `@theme`; each variable creates matching utilities (`--color-primary-500` → `bg-primary-500`):
```css
@import "tailwindcss";
@plugin "@tailwindcss/typography";
@plugin "@tailwindcss/forms";
@theme {
--color-primary-50: oklch(0.97 0.02 230);
--color-primary-500: oklch(0.68 0.15 230);
--color-primary-900: oklch(0.38 0.08 230);
--font-sans: "Inter", sans-serif;
--font-mono: "JetBrains Mono", monospace;
--animate-fade-in: fade-in 0.5s ease-out;
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
}
```
- v4 detects source files automatically; add `@source "../path"` only for files it misses (for example a component library in `node_modules`).
- A JavaScript `tailwind.config.js` is not detected automatically in v4. Load it with `@config "./tailwind.config.js";` only while migrating.
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 React with TypeScript: typed function components, React 19 ref-as-prop, hooks discipline, state ownership, error boundaries and measured performance work.
Cursor
frontend
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A Next.js app on Tailwind v4 still has a tailwind.config.js with darkMode: 'class' that is no longer loaded, and buttons whose className strings contain {/* Layout */} comments.
Apply the Tailwind CSS Best Practices rule to src/app/globals.css, tailwind.config.js and src/components/ui/button.tsx.
globals.css gains @custom-variant dark and an @theme block carrying the old colors, fonts and animation; plugins move to @plugin; tailwind.config.js is removed (or loaded with @config during migration); button.tsx uses cva plus tailwind-merge and has no comments inside class strings; class order is left to prettier-plugin-tailwindcss.
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-best-practices.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 Best Practices project conventions"
globs: "**/*"
alwaysApply: false
---
Apply these rules when styling with Tailwind CSS. They target Tailwind v4; if the project is on v3 (it has a `tailwind.config.js` and `@tailwind` directives), keep its existing config style.
## Class order
- Let `prettier-plugin-tailwindcss` sort classes instead of ordering them by hand. Do not put comments inside `className` strings: in JSX, `{/* ... */}` inside a string literal is emitted as class names.
- Long class lists are a signal to extract a component, not to add comments.
## Responsive design
- Mobile first: unprefixed utilities apply to every width; `sm:`, `md:`, `lg:`, `xl:`, `2xl:` apply from that breakpoint up.
```tsx
<div className="grid grid-cols-1 gap-4 p-4 sm:grid-cols-2 sm:gap-6 sm:p-6 lg:grid-cols-3 lg:gap-8 lg:p-8 xl:grid-cols-4">
```
- Use container queries (`@container` on the parent, `@sm:`/`@md:` on children) for components whose layout should depend on their container rather than the viewport.
## Component variants
```tsx
import { cva, type VariantProps } from 'class-variance-authority';
import { twMerge } from 'tailwind-merge';
export const buttonVariants = cva(
'inline-flex items-center justify-center rounded-md font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 disabled:pointer-events-none disabled:opacity-50',
{
variants: {
variant: {
default: 'bg-primary text-primary-foreground hover:bg-primary/90',
destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90',
outline: 'border border-input bg-background hover:bg-accent hover:text-accent-foreground',
ghost: 'hover:bg-accent hover:text-accent-foreground',
},
size: {
default: 'h-10 px-4 py-2',
sm: 'h-9 px-3 text-sm',
lg: 'h-11 px-8 text-lg',
icon: 'size-10',
},
},
defaultVariants: { variant: 'default', size: 'default' },
}
);
export type ButtonVariantProps = VariantProps<typeof buttonVariants>;
export const buttonClass = (props: ButtonVariantProps & { className?: string }) =>
twMerge(buttonVariants(props), props.className);
```
- Merge caller overrides with `tailwind-merge` so `className="px-2"` replaces the variant's padding instead of conflicting with it.
## Dark mode
- By default `dark:` follows the operating system (`prefers-color-scheme`). For a user toggle, switch `dark:` to a class in your main CSS file:
```css
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
```
```tsx
<div className="border-gray-200 bg-white text-gray-900 dark:border-gray-700 dark:bg-gray-900 dark:text-white">
```
- The toggle sets or removes `dark` on `<html>`, stores the choice (for example in `localStorage`), falls back to the system preference when nothing is stored, and applies the class before first paint (an inline script in `<head>`) to avoid a flash. Give icon-only toggle buttons an `aria-label`.
## Configuration (v4)
- Configure the theme in CSS with `@theme`; each variable creates matching utilities (`--color-primary-500` → `bg-primary-500`):
```css
@import "tailwindcss";
@plugin "@tailwindcss/typography";
@plugin "@tailwindcss/forms";
@theme {
--color-primary-50: oklch(0.97 0.02 230);
--color-primary-500: oklch(0.68 0.15 230);
--color-primary-900: oklch(0.38 0.08 230);
--font-sans: "Inter", sans-serif;
--font-mono: "JetBrains Mono", monospace;
--animate-fade-in: fade-in 0.5s ease-out;
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
}
```
- v4 detects source files automatically; add `@source "../path"` only for files it misses (for example a component library in `node_modules`).
- A JavaScript `tailwind.config.js` is not detected automatically in v4. Load it with `@config "./tailwind.config.js";` only while migrating.
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 React with TypeScript: typed function components, React 19 ref-as-prop, hooks discipline, state ownership, error boundaries and measured performance work.