Remove --tw- variables from universal selector #7317
-
Hey everybody, at the moment I'm facing a problem with tailwind variables that are declared in the universal selector *, :after, :before { }. The Problem is, that css variables in the universal selector seem to cause a super high CPU load on older iOS devices on every interaction(click, touch etc.). If I remove them from the universal selector, everything seems to be fine. My generated tailwind.css file has this universal selector:
Is it somehow possible to remove the variables declaration from the universal selector and move them to the specific class in which they are needed. I took a look at other tailwind.css files and it seems that it is somehow possible. For example on tailwindcss.com only the --tw-content variable is declared in the universal selector:
To demonstrate what the problem on iOS devices , I screen captured the CPU usage of a iPad mini with tailwind variables declared in the universal selector. What you see is what happens if a empty div is touched: Screen.Recording.2022-02-03.at.16.53.39.movand here without the variables: Screen.Recording.2022-02-03.at.16.57.26.movthere is a huge difference in CPU usage so the website is barely usable because the interface is blocked for nearly 5 seconds I hope somebody has an idea, what I am trying to achieve here. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 12 replies
-
Hey! Try this: // tailwind.config.js
module.exports = {
experimental: {
optimizeUniversalDefaults: true
}
// ...
} We tried to make this strategy the default for v3 but it's not compatible with CSS modules or component-specific styles in Svelte/Vue components so we had to revert to using the universal stuff by default. We use that option on the tailwindcss.com site though, and as long as you are using Tailwind the recommended way (as just a simple boring global stylesheet without CSS modules or any other scoped styles that cause PostCSS to run multiple times in isolation) you should be good. |
Beta Was this translation helpful? Give feedback.
-
Hi @adamwathan , I have tried this option, and I end up having some strange CSS, that I am not sure is expected. For instance, using the universal selector I end up with one selector like that: And later another rule that simply seems to override it: I am also using some components beyond utility, and I end up having those kind of strange selectors: Is this the expected output of this option? I feel a way better approach here would be to try to implement that: #8135 to strip down unused variables from both the generated selectors and the universal selectors. I think that most people do not even use half of those variables. A very naive approach that would simply state for instance "if no sepia filter class is used in the whole codebase, then sepia variable is not outputted" would work I think. |
Beta Was this translation helpful? Give feedback.
-
As a tag up to this, I recently experienced an issue where the To fix it, I had to manually add 0913.mp4 |
Beta Was this translation helpful? Give feedback.
-
Big universal rules are also pretty detrimental to the UX of dev tools, at least in chromium browsers. It shows up on every parent element of the selected element unnecessarily, completely striked through because that rule matches all lower elements. It makes the inspector view several times longer with pure garbage. DevTools.-.www.meetup.com_devs-gent_.2024-10-17.15-53-53.mp4In a deep DOM this gets really bad and also grinds the performance of dev tools to a halt as it renders thousands of pointless elements. |
Beta Was this translation helpful? Give feedback.
-
@Inwerpsel PostCSS JIT Props might be worth trying. By example: https://stackblitz.com/edit/github-7wftaw Honestly, I think Tailwind v4 would be your best bet even if it's still in alpha. It really depends on your project's browser support requirements. There's probably good reason why they were placed on the By adding JIT Props, you can place them within the Install PostCSS JIT Props. Remove the // tailwind.config.cjs
experimental: {
optimizeUniversalDefaults: true
} Now add the Create a module.exports = {
plugins: [
require('postcss-import'),
require('tailwindcss'),
require('postcss-jit-props')({
'--tw-border-spacing-x': '0',
'--tw-border-spacing-y': '0',
'--tw-translate-x': '0',
'--tw-translate-y': '0',
'--tw-rotate': '0',
'--tw-skew-x': '0',
'--tw-skew-y': '0',
'--tw-scale-x': '1',
'--tw-scale-y': '1',
'--tw-pan-x': ' ',
'--tw-pan-y': ' ',
'--tw-pinch-zoom': ' ',
'--tw-scroll-snap-strictness': 'proximity',
'--tw-gradient-from-position': ' ',
'--tw-gradient-via-position': ' ',
'--tw-gradient-to-position': ' ',
'--tw-ordinal': ' ',
'--tw-slashed-zero': ' ',
'--tw-numeric-figure': ' ',
'--tw-numeric-spacing': ' ',
'--tw-numeric-fraction': ' ',
'--tw-ring-inset': ' ',
'--tw-ring-offset-width': '0px',
'--tw-ring-offset-color': '#fff',
'--tw-ring-color': 'rgb(59 130 246 / 0.5)',
'--tw-ring-offset-shadow': '0 0 #0000',
'--tw-ring-shadow': '0 0 #0000',
'--tw-shadow': '0 0 #0000',
'--tw-shadow-colored': '0 0 #0000',
'--tw-blur': ' ',
'--tw-brightness': ' ',
'--tw-contrast': ' ',
'--tw-grayscale': ' ',
'--tw-hue-rotate': ' ',
'--tw-invert': ' ',
'--tw-saturate': ' ',
'--tw-sepia': ' ',
'--tw-drop-shadow': ' ',
'--tw-backdrop-blur': ' ',
'--tw-backdrop-brightness': ' ',
'--tw-backdrop-contrast': ' ',
'--tw-backdrop-grayscale': ' ',
'--tw-backdrop-hue-rotate': ' ',
'--tw-backdrop-invert': ' ',
'--tw-backdrop-opacity': ' ',
'--tw-backdrop-saturate': ' ',
'--tw-backdrop-sepia': ' ',
'--tw-contain-size': ' ',
'--tw-contain-layout': ' ',
'--tw-contain-paint': ' ',
'--tw-contain-style': ' '
}),
require('autoprefixer')
]
} |
Beta Was this translation helpful? Give feedback.
Hey! Try this:
We tried to make this strategy the default for v3 but it's not compatible with CSS modules or component-specific styles in Svelte/Vue components so we had to revert to using the universal stuff by default.
We use that option on the tailwindcss.com site though, and as long as you are using Tailwind the recommended way (as just a simple boring global stylesheet without CSS modules or any other scoped styles that cause PostCSS to run multiple times in isolation) you should be good.