Tailwind v4 has no important
option support
#15866
-
I tried updating tailwind to v4 today, but I noticed there is no support for the In my case, I have a chrome extension which injects dom onto some pages. I use the important cause like this |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
You'd replace the @layer theme, base, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/preflight.css" layer(base);
@theme {
/* ... */
}
@layer utilities {
#my-extension {
@tailwind utilities;
}
} |
Beta Was this translation helpful? Give feedback.
-
After several attempts, I discovered that using the .app {
@import 'tailwindcss' source('../src');
} Compiled result: @layer theme, base, components, utilities;
@layer theme {
.app :root, .app :host {
--font-sans: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji",
"Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
...
}
}
@layer base {
.app *, .app ::after, .app ::before, .app ::backdrop, .app ::file-selector-button {
box-sizing: border-box;
...
}
.app html, .app :host {
line-height: 1.5;
-webkit-text-size-adjust: 100%;
...
}
...
}
@layer utilities {
.app .text-red-500 {
color: var(--color-red-500);
}
...
} Soon, I realized that selectors like In the end, I didn’t need to modify the My configuration: /* input.css */
@import 'tailwindcss' source('../src'); /* postcss.config.mjs */
export default {
plugins: {
'@tailwindcss/postcss': {},
'postcss-nested': {},
'postcss-prefix-selector': { prefix: '.app' },
},
} The If you don't need to transform CSS Nesting syntax for legacy browser compatibility, you can remove the 'postcss-prefix-selector': {
prefix: '.app',
exclude: [/^\s*\.app(?:\b|[\s.#[:])/],
transform: (prefix, selector, prefixedSelector, filePath, rule) => {
// Replace ':root', 'body', and 'html' with .app
if ([':root', 'body', 'html'].some(globalSel => selector.startsWith(globalSel))) {
return selector.replace(/(html\s+body|:root\s+body|html|:root|body)/gm, prefix);
}
// Skip nested rules — only apply prefix to root-level rules
if (rule.parent && rule.parent.type === 'rule') {
return selector;
}
// Apply prefix to top-level selector only
return prefixedSelector;
},
} Compiled result: .app .a {
.b {
...
}
} One more important note: if you're using multiple PostCSS plugins, you might encounter an issue where the prefix is added multiple times. For example: .app .app .a {
...
} To prevent this, you should add the 'postcss-prefix-selector': {
prefix: '.app',
exclude: [/^\s*\.app(?:\b|[\s.#[:])/],
} As I progressed with the migration to Tailwind v4, I encountered several unexpected issues. Did you think adding a prefix would solve all scoping problems? I used to think the same. However, v4 introduced the Example: <body class="app">
<div class="bg-green-500">Hello</div>
</body>
@layer utilities {
.app .bg-green-500 {
background-color: var(--color-green-500);
}
}
div {
background: red;
} In past experience, the selector There are two ways to resolve this issue:
Removing layers (layer(xxx)): @import 'tailwindcss/theme.css' source('../src');
@import 'tailwindcss/preflight.css' source('../src');
@import 'tailwindcss/utilities.css' source('../src'); See also: Using important: @import "tailwindcss" important; There are a few subtle changes in v4 that are not clearly highlighted in the documentation:
<div class="bg-me [--bg:red]">Hello</div> v3 theme config: export default {
theme: {
extend: {
colors: {
me: 'var(--bg)',
},
},
},
} v4 theme config: @theme {
--color-me: var(--bg);
} v3 output: .bg-me {
background-color: var(--bg);
}
.\[--bg\:red\] {
--bg: red;
} v4 output: @layer theme, base, components, utilities;
@layer theme {
:root, :host {
--color-me: var(--bg);
}
}
@layer utilities {
.bg-me {
background-color: var(--color-me);
}
.\[--bg\:red\] {
--bg: red;
}
} In v4, the variable v3 tailwind play https://play.tailwindcss.com/shIRdFxzH3 Performance Warning
Therefore, it's important to narrow the file scope as precisely as possible. Here’s my configuration:
Note: Adding |
Beta Was this translation helpful? Give feedback.
You'd replace the
@import "tailwindcss";
with: