styling · maxtrigify — Claude Code rule: what it does, how to install
styling is a Claude Code rule that enforces Tailwind CSS patterns. Guides the agent to combine classes with tailwind-merge, clsx, and CVA.
Apache-2.0 ★ 141 updated 2026-11-17
.claude/rules/styling.mdWhat styling does
The rule instructs coding assistants to merge Tailwind utilities via a shared cn helper that combines clsx and tailwind-merge. Without resolving conflicts through tailwind-merge, competing utility classes stay in the markup and break component styling.
Class composition must follow a strict order: base styling first, component variants second, state-driven styles third, and incoming className overrides last. When handling multi-state elements, the rule requires creating variant maps with class-variance-authority outside the render function and generating types via VariantProps.
Dynamic interpolation like string templates is disallowed because Tailwind cannot scan constructed utility names; instead, arbitrary dynamic values must pass via CSS custom properties in inline styles. The project must have clsx, tailwind-merge, and class-variance-authority installed to implement these code patterns.
What it helps with
- Prevent Tailwind class conflicts during props merging
- Structure component variant states with CVA
- Pass dynamic runtime styles through CSS variables
- Enforce consistent base, variant, and override class ordering
Example prompts for the agent
- Create a Button component using CVA variants and the cn utility
- Refactor this Card so consumer className props override default styles
- Add state-dependent styles without generating dynamic Tailwind class strings
How to install styling
Manually: put the files in place
.claude/rules/styling.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/styling.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/styling.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/styling.mdManually: put the files in place
.clinerules/styling.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about styling
How to install styling in Claude Code?
Save the rule text into .claude/rules/styling.md inside your project directory.
What does the styling rule do?
It directs the agent to structure Tailwind CSS components using tailwind-merge, clsx, and CVA with a four-tier class priority order.
What is required for styling to work?
Your repository needs the clsx, tailwind-merge, and class-variance-authority dependencies.
Source text
styling.md · Apache-2.0
---
title: Component Styling with Tailwind CSS
impact: HIGH
impactDescription: Predictable, maintainable styling patterns
tags: styling, tailwind, cn, clsx, tailwind-merge, cva, class-variance-authority
---
## Component Styling with Tailwind CSS
Use Tailwind CSS with intelligent class merging (`tailwind-merge`), conditional classes (`clsx`), and variant APIs (CVA).
### The `cn` Utility Function
```tsx
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
```
**Why:** Without `tailwind-merge`, conflicting classes both apply. The `cn` utility resolves conflicts intelligently.
### Class Merging
**Incorrect:**
```tsx
// Without tailwind-merge, conflicting classes both apply
className = "bg-red-500 bg-blue-500"; // Both classes apply, causing conflicts
className = "px-4 py-2 px-8"; // Both px-4 and px-8 apply
```
**Correct:**
```tsx
twMerge("bg-red-500", "bg-blue-500"); // "bg-blue-500"
twMerge("px-4 py-2", "px-8"); // "py-2 px-8"
twMerge("text-sm", "text-lg"); // "text-lg"
```
### Component Pattern: Order Matters
Apply classes in this order:
1. **Base styles** (always applied)
2. **Variant styles** (based on props)
3. **Conditional styles** (based on state)
4. **User overrides** (className prop)
**Incorrect:**
```tsx
// Wrong order: user className comes before variants, preventing overrides
className={cn(className, variant === 'primary' && 'bg-blue-500')}
```
**Correct:**
```tsx
const Component = ({
className,
variant,
isActive,
...props
}: ComponentProps) => {
return (
<div
className={cn(
"rounded-lg border bg-white shadow-sm", // 1. Base
variant === "primary" && "bg-blue-500", // 2. Variants
isActive && "ring-2 ring-blue-500", // 3. Conditionals
className // 4. User overrides
)}
{...props}
/>
);
};
```
### Conditional Classes with `clsx`
```tsx
cn("base", isActive && "active");
cn("base", { active: isActive, disabled: isDisabled });
cn(["base", isLarge ? "text-lg" : "text-sm"]);
```
### Class Variance Authority (CVA)
For components with multiple variants:
```tsx
import { cva, type VariantProps } from "class-variance-authority";
const buttonVariants = cva(
"inline-flex items-center justify-center gap-2 rounded-md text-sm font-medium transition-all",
{
variants: {
variant: {
default: "bg-blue-500 text-white hover:bg-blue-600",
destructive: "bg-red-500 text-white hover:bg-red-600",
outline: "border border-gray-300 bg-transparent hover:bg-gray-50",
ghost: "hover:bg-gray-100",
},
size: {
default: "h-9 px-4 py-2",
sm: "h-8 px-3 text-sm",
lg: "h-10 px-6",
icon: "h-9 w-9",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
);
type ButtonProps = React.ComponentProps<"button"> &
VariantProps<typeof buttonVariants>;
export const Button = ({ className, variant, size, ...props }: ButtonProps) => {
return (
<button
className={cn(buttonVariants({ variant, size }), className)}
{...props}
/>
);
};
```
**Key points:**
- Define CVA variants **outside** the component
- Use `VariantProps<typeof variants>` for TypeScript types
- Always merge with `className` prop using `cn`
### Best Practices
**1. Extract Repeated Patterns:**
```tsx
export const focusRing = 'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500';
export const disabled = 'disabled:pointer-events-none disabled:opacity-50';
className={cn(focusRing, disabled, className)}
```
**2. Use CSS Variables for Dynamic Values:**
**Incorrect:**
```tsx
// Dynamic class generation (not detected by Tailwind)
<div className={`bg-[${dynamicColor}]`} />
```
**Correct:**
```tsx
// CSS variables
<div
className="bg-[var(--color)]"
style={{ "--color": dynamicColor } as React.CSSProperties}
/>
```
**3. Document Variants:**
```tsx
type ButtonProps = {
/** The visual style @default "default" */
variant?: "default" | "destructive" | "outline" | "ghost";
/** The size @default "default" */
size?: "sm" | "default" | "lg" | "icon";
};
```
### Common Patterns
**State-Based Styling:**
```tsx
<div
className={cn(
"transition-all",
isOpen ? "opacity-100 translate-y-0" : "opacity-0 translate-y-4"
)}
/>
```
**CVA with Additional Conditionals:**
```tsx
<div
className={cn(
baseVariants({ variant, size }),
isActive && "ring-2 ring-blue-500",
isDisabled && "opacity-50 cursor-not-allowed",
className
)}
/>
```
© maxtrigify · Apache-2.0 · maxtrigify/Otto
Similar IDE rules
All IDE rules for Frontend and UI →styling by other authors comes first.