Skip to main content
⚑ OpenDevDocs
Tailwind CSS
frontendtailwindcssTroubleshootingIntermediate

Common Tailwind CSS Mistakes & How to Avoid Them

Diagnose and fix dynamic string interpolation bugs, class duplication, specificity issues, and improper @apply over-usage.

Verified: 2026-10-01
Tested on:tailwindcss 4.x
Platforms:
web
#tailwindcss#best-practices#troubleshooting#anti-patterns

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:

  1. You lose the benefit of local reasoning in your markup.
  2. Your CSS bundle size grows with every custom @apply class.
  3. 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>

On this page