# Svelte + Tailwind v4 setup

- Published: 2026-12-03
- Tags: Why Svelte, Svelte
- URL: https://devtrillo.com/writing/why-svelte-21-svelte-tailwind-v4-setup/

> Part 21 of a series on Svelte in 2026. [Part 20](/writing/why-svelte-20-a-component-library-in-svelte/) shipped a Svelte component to another app. This part sets up the styling that app uses.

Installing Tailwind in a SvelteKit app takes one command now, and I'm not going to pretend it's harder than that. What took me longer was the second hour, when a component's `<style>` block quietly overrode a utility it had no business beating, and a Badge from an installed package rendered with no background at all.

So this is the setup I use for new projects: the command, what it changed, and the rules I keep afterwards so Tailwind and Svelte's own scoped CSS stay out of each other's way. I checked every claim in a fresh app: `sv` 1.1.1 (minimal template), Svelte 5.57.3, SvelteKit 3.1.0, Vite 8.3.5, `tailwindcss` and `@tailwindcss/vite` 4.3.3, `svelte-check` 4.7.7, Node 24, with computed styles read in Playwright's Chromium.

## One command, four files

Tailwind's SvelteKit guide has you install two packages, add a Vite plugin, create a CSS file with one line in it, and import it from the root layout.[^install] The Svelte CLI does the same thing for you:

```sh
npx sv add tailwindcss="plugins:none"
```

Without the option it stops to ask about the typography and forms plugins. With it, it installed `tailwindcss` and `@tailwindcss/vite` and touched four files. The Vite config gets the plugin, ahead of SvelteKit's:

```ts title="vite.config.ts"
import tailwindcss from "@tailwindcss/vite";
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    tailwindcss(),
    sveltekit({
      // your adapter and compilerOptions stay as they were
    }),
  ],
});
```

If you're adding it by hand to an existing app, merge those two lines into the file you have. Since SvelteKit 3 moved the adapter into the `sveltekit()` call ([part 16](/writing/why-svelte-16-migrating-to-sveltekit-3-with-sv-migrate/)), pasting the guide's snippet over your config deletes your adapter.

The stylesheet is where the CLI and the guide disagree. The guide says `src/app.css`; the CLI wrote `src/routes/layout.css`, next to the layout that imports it:

```css title="src/routes/layout.css"
@import "tailwindcss";
```

```svelte title="src/routes/+layout.svelte"
<script lang="ts">
  import "./layout.css";
  let { children } = $props();
</script>

{@render children()}
```

Either location works. It matters later, because two of the fixes below are paths relative to that file. The fourth change was editor config: a recommendation for the Tailwind VS Code extension and a `files.associations` entry that tells VS Code to open every `.css` file in the Tailwind language mode.

There's no `tailwind.config.js` and no `@tailwind base` line. Tailwind v4 is configured in CSS, so a brand colour goes in the same file:

```css title="src/routes/layout.css"
@import "tailwindcss";

@theme {
  --color-brand: #b42318;
}
```

That gives you `bg-brand`, `text-brand` and the rest, and nothing else to keep in sync. If a tutorial tells you to create a config file, it's a v3 tutorial.

## A component's `<style>` wins, even when it shouldn't

Here's the one that surprised me. A page with a utility class, and a component style written as loosely as CSS allows:

```svelte title="src/routes/layer/+page.svelte"
<p class="text-blue-600">Which colour am I?</p>

<style>
  :global(p) {
    color: red;
  }
</style>
```

`:global(p)` has the specificity of one element, `0-0-1`. `.text-blue-600` is one class, `0-1-0`. By the rules most of us learned, the class wins. In the production build Chromium computed `rgb(255, 0, 0)`. Red.

