Tailwind CSS is a CSS framework and design system based on Utility Class, which can quickly add common styles to components, and support flexible extension of theme styles.
To use Tailwind CSS in Modern.js, you only need to configure it according to the Rsbuild steps. Rsbuild supports Tailwind CSS versions v3 and v4:
As Modern.js evolves, to provide a more unified build experience and stronger configuration flexibility, we have adjusted the way Tailwind CSS is supported. Modern.js V3 recommends integrating Tailwind CSS natively through Rsbuild, no longer relying on the @modern-js/plugin-tailwindcss plugin, thereby fully leveraging Rsbuild’s more flexible configuration capabilities and better build experience.
Taking Tailwind CSS V3 as an example, the migration steps are as follows:
@modern-js/plugin-tailwindcss dependency@modern-js/plugin-tailwindcss plugin in the modern.config.ts fileCreate or update the postcss.config.cjs file.
module.exports = {
plugins: {
tailwindcss: {},
},
};Single Configuration Case:
tailwind.config.{ts,js}, no additional action is neededmodern.config.ts, migrate the Tailwind-related configuration to tailwind.config.{ts,js}Dual Configuration Case: If configurations exist in both tailwind.config.{ts,js} and modern.config.ts, merge the configurations and migrate the merged result to tailwind.config.{ts,js}
Special Directory Handling: If your project contains storybook or config/html directories, supplement ./storybook/**/* or ./config/html/**/*.{html,ejs,hbs} in the content field of tailwind.config.{ts,js}
/* Old way */
@import 'tailwindcss/base.css';
@import 'tailwindcss/components.css';
@import 'tailwindcss/utilities.css';
/* New way */
@tailwind base;
@tailwind components;
@tailwind utilities;If the project uses twin.macro, perform the following steps; otherwise, ignore:
pnpm add twin.macro styled-components babel-plugin-macros -Dbabel-plugin-macros Babel plugin:export default defineConfig({
plugins: [appTools()],
tools: {
babel: {
plugins: [
[
'babel-plugin-macros',
{
twin: {
preset: 'styled-components',
config: './tailwind.config.ts',
},
},
],
],
},
},
});If your project still uses Tailwind CSS v2, we recommend upgrading to v3 to support JIT and other features. For differences between Tailwind CSS v2 and v3, please refer to the following articles:
The migration for Tailwind CSS v2 also follows the above steps, but note that the original plugin automatically adapts to the differences between Tailwind v2 (purge configuration) and v3 (content configuration). After removal, you need to use the purge option to specify the CSS classes to retain.
Tailwind CSS provides an official extension called Tailwind CSS IntelliSense for autocompletion of Tailwind CSS class names, CSS functions, and directives in VS Code.
You can follow the steps below to enable the autocomplete feature:
tailwind.config.{ts,js} file, you need to create one and write the Tailwind CSS configuration for your current project. Otherwise, the IDE plugin will not work correctly.Tailwind CSS v2 and v3 do not support the IE 11 browser, please refer to:
If you use Tailwind CSS on IE 11 browser, some styles may not be available, please use it with caution.