2

So the prop value={defaultSystem} in the ChakraProvider it overwrite the styles from the TailwindCSS classes even in normal tags not a Chakra component.

Example**

<ChakraProvider value={defaultSystem}>
   <div className="mx-2 p-4 text-4xl">text</div>
</ChakraProvider>

These classes don't work, I have to make them important. But classes like bg-*, hidden and other works fine. I don't know if there is any other classes doesn't work but I noticed the padding, margin, text-size don't work even on normal tags.

4 Answers 4

1

TL;DR: You need to use the correct layer order (explanation below), like this:

@layer reset, tokens, theme, base, recipes, components, utilities;

Chakra UI with TailwindCSS. Sure?

The first and most important thing I'd point out is that Chakra UI wasn't designed to be used with Tailwind CSS, so using them together is somewhat discouraged.

Instead, you might want to try a fully TailwindCSS-based system like ShadCN or DaisyUI.

@layer

I assume you're using TailwindCSS v4. TailwindCSS v4 handles layers properly by default. It's important to understand that CSS layers have a priority order, but so-called 'unlayered' styles (those not assigned to any layer) will always take precedence over any layered styles. You can read more about that here:

So, TailwindCSS implicitly declares the following in the background through @import 'tailwindcss';:

@layer theme, base, components, utilities;

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

And Chakra UI does it like this:

@layer reset, base, tokens, recipes;

Solution #1 - CSS Specificity by properly layer ordering (recommended)

So it's clear that Chakra UI's reset layer has the highest priority, followed by everything else. Since TailwindCSS also includes a CSS reset called Preflight, this isn't entirely necessary. But we're essentially trying to merge two tools that were designed independently of each other.

Let's reduce the strength of the reset layer (solution instead of @import "tailwindcss";):

style.css

@layer reset, tokens, theme, base, recipes, components, utilities;

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

Disable preflight in ChakraUI

Extra: It may be worth disabling ChakraUI's preflight system, since TailwindCSS has its own, as ok_i suggested.

style.css

@layer tokens, theme, base, recipes, components, utilities;

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

theme.ts

export const system = createSystem(defaultConfig, {
  preflight: false, // Can disable ChakraUI's preflight
})

Disable preflight in TailwindCSS

Alternatively, you can disable TailwindCSS's preflight if you prefer to use ChakraUI's instead.

style.css

@layer reset, tokens, theme, base, recipes, components, utilities;

@import "tailwindcss/theme.css" layer(theme);
/* @import "tailwindcss/preflight.css" layer(base); */ /* Can disable TailwindCSS's preflight */
@import "tailwindcss/utilities.css" layer(utilities);

theme.ts

export const system = createSystem(defaultConfig, {
  preflight: true,
})

Solution #2 - Disable layers in both dependencies

Or disable layers in both systems (which I wouldn't recommend):

style.css

/* instead of @import "tailwindcss" */
@import "tailwindcss/theme.css"; /* without layer(theme) */
@import "tailwindcss/preflight.css"; /* without layer(base) */
@import "tailwindcss/utilities.css"; /* without layer(utilities) */

theme.ts

export const system = createSystem(defaultConfig, {
  disableLayers: true,
})
Sign up to request clarification or add additional context in comments.

Comments

0

just a reminder for me in v3

u can put this in the provider componenet

export const system = createSystem(defaultConfig, {
  preflight: false,
});

and pass it to the ChakraProvider as value

5 Comments

Sounds good. I'm not very experienced with Chakra UI, but I sometimes run into questions like this. So defaultConfig is the tailwind.config.js that I pass to createSystem, and Chakra then uses it?
no the defaultConfig are the styles from chakra chakra-ui.com/docs/get-started/…) and in the new system we made ,you pass the prop "preflight: false" will chakra not to override the tailwindclasses
Now, I don't understand how ChakraUI's layers can be weaker than TailwindCSS's without any extra configuration (like here: stackoverflow.com/a/79674300/15167500) - could you explain?
I see. You're disabling the reset CSS, but even then ChakraUI remains stronger than TailwindCSS. The goal is for TailwindCSS to have higher priority, and for you to be able to adjust ChakraUI's appearance when needed, right? I believe the solution you provided doesn't produce the expected result, and someone who doesn't fully understand CSS layers might draw misleading conclusions from it. I still believe that properly configuring the CSS layers should be the correct solution.
Of course, as an additional step it’s not a bad idea to disable ChakraUI’s preflight, since TailwindCSS has its own preflight as well. That’s a good point.
-1

Try with the below code

import { CacheProvider } from '@emotion/react';
import createCache from '@emotion/cache';
import { ChakraProvider } from '@chakra-ui/react';
import App from './App';

const chakraCache = createCache({
  key: 'chakra',
  prepend: true, // ensure Chakra styles are loaded before others
});

export default function Root() {
  return (
    <CacheProvider value={chakraCache}>
      <ChakraProvider>
        <App />
      </ChakraProvider>
    </CacheProvider>
  );
}

2 Comments

Why try this code?
It doesn't solve the layers issue, because in CSS layers the load order doesn't matter when styles are placed in different layers. This suggestion doesn't work on its own without additional explanation.
-1

Disable Chakra’s Global Styles

<ChakraProvider resetCSS={false} theme={defaultSystem}>
  <div className="mx-2 p-4 text-4xl">This is your text.</div>
</ChakraProvider>

This way, Chakra doesn’t interfere with Tailwind classes on native HTML elements.

Wrap Only Chakra Components in Provider

If your Tailwind app is the core and Chakra is used for a few components:

<div className="mx-2 p-4 text-4xl">This is your text.</div>

<ChakraProvider theme={defaultSystem}>
  <SomeChakraComponent />
</ChakraProvider>

1 Comment

Okay, you're not using them together, but then what's the point of merging them? It's pretty hard to manage a project when you rely on two independent frameworks that can't work together. Either you merge them properly, or you shouldn't use Chakra UI with TailwindCSS - but this way you're just sweeping the problem under the rug until the first point where you actually need both to work together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.