Sveltekit is finally in public beta. Does anyone know how to use it with Tailwind CSS? There aren't any official docs for this integration.
Since Tailwind is a PostCSS plugin, there's nothing stopping you from using it with Sass, Less, Stylus, or other preprocessors, just like you can with other PostCSS plugins like Autoprefixer.
Using modifiers with custom CSS. Any custom styles you add to Tailwind with @layer will automatically support Tailwind's modifier syntax for handling things like hover states, responsive breakpoints, dark mode, and more.
Tailwind forces an extra HTML-based abstraction layer for a CSS job. This is always going to be an anti-pattern. On balance, I think it's slightly worse than just using inline CSS – either via Styled Components or "vanilla CSS". At least inline CSS doesn't force me to memorize a bunch of new class names.
There are great benefits of using Tailwind, like development speed. Tailwind provides almost all the tools you need to build out a site in just HTML, so you rarely need to write any custom CSS. Tailwind was built from the ground-up to be super customizable.
Since you're using SvelteKit, you can also look at using the Svelte Adder for Tailwind.
From their README:
You must start with a fresh copy of the official SvelteKit template, which is currently created by running this command:
npm init svelte@next
Once that is set up, run this command in your project directory to set up Tailwind CSS:
npx svelte-add tailwindcss # --jit
That command will perform the Tailwind setup for you, so you don't have to create all the configs yourself.
Luckily, setting up Tailwind CSS in Sveltekit is easy.
If you don't have a Sveltekit project already, now's the time to create one.
npm init svelte@next
npm install
Assuming you already have Svelte
npm install -D tailwindcss@latest postcss@latest autoprefixer@latest
If you want to use just in type compilation for Tailwind, install that, too.
npm install -D @tailwindcss/jit
npx tailwindcss init -p
Next, change the created tailwind.config.js
to a commonjs module by renaming it to tailwind.config.cjs
. You just need to change the extension to cjs
.
Then, inside the config, setup which pages/components to purge from.
// tailwind.config.cjs
module.exports = {
purge: ['src/app.html', 'src/**/*.svelte'],
...
}
Create a styles.css
file in the src folder.
// ./src/style.css
@tailwind base;
@tailwind components;
@tailwind utilities;
Now, create a layout component to import the styles from.
// ./src/routes/$layout.svelte
<script>
import '../style.css';
</script>
This is the final step.
In your svelte.config.cjs
file, add postcss as a preprocessor.
// svelte.config.cjs
module.exports = {
// add this
preprocess: sveltePreprocess({
postcss: true,
defaults: {
style: 'postcss',
},
}),
}
And create a postcss.config.cjs
file in the root of the project.
// postcss.config.cjs
module.exports = {
plugins: {
'tailwindcss': {},
autoprefixer: {},
},
};
If you're using @tailwindcss/jit
, replace tailwindcss
above with @tailwindcss/jit
.
That's it! You're now ready to use Sveltekit and Tailwind CSS.
P.S. Credit goes to Matt Lehrer for writing a great blog post on the subject.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With