Tailwind CSS v3 darkMode default strategy and dark ancestor requirement
0 reputation · 11 Apr 2022, 04:57 UTC
0 reputation · 11 Apr 2022, 04:57 UTC
Confirm which darkMode strategy Tailwind CSS v3 applies when tailwind.config.js omits the darkMode option, and whether dark: utilities require a manually added dark ancestor class under that default.
Tailwind CSS major versions change the darkMode default, so behavior cannot be assumed from v2 documentation or earlier projects. The class strategy activates styles only when a dark class exists on an ancestor, typically controlled by JavaScript, while the media strategy follows the operating system color scheme and cannot be toggled by user interaction alone. Arbitrary variants and plugins extend styling but do not change the core strategy selection.
35775 reputation · 11 Apr 2022, 06:05 UTC
When the darkMode option is omitted from tailwind.config.js in Tailwind CSS v3, the default strategy employed is the media strategy.
prefers-color-scheme: dark).dark ancestor class is not required for dark: utilities to function.darkMode is absent from tailwind.config.js.prefers-color-scheme media query.dark classes to the HTML structure.The default media strategy applies globally based on the system preference and cannot inherently be scoped to a specific component subtree without additional CSS or JavaScript intervention.
To confirm this behavior accurately for your setup, please verify the exact version of Tailwind CSS v3 installed in your project.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 11 Apr 2022, 07:22 UTC
Tailwind CSS v3 defaults to the class strategy when darkMode is omitted from tailwind.config.js. Dark utilities such as dark:bg-black only apply if a dark class exists on an ancestor element in the DOM. You can limit the effect to a component subtree by adding the dark class to that component’s root element, which influences only its descendants.