-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Help! How can I fix the override issue between Tailwind v4 and Ant Design v5? #15822
-
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>
}
/>
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 9 replies
-
Could you provide a minimal reproducible example please?
As an aside, Adam Wathan (creator of Tailwind) does seem to advocate avoiding @apply
:
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
There's nothing wrong with writing custom CSS!
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
u can use variable to save the reusable tailwind classes
const sty = {
btn: "px-4 py-1 border",
input: "...",
...
}
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi, You may try this configuration.
- First you must wrap your
<App/>
with<StyleProvider layer>
. Source
//App.tsx import { StyleProvider } from '@ant-design/cssinjs'; <StyleProvider layer> <App {...props} /> </StyleProvider>
- 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);
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1 -
🚀 6
-
@zunnur-trinovik you're awesome! Thank you, that worked for me
Beta Was this translation helpful? Give feedback.
All reactions
-
@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.
Beta Was this translation helpful? Give feedback.
All reactions
-
@remkodejong You can try wrap the <StyleProvider>
with <ConfigProvider>
<ConfigProvider theme={{ ...CustomTheme }}>
<StyleProvider layer>
<App {...props} />
</StyleProvider>
</ConfigProvider>
reference: link
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
In my case just adding bellow line after 'tailwindcss' importing solve the problem.
@import 'tailwindcss';
@import 'tailwindcss/utilities.css' layer(utilities) important;
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
- https://tailwindcss.com/docs/preflight#overview
- https://ant.design/docs/react/compatible-style#layer
- How can I insert a custom layer between TailwindCSS's default base, components and utilities layers?
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> );
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
🎉 1