Sub topic 6
Load the stylesheet from your app
This chapter assumes you have a Vite project with tailwindcss and @tailwindcss/vite installed, the plugin registered in vite.config.ts, and a CSS file containing @import "tailwindcss";. It also assumes you know basic JavaScript modules and how an HTML page loads a script. The goal here is to connect that CSS file to your running app, so the browser receives the generated styles.
Why the CSS file does nothing by itself
Creating src/style.css and adding @import "tailwindcss"; does not apply any styles. Vite starts from an entry point, follows the imports it finds, and builds a module graph. A CSS file that nothing imports is not in that graph, so Vite never processes it and the Tailwind plugin never sees it.
The plugin works on files that enter the graph. When Vite processes your CSS, Tailwind reads the @import "tailwindcss" line, expands it into its base styles, theme variables, and utilities, and then generates only the utility classes it finds in your source files. Loading the stylesheet is the step that starts this process.
The two common ways to load CSS
There are two usual approaches. The JavaScript import is the one used in the Vite templates and in this tutorial.
Import the CSS from JavaScript
In your entry module, add a side-effect import. It has no binding, because you want the file's effect, not a value from it.
import './style.css'
document.querySelector('#app').innerHTML = `
<h1 class="text-3xl font-bold underline text-blue-600">Hello Tailwind</h1>
`During development, Vite injects the CSS into the page through a <style> element and updates it with hot module replacement when you edit the file. In a production build, Vite extracts the CSS into a separate file and links it from the built HTML.
For this to work, your index.html must load the entry module. A Vite project created from the vanilla template already does this:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>The type="module" attribute matters. Vite relies on native ES modules in development, and an import statement is only valid inside a module script.
Link the CSS from HTML
You can also reference the stylesheet directly in index.html. Vite processes <link> tags that point to local files, so the Tailwind plugin still runs.
<link rel="stylesheet" href="/src/style.css" />This approach works well for projects with little JavaScript. It has one drawback in frameworks: component files typically import their own CSS, so keeping a single import in the entry file is easier to follow. Pick one method per stylesheet. Loading the same file both ways can include its styles twice.
File names and paths
The file name is your choice. The Vite vanilla template creates src/style.css, while React templates usually create src/index.css. What matters is that the name in the import matches the file on disk.
| Template | Typical entry file | Typical CSS file |
|---|---|---|
| Vanilla JavaScript | `src/main.js` | `src/style.css` |
| React | `src/main.jsx` | `src/index.css` |
| Vue | `src/main.js` | `src/style.css` |
Template contents change between Vite versions, so check your own project instead of relying on this table. If the template already imports a CSS file, replace the contents of that file with the Tailwind import, or remove the template styles you do not want. Template CSS often sets colors, fonts, and layout on body or #app, and these rules can conflict with your utilities.
A relative import such as ./style.css is resolved from the file that contains the import. If your entry is src/main.js and the stylesheet is in src/styles/app.css, write:
import './styles/app.css'Verify the connection
Run the dev server:
npm run devOpen the printed local URL, usually http://localhost:5173. The heading from the example should look large, bold, underlined, and blue. That result tells you three things at once: the plugin is registered, the CSS import is expanded, and the class names in your JavaScript were detected.
To check what the browser received, open the developer tools and inspect the <h1>. In the Styles pane you should see rules such as font-size: var(--text-3xl) and font-weight: var(--font-weight-bold). In the Elements pane, a <style> element with data-vite-dev-id pointing to your CSS file confirms Vite injected it.
Common errors when loading the stylesheet
| Symptom | Likely cause | Fix |
|---|---|---|
| The heading is plain black text in a serif font | The CSS file is not imported anywhere | Add `import './style.css'` to the entry module |
| The console shows `Failed to resolve import "./style.css"` | The path or file name does not match the file on disk | Check the folder and the spelling, including case on Linux and macOS |
| Browser default styles appear, but no utilities apply | The file is loaded, but `@import "tailwindcss";` is missing or misspelled | Open the CSS file and confirm the import line |
| The CSS loads, but classes in some files are ignored | Those files are outside what Tailwind scans | See the chapter on source detection and `@source` |
| Styles from the Vite template override your utilities | Leftover rules in the template CSS | Remove the template rules |
| The page is blank and the console reports a script error | `index.html` does not load the entry module, or the script is missing `type="module"` | Fix the `<script>` tag |
If you changed vite.config.ts and nothing happens, restart the dev server. Vite reads that file once at startup, so plugin changes need a restart. Edits to the CSS and to your source files are picked up without one.
Class names must appear as complete strings
Tailwind scans your files as plain text. It does not run your code. A class is generated only if its full name appears somewhere in a scanned file. This affects how you write class names in JavaScript:
// Works: the full class name is present in the source
const color = isError ? 'text-red-600' : 'text-green-600'
// Fails: Tailwind never sees "text-red-600" as a complete string
const broken = `text-${isError ? 'red' : 'green'}-600`The second example loads the stylesheet correctly, yet the colors will not appear. Because the class name is assembled at runtime, it never exists as a whole string for the scanner to find. Write the full names, or map a state to a complete class name in an object.
const colorClasses = {
error: 'text-red-600',
success: 'text-green-600',
}
const label = document.createElement('p')
label.className = colorClasses[isError ? 'error' : 'success']Loading more than one stylesheet
You can import other CSS files alongside the Tailwind file, for example a file for a third-party widget. Each import is processed by Vite. Only the files that contain @import "tailwindcss" receive Tailwind's expansion. If you want a second file to use Tailwind features such as @apply or theme variables, import it from the main Tailwind file or give it its own import. For most projects, one Tailwind entry stylesheet is enough, and it avoids generating the base styles more than once.
Exercise
You added @import "tailwindcss"; to src/style.css and wrote <h1 class="text-3xl font-bold"> in your page, but the heading renders in default browser styles. The dev server shows no errors. Name the most likely cause and the one-line fix, and explain why the plugin never ran on the file.
Show answer
The CSS file is not part of Vite's module graph because nothing imports it. Add import './style.css' to the entry module (for example src/main.js), or link it from index.html. Vite only processes CSS that is reachable from the entry point, so the Tailwind plugin never received the file and no utilities were generated. No error appears because an unreferenced file is simply ignored.
Exercise
A component builds a class with the template string bg-${tone}-500, where tone is 'blue' or 'red'. The stylesheet is loaded correctly, but no background color appears. Why, and how would you change the code?
Show answer
Tailwind scans source files as text and does not execute them. The complete names bg-blue-500 and bg-red-500 never appear in the source, so no CSS is generated for them. Write the full names, for example const tones = { blue: 'bg-blue-500', red: 'bg-red-500' } and use tones[tone].