Common Tailwind CSS Mistakes & How to Avoid Them
Diagnose and fix dynamic string interpolation bugs, class duplication, specificity issues, and improper @apply over-usage.
Common Tailwind CSS Mistakes & How to Avoid Them
1. Constructing Class Names Dynamically (The #1 Bug)
The Mistake:
// β BROKEN: Tailwind's static compiler cannot extract partial strings
function Card({ color }: { color: "blue" | "red" }) {
return <div className={`bg-${color}-500 text-white`} />;
}Why it fails:
Tailwind scans your source files with a static regex parser at build time. It searches for complete, unbroken string literals like "bg-blue-500". Because "bg-${color}-500" is not a complete class name, Tailwind never compiles that CSS class into the bundle!
The Fix:
Always write complete, unbroken class names using a mapping object:
// β
CORRECT: Full unbroken class strings
const colorStyles = {
blue: "bg-blue-500",
red: "bg-red-500",
};
function Card({ color }: { color: "blue" | "red" }) {
return <div className={`${colorStyles[color]} text-white`} />;
}2. Overusing @apply to Recreate Semantic CSS
The Mistake:
/* β Premature abstraction */
.custom-nav-button {
@apply px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500;
}Why it hurts:
- You lose the benefit of local reasoning in your markup.
- Your CSS bundle size grows with every custom
@applyclass. - You reintroduce CSS class naming fatigue.
The Fix:
Extract a reusable React component instead of a CSS class:
// β
CORRECT: Abstract in JSX/React
export function NavButton({ children, ...props }: ButtonProps) {
return (
<button className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700" {...props}>
{children}
</button>
);
}3. Desktop-First Thinking in Breakpoints
The Mistake:
Assuming sm: targets mobile devices:
<!-- β WRONG: Applies text-sm on desktop, but text-xl on mobile! -->
<h1 class="text-xl sm:text-sm">Title</h1>The Fix:
Tailwind is mobile-first. The un-prefixed class applies to mobile, and prefixes like md: apply to tablets/desktops:
<!-- β
CORRECT: text-sm on mobile, text-xl on desktop (md+) -->
<h1 class="text-sm md:text-xl">Title</h1>Related Topics
Tailwind CSS with React 19
Patterns for dynamic class composition, conditional styling, props-driven UI variants, and state transitions in React applications.
Tailwind CSS Performance
How Tailwind achieves sub-millisecond build performance, zero runtime overhead, minimal CSS bundle footprint, and efficient dead code elimination.