The reason is in the built CSS. Tailwind v4 puts everything it generates inside cascade layers (`@layer theme`, `@layer base`, `@layer utilities`), and Svelte's component CSS isn't in any layer. The output had `p{color:red}` sitting outside them. Unlayered styles beat layered ones before specificity is even compared.[^layers] Scoped rules make it more lopsided: [part 11](/writing/why-svelte-11-scoped-styles-css-that-stays-put/) showed that Svelte adds a scoping class to every selector, a `0-1-0` bump the docs describe,[^scope] so a scoped rule would win on specificity alone too.

In practice, any property you set in a component's `<style>` block can't be changed by a utility on the same element. I gave two buttons `bg-blue-600` and one of them also a `local` class styled red in the component. The plain one came out blue, the other red. Changing the second to `bg-blue-600!`, v4's important modifier, turned it blue again, because an `!important` declaration outranks a normal one whatever layer it's in. That works, and it's also the start of an arms race I'd rather not have in a codebase.

So my rule is about ownership, not taste. A property lives in the markup or in the style block, never both. If a component wants callers to change its background with a class, the background has to be a utility; if it's in `<style>`, the class reaches the element and loses.

**Quiz:** A component has <style> h2 { margin: 0; } and a parent renders it with class="mt-8" passed through to the h2. What margin does the h2 get?

- mt-8, because a class beats an element selector
- margin: 0, because unlayered component CSS beats Tailwind's layered utilities
- Whichever stylesheet loads last

**Answer:** margin: 0, because unlayered component CSS beats Tailwind's layered utilities — Unlayered styles win over @layer utilities regardless of specificity. Move the margin into the markup if callers should be able to change it.

- Not "mt-8, because a class beats an element selector": That's the specificity rule, but Tailwind's utilities are in a cascade layer and Svelte's CSS isn't. Layer order is decided first.
- Not "Whichever stylesheet loads last": Source order only breaks ties after layers and specificity, and this isn't a tie.

## `@apply` needs to know where your theme is

The second surprise was a build failure. I wrote a component style using `@apply`, the way plenty of v3-era code does:

```svelte
<h1>apply</h1>

<style>
  h1 {
    @apply text-2xl font-bold text-red-500;
  }
</style>
```

`vite build` stopped with:

```text
Error: Cannot apply unknown utility class `text-2xl`. Are you using CSS
modules or similar and missing `@reference`?
```

Tailwind compiles each Svelte `<style>` block on its own, separate from `layout.css`, so the block has no idea what `text-2xl` means. The docs' fix is `@reference "tailwindcss";` at the top of the block.[^directives] That got me one step further and then failed on `text-brand`, the colour from my `@theme`. The bare `tailwindcss` import only knows the default theme. Pointing at the app's own stylesheet fixed it:

```svelte title="src/routes/apply/+page.svelte"
<style lang="postcss">
  @reference "../layout.css";

  h1 {
    @apply text-2xl font-bold text-brand;
  }
</style>
```

`@reference` doesn't copy the stylesheet in. The page's CSS came out at 0.56 kB, holding the `h1` rule with `var(--color-brand, #b42318)` and a few `--tw-*` property declarations, while the 5.3 kB of utilities stayed in the layout's file. The path is relative to the component, so a file in `src/lib/components/` needs `../../routes/layout.css`, and it's the kind of thing that breaks when files move.

> **Note:** The `lang="postcss"` isn't for the build; Tailwind processed the block without
> it. It's for `svelte-check`, which reported `Unknown at rule @reference` and
> `Unknown at rule @apply` as warnings until I added it.

Having got it working, I don't use it. A rule made of `@apply` is a class list wearing a disguise, and the layer problem above applies to it too: it compiles into unlayered component CSS, so it beats every utility on that element. If the same eight classes repeat across files, that's a component asking to exist, not a stylesheet. The [Tailwind DRY series](/writing/tailwind-dry-1-stop-repeating-yourself/) goes through the options.

## Tailwind reads your files as text

Tailwind doesn't run your components. It scans source files for strings that look like class names and generates CSS for those.[^detect] That has two consequences, and I reproduced both.

