Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Tailwind CSS4 Installation

Tailwind CSS4 Installation (NPM)

Before you begin, make sure Node.js and npm are installed. You can check whether they are installed using the following command:

node -v

npm -v

If your system does not yet support Node.js and NPM, you can refer to our Node.js Tutorial。

In China, npm can be slow; you can use Taobao's customized cnpm (with gzip compression support) command-line tool to replace the default npm:



$ npm install -g cnpm --registry=https://registry.npmmirror.com

$ npm config set registry https://registry.npmmirror.com

You can then use the cnpm command to install modules:



$ cnpm install [name]

For more information, see:http://npm.taobao.org/。


Installing Tailwind CSS

Compared with Tailwind CSS 3, Tailwind CSS 4 automatically detects files such as HTML and JavaScript, eliminating the trouble of manually configuring content sources.

npm install

The simplest and fastest way to start using Tailwind CSS from scratch is through the Tailwind CLI tool.

Install via npm Install and configure Tailwind CSS in a local project:

npm install tailwindcss @tailwindcss/cli

1. Import Tailwind in CSS: Add @import "tailwindcss"; to the CSS file.

For example, import it at the beginning of the src/input.css file:

src/input.css file

@import "tailwindcss";

2. Start the Tailwind CLI build process: Run the CLI tool to scan for classes in source files and build the CSS.



npx @tailwindcss/cli -i ./src/input.css -o ./src/output.css --watch

3. Start using Tailwind in HTML: Add the compiled CSS file to and start using Tailwind's utility classes to style the content.

src/index.html file

 

 

 

 

    Hello world!

 

Installing as a Vite plugin

Vite is a modern frontend build tool designed to provide a fast development experience by leveraging native ES module support in modern browsers.

For details about Vite, see:Vite Tutorial。

Installing Tailwind CSS as a Vite plugin is the most seamless way to integrate it with frameworks such as Laravel, SvelteKit, React Router, Nuxt, and SolidJS.

1. Install Tailwind CSS

Install tailwindcss and @tailwindcss/vite via npm:



npm install tailwindcss @tailwindcss/vite

2. Configure the Vite plugin

Add the @tailwindcss/vite plugin to the Vite configuration file vite.config.ts:

vite.config.ts file

import

{

defineConfig

}

from

'vite'

import

tailwindcss from

'@tailwindcss/vite'

export

default

defineConfig

(

{

  plugins

:

[

    tailwindcss

(

)

,

 

]

,

}

)

3. Import Tailwind CSS

Add @import to the CSS file to import Tailwind CSS:



@import "tailwindcss";

4. Start the build process

Run the build process using npm run dev or another command you configured in the package.json file:



npm run dev

** 5. Start using Tailwind in HTML**

Make sure the compiled CSS is included in (the framework may handle this automatically), then start using Tailwind's utility classes to style the content.

Example

 

 

 

 

    Hello world!

 

Installing as a PostCSS plugin

Installing Tailwind CSS as a PostCSS plugin allows seamless integration with frameworks such as Next.js and Angular.

1. Install Tailwind CSS

Install tailwindcss, @tailwindcss/postcss, and postcss via npm.



npm install tailwindcss @tailwindcss/postcss postcss

2. Add Tailwind to the PostCSS configuration

Add @tailwindcss/postcss in the project's postcss.config.mjs file (or wherever PostCSS is configured).

postcss.config.mjs file

export

default

{

  plugins

:

{

   

"@tailwindcss/postcss"

:

{

}

,

 

}

}

3. Import Tailwind CSS

Add @import to the CSS file to import Tailwind CSS.



@import "tailwindcss";

4. Start the build process

Run the build process using npm run dev or another command you configured in the package.json file.



npm run dev

5. Start using Tailwind in HTML

Make sure the compiled CSS is included in (the framework may handle this automatically), then start using Tailwind's utility classes to style the content.

Example

 

 

 

 

    Hello world!