Tailwind Styles Work in Dev but Disappear in Production? The Purge Fix Guide
October 4, 2026 | 9 min read | Tailwind CSS
By Syed · Frontend Engineer & Creator of UITweak
Everything looks perfect with npm run dev. You deploy, and half your styles are gone: buttons render unstyled, colors are missing, spacing collapsed. This is the most reported Tailwind issue of all time, and the cause is almost always the same. Tailwind only generates CSS for class names it can find as complete strings in your source files. Anything built dynamically at runtime gets purged. Work through the fixes below in order.
30-second diagnosis: pick one missing style, find its class in DevTools (say bg-red-500), then search your codebase for that exact string. If the full class name never appears literally in your source, only as pieces inside a template literal or concatenation, your problem is Fix 1. If it does appear literally, skip to Fix 3.
Why This Happens
Tailwind is not a traditional CSS file with every utility pre-written. It scans your project for class-like strings and generates CSS only for what it finds. That is what keeps the production bundle tiny. The tradeoff: the scanner reads text, it does not run your JavaScript. So when you write bg-${color}-500, the scanner sees the fragments bg- and -500 but never the complete bg-red-500, and no CSS is generated for it. In v4 the scanning is automatic (no content config needed), but the rule is identical: no literal string, no CSS.
Fix 1: Stop Building Class Names Dynamically
This fixes the majority of cases. Replace string interpolation with a lookup map where every class name appears as a complete string:
/* WRONG: the scanner never sees "bg-red-500" */
function Badge({ color }) {
return <span className={`bg-${color}-500 text-white`}>New</span>;
}
/* RIGHT: full class names as complete strings */
const COLOR_CLASSES = {
red: "bg-red-500",
blue: "bg-blue-500",
green: "bg-green-500",
};
function Badge({ color }) {
return (
<span className={`${COLOR_CLASSES[color]} text-white`}>New</span>
);
}The map keys can be dynamic; the values must be whole class names. The same applies to string concatenation ("bg-" + color + "-500") and array joins that assemble utilities from fragments. If a class name is cut into pieces anywhere, the scanner cannot reassemble it.
Fix 2: Safelist with @source inline() (v4)
When the set of dynamic classes is small and known, tell Tailwind to always generate them. In v4 the syntax is @source inline() in your CSS file:
@import "tailwindcss";
/* always generate these utilities, scanner or not */
@source inline("bg-red-500", "bg-blue-500", "bg-green-500");
/* with variants: braces expand the list */
@source inline("{hover,}bg-red-500");
@source inline("{sm,md,lg}:text-lg");Still on v3? The equivalent is the safelist array in tailwind.config.js. Either way, safelist only what you need: every safelisted utility ships to all users, so a giant safelist quietly undoes the bundle-size win. The lookup map from Fix 1 stays the better default.
Confirm it worked: rebuild and check the production CSS for your class. A quick way without digging through build output: paste your stylesheet, including the @source inline() line, into our free Tailwind Config Visualizer and inspect the generated CSS. If your utilities appear in the output there, the safelist registered correctly and the problem is elsewhere.
Fix 3: The Scanner Is Not Seeing Your Files
If the class does appear literally in your source but still vanishes, the scanner may not be reading that file. In v4, content detection is automatic, but it skips gitignored files and only looks inside your project. Common traps: components living in a sibling package outside the project root, a newly added directory, or a UI library in node_modules. Register extra sources explicitly:
@import "tailwindcss";
/* also scan a shared package outside the project root */
@source "../shared-ui/src";
/* scan a specific dependency (v4 ignores node_modules by default) */
@source "../node_modules/@acme/ui";In v3, the same problem shows up as an incomplete content array in the config. Either way, the test is the same: if the literal class is in a file the scanner never opens, the utility never gets generated.
Fix 4: Classes Coming from a CMS or API
Class names stored in a database, CMS, or API response are invisible to the scanner by definition: they do not exist in your source at build time. You have two honest options. If the set is finite (a status field with five values, a theme picker with eight themes), safelist them with @source inline() as in Fix 2. If the set is open-ended, stop sending raw class names from the backend and send semantic tokens instead ("danger", "success"), then map them to full class names on the frontend exactly like Fix 1. Letting a CMS inject arbitrary class strings is how you get both missing styles and a style-injection hole.
Fix 5: Rule Out a Stale Production Build
Sometimes the CSS is fine and the deploy is not. If the missing styles appear after a hard refresh or in an incognito window, your host is serving cached CSS: clear the build cache and redeploy (Vercel, Netlify, and Cloudflare all offer this), and check for a service worker holding onto an old stylesheet. This one is worth exactly two minutes of checking before you rewrite anything. Our dark mode fix checklist covers the same stale-build pattern in more detail.
Quick Recap
- Never assemble class names from fragments; use a lookup map of complete strings.
- Safelist small known sets with
@source inline()(v4) orsafelist(v3). - Make sure the scanner actually opens every file containing your classes.
- CMS/API-driven classes must be safelisted or mapped from semantic tokens.
- Clear build cache and rule out stale deploys before rewriting code.
# Expansion draft: tailwind-styles-missing-in-production-fix
Debugging Walkthrough: Tracing One Missing Class End to End
Fixes 1 through 5 tell you what can go wrong. Here is how I actually work through a live case, using the 30-second diagnosis from the main guide as the starting point.
Say bg-red-500 is missing on a badge. First I search the codebase for the literal string bg-red-500. If it appears nowhere, the class is assembled at runtime and I go read the component. Nine times out of ten I find a template literal like bg-${color}-500 or a variant built the same way: ${variant}-red-500, where even the hover: prefix is dynamic. That second form surprises people. Variant prefixes are part of the class name as far as the scanner is concerned, so hover:bg-red-500 must also appear literally. I fix it with the lookup map from Fix 1, rebuild, and confirm the utility is in the output CSS.
If the literal string does exist, I check where. A class that only appears in a test fixture or a gitignored directory is invisible to the scanner for practical purposes. Then I rebuild and search the built stylesheet for the selector. That twenty-second check turns hope into certainty, and it is the step people skip most.
One more trap I hit regularly: the class exists literally, the scanner sees it, the CSS contains it, and the style still does not apply. That is not a purge problem at all. It is a cascade problem, usually an unlayered rule beating the utility in v4. The layer-cascade guide covers that one in full.
Prevention Checklist: Team Rules That Kill This Bug Class
Individual fixes do not survive team growth. These three rules do:
1. **Ban string-built class names in code review.** Any PR that assembles a utility from fragments gets sent back. The lookup-map pattern is the approved alternative, and it belongs in the team's style guide, not just in one person's head.
2. **Keep one visible safelist for the exceptions.** Dynamic classes you genuinely need, like CMS status colors, live in a single @source inline() block with a comment per entry. Past a dozen entries, switch to the semantic-token mapping from Fix 4.
3. **Grep the production CSS for critical classes in CI.** A tiny script checking a handful of load-bearing selectors (brand colors, dark-mode selectors) catches scanner regressions before they ship.
Frequently Asked Questions
Why does it work in dev but break in production?
The scanner rule applies in both, but dev setups are more forgiving: broader file watching, looser caching, and the fact that you usually notice the break only after the production build runs the full pipeline. The class was almost always dynamic (or unscanned) all along; deploy is just where it becomes visible.
Can I just disable purging entirely?
You can safelist aggressively, but you should not. Shipping the full utility catalog turns a ~10KB stylesheet into hundreds of kilobytes for every visitor. Fix the class construction instead; it is usually a ten-minute change.
Does v4 still purge unused styles?
Yes. v4 made content detection automatic, but generation is still driven by the class strings it finds. The mental model did not change: if Tailwind cannot read the complete class name in your source, the CSS will not exist.
What about third-party components with Tailwind classes?
Their classes only generate if the scanner reads the library's files. Point @source at the package as shown in Fix 3, or check whether the library ships its own compiled CSS (in which case you do not need the scanner to see it at all).
My classes are in a JSON file. Does the scanner read it?
v4 scans all non-ignored text files, so JSON is read. But the rule still holds: the value must contain the complete class name as a string. "color": "red" assembled later into bg-red-500 fails exactly like the template-literal case. Store the full class name in the JSON instead.
Verify the fix: after applying the lookup map or @source inline(), paste your stylesheet into the free Tailwind Config Visualizer and confirm your utilities appear in the generated CSS. If they do, the next production build will include them.