Tailwind v4 arbitrary values not working: five causes and fixes
Fix broken arbitrary values in Tailwind CSS v4: parentheses for CSS variables bg-(--x), underscores for spaces, dynamic strings, type hints and @source.
- Tailwind CSS 4.3.5
When a Tailwind CSS v4 arbitrary class like w-[420px], bg-[--brand] or grid-cols-[1fr 2fr] has no effect in the browser, Tailwind either couldn't parse the syntax, didn't scan the file, or needed a type hint. Here are the five causes that account for almost every case.
Five causes and fixes
Step 1: Using v3 bracket syntax for CSS variables
In Tailwind v4, shorthand CSS variables use parentheses, not square brackets:
css-var-fix.htmlHTML <!-- Wrong in v4 --><div class="bg-[--card-bg] text-[--card-fg]"> <!-- Right in v4 --><div class="bg-(--card-bg) text-(--card-fg)"><!-- Or explicit var() inside brackets --><div class="bg-[var(--card-bg)]">Step 2: Spaces inside brackets instead of underscores
HTML splits class attributes on whitespace. Replace every space inside an arbitrary value with an underscore (
_):spaces-fix.htmlHTML <!-- Wrong: browser reads two broken classes --><div class="grid grid-cols-[240px 1fr]"> <!-- Right --><div class="grid grid-cols-[240px_1fr]">Step 3: Building class names dynamically in JavaScript
Tailwind scans source files as plain text — it never executes JavaScript. String interpolation like
`w-[${width}px]`will never generate CSS. Use an inlinestyleprop for runtime numbers, or map to complete static class strings:dynamic-fix.tsxTSX // Wrong: Tailwind cannot see the value<div className={`w-[${width}px]`} /> // Right: CSS variable or inline style<div style={{ width }} />Step 4: Ambiguous CSS variable type (color vs length)
Utilities like
text-(--x)can mean eitherfont-sizeorcolor. Add a type hint when Tailwind picks the wrong property:text-(length:--heading-size)ortext-(color:--heading-color).Step 5: File is outside Tailwind's scanned paths
Tailwind v4 automatically ignores
node_modulesand paths listed in.gitignore. If a shared UI package or generated file holds your classes, point Tailwind at it with@source:src/styles/global.cssCSS @import "tailwindcss";@source "../packages/ui/src";
Questions
How do I escape a literal underscore inside an arbitrary value (like a URL)?
Prefix it with a backslash: bg-[url('https://example.com/hero\_bg.webp')]. Otherwise Tailwind turns the underscore into a space.
Why did my custom colour token not work even with @theme?
Check Tailwind v4 custom colors — the variable must start with --color- inside @theme.
Related articles
- Tailwind CSS v4Tailwind v4 custom colors: how to add them, and why they don’t workAdd a --color-* variable in @theme and every colour utility follows. The four reasons a custom colour silently does nothing, each reproduced.
- Tailwind CSS v4Tailwind CSS v4 vs v3: what changed and how to upgradeCompare Tailwind CSS v4 and v3: @theme in CSS, OKLCH colours, renamed utilities, breaking defaults, and the exact upgrade steps with @tailwindcss/upgrade.
- Tailwind CSS v4Tailwind v4 @apply not working: unknown utility class errorStyle blocks in Astro, Vue and Svelte and CSS modules need @reference in v4. Custom classes need @utility. The error, the fix and the other causes.
Still stuck? We answer every message ourselves. Write to us