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";. The CSS file does nothing until something loads it. Here you connect it to the app and learn why that step matters.
Why the import is required
Vite builds a module graph starting from your entry point. A CSS file that no module imports is not in that graph. The Tailwind plugin never sees it, and no utility classes are generated.
This is the most common reason for a setup that looks correct but produces unstyled output. The config is fine and the CSS is fine, but nothing connects them.
Two ways to load the CSS
You can load the stylesheet in one of two ways.
- Import it from JavaScript or TypeScript. Vite handles it as part of the module graph. This is the usual choice for apps with a JS entry file.
- Link it from HTML with a
<link rel="stylesheet" href="/src/style.css">tag. Vite also processes this, because it scansindex.htmlfor local assets.
This chapter uses the first approach. In a default Vite project, the entry file is src/main.js (or src/main.ts), and index.html loads it with a module script tag.
Import the stylesheet in the entry file
Add a side-effect import at the top of the entry file. A side-effect import has no from clause. It only runs the module, which here means injecting the CSS.
import './style.css'
document.querySelector('#app').innerHTML = `
<h1 class="text-3xl font-bold underline text-blue-600">Hello Tailwind</h1>
`The file name must match the CSS file you created. The Vite template may name it style.css, index.css, or main.css. If your project already has a file with a different name, import that one and put @import "tailwindcss"; in it. Do not create a second stylesheet by accident.
The <h1> uses four utility classes:
| Class | Effect |
|---|---|
| `text-3xl` | Large font size |
| `font-bold` | Bold weight |
| `underline` | Underlined text |
| `text-blue-600` | Medium-dark blue color |
Check that index.html loads the entry file
Your index.html must contain an element with the id app and a script tag that points at the entry file. The default Vite template already includes both:
<div id="app"></div>
<script type="module" src="/src/main.js"></script>If you use TypeScript, the script path ends in .ts instead. The querySelector('#app') call returns null when the element is missing. The assignment to innerHTML then throws an error in the browser console.
What happens when the page loads
The following diagram shows how the pieces connect during development.
When Vite serves style.css, the plugin expands @import "tailwindcss" and scans your project files for class names. It then emits only the utilities you used.
Common mistakes
- The import path is wrong. A typo such as
./styles.cssmakes Vite show an error overlay that says it failed to resolve the import. - The import sits in a file that never runs. Make sure the file you edited is the one named in the
<script>tag inindex.html. - The CSS file lacks
@import "tailwindcss";. The import succeeds, but no Tailwind styles appear. - The class name is misspelled. Tailwind ignores names it does not recognize, and the element renders unstyled.
Exercise
Your Tailwind config and CSS file are correct, but the page shows no Tailwind styles and the console shows no errors. What should you check first?
Show answer
Check that the entry file (for example src/main.js) contains import './style.css' with the correct file name, and that index.html loads that entry file with a script tag. A CSS file that nothing imports is never processed by the Tailwind plugin.