Sub topic 2
Import Tailwind in Your CSS
Assume you have a Vite project, Node.js and npm installed, and basic knowledge of CSS imports and JavaScript modules. Tailwind CSS v4 uses a CSS-first setup. In a typical Vite project, you do not need a tailwind.config.js file or the old v3 @tailwind directives.
Import Tailwind in the CSS entry file
Create or open the stylesheet used by your app, such as src/style.css. Add one import:
@import "tailwindcss";This import makes Tailwind’s generated styles available to the build. The @import statement must appear in a stylesheet that Vite processes.
Load the stylesheet from your app
Import the stylesheet from your JavaScript entry file. For a simple Vite project, this file is often src/main.js:
import './style.css'
document.querySelector('#app').innerHTML = `
<h1 class="text-3xl font-bold underline text-blue-600">
Hello Tailwind
</h1>
`The browser does not read style.css just because the file exists. The JavaScript import tells Vite to include it in the application bundle. The utility classes on the h1 element should produce a large, bold, underlined, blue heading.
Run and verify the development server
Start Vite from the project directory:
npm run devOpen the local URL printed in the terminal. Confirm that the heading is large, bold, underlined, and blue.
Tailwind generates CSS for class names it finds in your project files. Keep class names as complete strings when testing. Dynamically assembling fragments such as "text-" + color can prevent Tailwind from detecting the final class.
Exercise
Why must main.js import style.css?
Show answer
Vite only processes and includes the stylesheet when it is connected to the application entry point.