Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A Tailwind class with square brackets produces no CSS or the wrong CSS

Errorgrid-cols-[1fr 500px 2fr] has no effect

Short answer

Tailwind turns a class in square brackets into CSS only when the whole string is in a scanned file and the value is valid. A space must be written as an underscore, and a CSS variable needs a type hint so Tailwind knows if it is a size or a colour. Strings built at runtime are never found.

  • Applies to Tailwind CSS 4, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the browser dev tools on the element and check whether the generated class exists in the stylesheet. No rule for your class means Tailwind did not generate it; a rule with the wrong property means it guessed wrongly.

    grep -rn "\\[" src/ --include=*.jsx --include=*.tsx | grep className | head
  2. 2Match what you see, then jump to its fix

    1. 1The class contains a space and no CSS appears for it.A space was typed inside the bracketsFix
    2. 2A variable-based text class does not give the property you meant.A CSS variable is ambiguous, so Tailwind picks the wrong utility or noneFix
    3. 3w-[${width}px] works in the code but no CSS is generated.The class name is built from a variable, so it never appears in the sourceFix

What does the error look like?

  • grid-cols-[1fr 500px 2fr] has no effect
  • A class built on a CSS variable, such as text-[var(--size)], generates the wrong property or nothing

Cause 1 of 3

A space was typed inside the brackets

1Check

A class name cannot contain a space, so the browser sees two classes. Tailwind's own rule is to write an underscore, and it converts it to a space at build time.

grep -rn "grid-cols-\[" src/

2Fix

Replace each space with an underscore.

<div className="grid grid-cols-[1fr_500px_2fr]">

3Verify

The grid shows three columns with the sizes you gave.

npm run dev

Confirmed by tailwindcss.com

Did this fix it?

Cause 2 of 3

A CSS variable is ambiguous, so Tailwind picks the wrong utility or none

1Check

Tailwind infers the property from the value. A variable could be a size or a colour, so the docs say to add a type before it.

grep -rn "text-\[var\|text-(--" src/

2Fix

In Tailwind 4, use the shorthand with a type hint.

<p className="text-(length:--my-var)">Size</p>
<p className="text-(color:--my-var)">Colour</p>

3Verify

Dev tools shows font-size for the first and color for the second.

npm run dev

Confirmed by tailwindcss.com

Did this fix it?

Cause 3 of 3

The class name is built from a variable, so it never appears in the source

1Check

Tailwind scans files as plain text and cannot interpret string interpolation. The class must exist as a complete string.

grep -rn 'w-\[\${' src/

2Fix

Use a complete class for each choice.

const widths = { sm: 'w-[240px]', lg: 'w-[480px]' };
<div className={widths[size]} />

3Verify

Both classes appear in the generated stylesheet.

npm run build

Confirmed by tailwindcss.com

Did this fix it?

How do I stop it coming back?

  • Write arbitrary values as complete strings and use underscores for spaces.
  • Add length: or color: to a variable shorthand when the value could be both.
  • Use a mapping object for choices; never concatenate class names.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Hydration failed after adding next-themes dark mode

Console reports a server and client mismatch on the html element

next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.

3 causes, each with a fix

Web apps

shadcn/ui setup fails in a Vite React project using Tailwind v4

Imports from @/components/ui do not resolve in the editor or the build

shadcn init needs three things in a Vite project: Tailwind v4 installed with the @tailwindcss/vite plugin and a single @import "tailwindcss" line in your CSS, the @/* path alias in both tsconfig.json and tsconfig.app.json, and the same alias in vite.config.ts, with @types/node installed so path resolves.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.