Svelte + Tailwind v4 setup

By Esteban Trillonew today8 min readWhy SvelteSvelte

The background cabinet takes one key. Piwi holds the utilities one, and the `<style>` key stays on its hook for the things nobody passes in.
The background cabinet takes one key. Piwi holds the utilities one, and the `<style>` key stays on its hook for the things nobody passes in.

Part 21 of a series on Svelte in 2026. Part 20 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.1 The Svelte CLI does the same thing for you:

Terminal window
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:

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), 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:

src/routes/layout.css
@import "tailwindcss";
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:

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:

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.2 Scoped rules make it more lopsided: part 11 showed that Svelte adds a scoping class to every selector, a 0-1-0 bump the docs describe,3 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.

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?

@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:

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

vite build stopped with:

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.4 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:

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.

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 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.5 That has two consequences, and I reproduced both.

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

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

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

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

<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.5 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:

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.

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 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.

Footnotes

  1. Tailwind CSS, Install Tailwind CSS with SvelteKit. ↩

  2. MDN, Cascade layers: styles not in a layer take precedence over layered styles for normal declarations. ↩

  3. Svelte docs, Scoped styles. ↩

  4. Tailwind CSS, Functions and directives, @reference and @source. ↩

  5. Tailwind CSS, Detecting classes in source files. ↩ ↩2

Read next

  1. Svelte and AI coding toolsDec 3, 2026 Why Svelte · 8 min
  2. A component library in SvelteDec 3, 2026 Why Svelte · 7 min
  3. Svelte vs React: one component, two waysDec 2, 2026 Why Svelte · 7 min
  4. Observability in SvelteKitDec 2, 2026 Why Svelte · 6 min
  5. Testing Svelte componentsDec 2, 2026 Why Svelte · 7 min
Piwi hands a single letter to the user at a mailbox; a small note reads no noise.

Get the next one in your inbox

One email when a post goes out, with its sketch. Nothing in between, and one link to leave.

Sign in faster next time?

Add a passkey and skip the email link. Your fingerprint, face or device PIN is enough.