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

Tailwind CSS3 Installation (NPM)

Tailwind CSS 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/。


Install Tailwind CSS 3

This section is about installing Tailwind CSS 3. The installation method for Tailwind CSS 4 has changed; it automatically detects files such as HTML and JavaScript, eliminating the trouble of manually configuring content sources.

For details, see:Tailwind CSS4 Installation。

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

npm install -D tailwindcss

npx tailwindcss init

After running the above command, a basic configuration file tailwind.config.js is generated for customization.

Configure Template Paths

Next, we can add template paths in the tailwind.config.js file:

tailwind.config.js file code:

/** @type {import('tailwindcss').Config} */

module.

exports

=

{

  content

:

[

"./src/**/*.{html,js}"

]

,

 

// Adjust according to the actual file types in the project

  theme

:

{

    extend

:

{

}

,

 

}

,

  plugins

:

[

]

,

}

Add Tailwind Directives to the CSS File

In your main CSS file src/input.css (create it if it doesn't exist), add each Tailwind feature module via @tailwind directives.

src/input.css file code:

@tailwind base;

@tailwind components;

@tailwind utilities;

Start the Tailwind CLI Build Process

Run the CLI tool to scan classes in template files and build CSS.



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

Using Tailwind in HTML

Add the compiled CSS file to the tag, and then you can start using Tailwind's utility classes to style your content.

src/index.html file code:

 

 

 

 

    Hello world!

 

The above steps apply to most modern front-end projects, whether using Vue, React, or other frameworks. Make sure the compiled CSS file is included in the build process so that Tailwind CSS styles can be correctly applied to your project.


Using PostCSS

Installing Tailwind CSS as a PostCSS plugin is the best way to integrate seamlessly with build tools such as webpack, Rollup, Vite, and Parcel.

1. Install Tailwind CSS

Install Tailwind CSS and its related dependencies via npm, and generate the tailwind.config.js configuration file.



npm install -D tailwindcss postcss autoprefixer

npx tailwindcss init

2. Add Tailwind to the PostCSS Configuration

In the project's postcss.config.js file, add the tailwindcss and autoprefixer plugins.

postcss.config.js file code:

module.

exports

=

{

  plugins

:

{

    tailwindcss

:

{

}

,

    autoprefixer

:

{

}

,

 

}

}

3. Configure Template Paths

In the tailwind.config.js file, add the paths of all template files so that Tailwind CSS can generate the required styles based on these files.

tailwind.config.js file code:

/** @type {import('tailwindcss').Config} */

module.

exports

=

{

  content

:

[

"./src/**/*.{html,js}"

]

,

// Replace with the actual template paths in the project

  theme

:

{

    extend

:

{

}

,

 

}

,

  plugins

:

[

]

,

}

4. Add Tailwind Directives to the CSS File

In the main CSS file, add the following three core Tailwind directives to import base styles, component styles, and utility styles.

main.css file code:

@tailwind base;

@tailwind components;

@tailwind utilities;

5. Start the Build Process

Start the project's build process via npm run dev or the related commands configured in the package.json file.



npm run dev

6. Use Tailwind CSS in HTML

Make sure the compiled CSS file is correctly included in the tag (some frameworks may do this automatically), then start using the utility classes provided by Tailwind CSS in HTML to style your content.

index.html file code:

 

 

 

 

    Hello world!