The first is the familiar one. With `let color = $state("emerald")`, this paragraph rendered transparent:

```svelte
<p class={`bg-${color}-500`}>interpolated</p>
```

No file contains the string `bg-emerald-500`, so the CSS has no such rule.

> **Note:** I ran both missing-class cases against `vite dev` as well as the production
> build, half expecting the dev server to be more forgiving. It wasn't: the same
> elements came out transparent in both.

A map of whole class names works, because both names are sitting in the source:

```svelte
<script lang="ts">
  let { ok } = $props();
  const tones = { ok: "bg-lime-500", bad: "bg-rose-500" };
</script>

<p class={ok ? tones.ok : tones.bad}>mapped</p>
```

The second follows on from part 20. Tailwind skips `node_modules` when it scans.[^detect] I packed a tiny library whose `dist/Badge.svelte` used `bg-violet-600 text-white`, installed the tarball and rendered `<Badge>`. The badge had no background; `bg-violet-600` wasn't in any CSS file. One line in the app's stylesheet fixed it:

```css title="src/routes/layout.css"
@import "tailwindcss";
@source "../../node_modules/ui";
```

The path is relative to the stylesheet, and the CLI's `src/routes/` location is why it's two levels up rather than the one the docs show for `src/app.css`.

That makes it a contract between the library and every app that uses it. A Svelte library built with Tailwind classes is telling its consumers "you must run Tailwind v4, and you must add an `@source` line." For an internal library that every app already shares, that's fine and I'd say so in the README. For anything published, I'd keep doing what part 20 did, with plain scoped CSS and custom properties for theming, so it works whatever the app styles itself with.

**Quiz:** An installed package's component renders, but none of its Tailwind classes have any effect. The app's own utilities work. What's the likely cause?

- The package was compiled with a different Svelte version
- Tailwind doesn't scan node_modules, so it never saw those class names
- Scoped styles block global classes from reaching the component

**Answer:** Tailwind doesn't scan node_modules, so it never saw those class names — Add @source pointing at the package, relative to your stylesheet, and Tailwind generates its classes too.

- Not "The package was compiled with a different Svelte version": Svelte libraries ship source and the app compiles them, so the markup and classes arrive intact. It's the CSS for those classes that's missing.
- Not "Scoped styles block global classes from reaching the component": Scoping limits which elements a component's own <style> rules match. A global utility class still applies to any element that has it.

## The setup I start with

`npx sv add tailwindcss`, then a `@theme` block in the stylesheet it creates, and that's the whole install. After that I hold to a split. Utilities go in the markup for anything a caller might want to change. The `<style>` block is for what utilities say badly: a `:has()` selector, a long grid template, a keyframe animation. Those are properties nobody passes in. `@apply` stays out, and dynamic class names are always whole strings.

Coming from React, the closest thing is Tailwind next to CSS Modules, and the layer issue is the same there: CSS Modules output is unlayered too. What Svelte adds is that the `<style>` block lives in the component file, so the overlap is easy to make and easy to see. Keep each property in one place and you'll never meet it.

Next: Svelte and AI coding tools.

[^install]: Tailwind CSS, [Install Tailwind CSS with SvelteKit](https://tailwindcss.com/docs/installation/framework-guides/sveltekit).

[^layers]: MDN, [Cascade layers](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Cascade_layers): styles not in a layer take precedence over layered styles for normal declarations.

[^scope]: Svelte docs, [Scoped styles](https://svelte.dev/docs/svelte/scoped-styles).

[^directives]: Tailwind CSS, [Functions and directives](https://tailwindcss.com/docs/functions-and-directives), `@reference` and `@source`.

[^detect]: Tailwind CSS, [Detecting classes in source files](https://tailwindcss.com/docs/detecting-classes-in-source-files).

- **Part 22 — Svelte and AI coding tools**: Getting coding agents to write current Svelte 5
