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.
Modern.js Module supports developing component styles using Tailwind CSS.
To use Tailwind CSS in Modern.js Module, you can follow the steps below:
pnpm run new
in the root directory of your project and make the following selections:After successful initialization, you will see that the package.json
has added dependencies for tailwindcss
and @modern-js/plugin-tailwindcss
.
modern.config.ts
:index.css
file and add the following code:Depending on your needs, you can selectively import the CSS styles provided by Tailwind CSS. Please refer to the @tailwind
documentation for detailed usage of the @tailwind
directive.
index.css
file, for example, add the following code in the root component src/index.jsx
:In Modern.js Module, you have two ways to configure Tailwind CSS:
tailwind.config.{ts,js}
file, which follows the official usage of Tailwind CSS. Please refer to "Tailwind CSS - Configuration" for more details.Please upgrade Modern.js to version >= 2.33.0 to support automatic reading of tailwind.config.{ts,js}
files.
tailwind.config.{ts,js}
file for configuration.If you are using both the tailwind.config.{ts,js}
file and style.tailwindcss
option, the configuration defined in style.tailwindcss
will take precedence and override the content defined in tailwind.config.{ts,js}
.
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.When using Tailwind CSS, please note that there are significant differences between the bundle and bundleless modes in terms of the build artifacts.
For definitions of bundle and bundleless, please refer to the "In-depth understanding of build".
In Bundle mode, a separate CSS file is generated, and the JS output does not automatically reference the CSS output file.
If you need to inject styles into the JS artifact, you can enable the style.inject option.
If you haven't enabled style.inject
, you can also let users manually import the CSS file:
In bundleless mode, the CSS file is automatically imported in the artifact code without the need for additional processing.
You can add a class name prefix using the prefix option provided by Tailwind CSS. This helps avoid potential class name conflicts, such as when different versions of Tailwind CSS are used in different parts of an application or module.
For example, you can add the foo-
prefix using the prefix
option in tailwind.config.js
:
Here are some usage examples of Tailwind CSS.
Tailwind CSS supports adding styles to HTML tags through class names. When using HTML class names, please note that the corresponding CSS styles of Tailwind CSS must be imported in advance.
Generated styles (after bundling):
@apply
Tailwind CSS provides the @apply
directive, which allows us to inline the styles provided by Tailwind CSS into our own styles.
@apply
can be used in CSS, Less, and Sass.
However, there are some considerations when using it with Less and Sass:
When using Tailwind with Sass and there is an !important
after @apply
, interpolation should be used to ensure Sass compiles correctly.
When using Tailwind with Less, you cannot nest Tailwind's @screen
directive.
theme()
function to reference your screen sizes or simply avoid nesting your @screen
directive.designSystem
configdesignSystem
is a deprecated configuration option in Modern.js Module.
Starting from Modern.js Module version 2.33.0, you can use the theme
configuration option of Tailwind CSS as a replacement for designSystem
. It is no longer necessary to split the theme
configuration and set it on designSystem
.