Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Help! How can I fix the override issue between Tailwind v4 and Ant Design v5? #15822

Unanswered
skt-developer-007 asked this question in Help
Discussion options

I upgraded Tailwind from v3 to v4, and this issue came up.

I removed the padding styles from Ant Design to control them myself and added !important to py-[4rem] to make it override Ant Design’s styles. However, I now want to set a separate style for the Ant Design Input component with py-[1rem], but it’s not working as expected.

How can I fix this issue?

globals.css

@import "tailwindcss";
@layer components {
 .form-control {
 @apply py-[4rem]! px-[.7rem] text-sm outline-none border border-slate-300 hover:border-green-600 rounded-md focus:border-green-600 focus:ring-2 focus:ring-green-100;
 }
}
.ant-input-affix-wrapper {
 padding: 0 !important;
}

page.jsx

<Input
 placeholder='ค้นหา'
 className="form-control py-[1rem]!"
 suffix={
 <div className='bg-web-green rounded-full cursor-pointer' onClick={() => console.log(1)}> 
 <FontAwesomeIcon icon={faSearch} className='opacity-50' />
 </div>
 }
/>
You must be logged in to vote

Replies: 3 comments 9 replies

Comment options

Could you provide a minimal reproducible example please?


As an aside, Adam Wathan (creator of Tailwind) does seem to advocate avoiding @apply:

You must be logged in to vote
3 replies
Comment options

If I don't use @apply, what alternatives do I have to achieve this without writing custom CSS?.
Sorry, I used a translator to translate the language. I’m Thai and not very good at English.

Comment options

wongjn Jan 24, 2025
Collaborator

There's nothing wrong with writing custom CSS!

Comment options

u can use variable to save the reusable tailwind classes

const sty = {
btn: "px-4 py-1 border",
input: "...",
...
}

Comment options

Hi, You may try this configuration.

  1. First you must wrap your <App/> with <StyleProvider layer>. Source
//App.tsx
import { StyleProvider } from '@ant-design/cssinjs';
<StyleProvider layer>
 <App {...props} />
</StyleProvider>
  1. Then, use layer to reorder it to give tailwind priority to override ant design styling. source
/*globals.css*/
@layer antd, theme, base, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/utilities.css" layer(utilities);
You must be logged in to vote
4 replies
Comment options

@zunnur-trinovik you're awesome! Thank you, that worked for me

Comment options

@zunnur-trinovik thanks for your post, this really helps, we still have issues with the global styling from Ant Design though, say we have in our themeconfig the following:

export const customTheme = {
...
token: {
 ...
 linkHoverDecoration: "underline",
 colorLinkHover: "var(--custom-blue)",
 ....
}
...

Then these values don't get picked up by the app.
I'm kinda hesistant to add className="hover:text-(--custom-color-link)! hover:underline!" to every link component in the codebase.

Any ideas how to fix this? If you need more info, let me know.

We're using Vite, React 19, React-Router, Ant Design 5 and trying to implement Tailwind v4.

Comment options

@remkodejong You can try wrap the <StyleProvider> with <ConfigProvider>

<ConfigProvider theme={{ ...CustomTheme }}>
 <StyleProvider layer>
 <App {...props} />
 </StyleProvider>
</ConfigProvider>

reference: link

Comment options

This solution did not completely eliminate the problem. The Alert component is unable to read colors correctly and fully during the initial rendering process. I haven't found a solution for this issue yet.

Comment options

In my case just adding bellow line after 'tailwindcss' importing solve the problem.

@import 'tailwindcss';
@import 'tailwindcss/utilities.css' layer(utilities) important;
You must be logged in to vote
2 replies
Comment options

Hi. Your problem is caused by the fact that TailwindCSS uses layers, while the antd package does not. Unlayered styles (like those from antd) are stronger than any layered styles.

I think a more refined solution than using !important is to handle this by placing all antd styles into a layer. Then, you need to set the priority between layers so that TailwindCSS utilities are stronger than antd, but TailwindCSS's preflight (reset CSS) is weaker than antd.

Your main CSS file use this instead of @import "tailwindcss";:

@layer theme, base, antd, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/preflight.css" layer(base);
@import "tailwindcss/utilities.css" layer(utilities);

And should use StyleProvider:

import { StyleProvider } from '@ant-design/cssinjs';
import { ConfigProvider } from 'antd';
export default () => (
 <StyleProvider layer>
 <ConfigProvider>
 <MyApp />
 </ConfigProvider>
 </StyleProvider>
);
Comment options

Now it's working. I was adding StyleProvider wrapper in the wrong place. I'm using antd and tailwindcss with nextjs project by following antd docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /