A Tailwind class does not override the class before it
className="grid flex" renders as display: gridShort 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.
Fix it fast
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').className2Match what you see, then jump to its fix
- 1
Both 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
You cannot remove the other class, for example it comes from a third-party componentYou really need one specific utility to winFix - 3
A 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
A 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
- 1
What does the error look like?
className="grid flex" renders as display: gridA utility you added last has no effect on the elementA 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').classNameConfirmed by tailwindcss.com
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').className2Fix
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')).backgroundColorConfirmed by tailwindcss.com
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"]').lengthConfirmed by tailwindcss.com
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')).borderRadiusConfirmed by tailwindcss.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
There is no tailwind.config.js after installing Tailwind 4 and HeroUI in Next.js
HeroUI components render unstyled
Nothing is broken: Tailwind CSS 4 is configured in CSS and no longer creates tailwind.config.js.
3 causes, each with a fixWeb 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 fixWeb apps
A shadcn/ui Dialog stays open after the form is submitted
Form submits and the data saves, but the modal remains on screen
A shadcn Dialog is a Radix dialog.
3 causes, each with a fixWeb apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fixWeb apps
react-i18next shows the wrong plural form for a count, or prints the key instead of the text
t('item') prints the key item instead of a translated sentence
i18next picks a plural from the count option and looks for keys ending in _one, _other and so on.
4 causes, each with a fixWeb apps
useSession throws because there is no SessionProvider above it
[next-auth]: `useSession` must be wrapped in a <SessionProvider />
useSession reads the session from React Context, and SessionProvider is what creates that context.
3 causes, each with a fix