Maaz Zindani

Loading the index…

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

A Tailwind class does not override the class before it

ErrorclassName="grid flex" renders as display: grid

Short answer

Tailwind does not read your class attribute from left to right. When two utilities set the same property, the one that comes later in the generated stylesheet wins, wherever it sits in className. Remove the conflicting class, pick one in code with a condition, or add the important modifier to force the winner.

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

Fix it fast

  1. 1Find out which cause you have

    Open the element in browser dev tools, look at the Styles pane, and find the property that is crossed out. The crossed-out rule is the losing class; the one above it is what the stylesheet ranked higher.

    // In the console, print the exact class string React rendered
    document.querySelector('#my-element').className
  2. 2Match what you see, then jump to its fix

    1. 1Both classes are in className and the one you wrote last is crossed out in dev toolsTwo utilities set the same property on one elementFix
    2. 2You cannot remove the other class, for example it comes from a third-party componentYou really need one specific utility to winFix
    3. 3A className prop from the parent does not replace the default class inside the childA component lets callers add extra classes that clash with its ownFix
    4. 4A utility such as rounded-none has no effect on an element that also has your custom classYour own component class should be overridable but is notFix

What does the error look like?

  • className="grid flex" renders as display: grid
  • A utility you added last has no effect on the element
  • A class passed from a parent component does not replace the child's default class

Cause 1 of 4

Two utilities set the same property on one element

1Check

Look for two utilities from the same group on one element, for example grid and flex, or two background colours. Tailwind's own page says the class that appears later in the stylesheet wins, not the class that appears later in the class attribute.

grep -rn "className=\"[^\"]*grid[^\"]*flex" src/

2Fix

Never put two conflicting utilities on one element. Choose the one you want in code, so only one is ever rendered.

export function Panel({ gridLayout }) {
  return <div className={gridLayout ? "grid" : "flex"}>{/* ... */}</div>;
}

3Verify

Inspect the element again: only one display utility is present and it is applied.

document.querySelector('#my-element').className

Confirmed by tailwindcss.com

Did this fix it?

Cause 2 of 4

You really need one specific utility to win

1Check

Confirm the other class is outside your control (a library component or a base class you cannot edit).

document.querySelector('#my-element').className

2Fix

Add an exclamation mark to the end of the utility. Tailwind then marks all its declarations as important. Keep this for cases where you have no other way to manage specificity.

<div className="bg-teal-500 bg-red-500!">...</div>

3Verify

The element now shows the red background, and the generated rule carries the important flag in dev tools.

getComputedStyle(document.querySelector('#my-element')).backgroundColor

Confirmed by tailwindcss.com

Did this fix it?

Cause 3 of 4

A component lets callers add extra classes that clash with its own

1Check

Open the child component and see whether it joins a default class string with props.className.

grep -rn "className=" src/components/

2Fix

Expose specific props for the variations you allow (such as a layout or size prop) and map each one to a single class, rather than accepting arbitrary extra classes.

export function Card({ layout = "stack" }) {
  return <div className={layout === "grid" ? "grid" : "flex"}>{/* ... */}</div>;
}

3Verify

Render the component with each prop value and check that only one class from the group is present.

document.querySelectorAll('[class*="grid"]').length

Confirmed by tailwindcss.com

Did this fix it?

Cause 4 of 4

Your own component class should be overridable but is not

1Check

Find where the custom class is defined and whether it sits inside a Tailwind layer. The docs put complex classes that you still want utilities to override into the components layer.

grep -rn "@layer" src/ app/

2Fix

Wrap the class in the components layer, so a utility on the element can override it.

@layer components {
  .card {
    border-radius: 1rem;
  }
}

3Verify

Use the class with a utility, for example class="card rounded-none", and check the corners are square.

getComputedStyle(document.querySelector('.card')).borderRadius

Confirmed by tailwindcss.com

Did this fix it?

How do I stop it coming back?

  • Pick between variants in JavaScript (a ternary or a lookup object) instead of concatenating two classes.
  • Give components named props for style choices, not a free className that callers stack on top.
  • Treat the important modifier as a last resort, because it makes the next override harder.

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

Next step

Want this handled for good?

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