DEV Community

Elian Van Cutsem
Elian Van Cutsem

Posted on • Originally published at elian.codes on

Use TailwindCSS with Sveltekit (2021)

Since my previous blogpost on using TailwindCSS (JIT) with Sveltekit, a lot has changed. That's why I've updated the article to a newer (and better) method.

Setting up Sveltekit

Setting up a new Sveltekit project is not that hard anymore. Just run the following set of commands:

npm init svelte@next tailwind-sveltekit-app
Enter fullscreen mode Exit fullscreen mode

You can always search the official documentation for more info

Adding an adapter

In most cases, I tend to use Sveltekit as a static site generator. To tell Sveltekit to create static pages, add the static-adapter with following command:

npm i @sveltejs/adapter-static@next
Enter fullscreen mode Exit fullscreen mode

Also add the following to the svelte.config.cjs

const adapter = require('@sveltejs/adapter-static');

module.exports = {
    kit: {
        adapter: adapter({
            pages: 'build',
            assets: 'build',
            fallback: null
        })
    }
};
Enter fullscreen mode Exit fullscreen mode

This tells Sveltekit to put the output in the build folder and dont use a fallback (so generate a index.html, 404.html, contact.html, ...)

more info on the adapter here

Adding TailwindCSS

Previously I described some additional steps to install Tailwind, like installing autoprefixer and PostCSS. I also setup some extra NPM scripts.

Since TailwindCSS is used a lot across the web these days, it got way easier. Just use the following command:

npx svelte-add@latest tailwindcss
Enter fullscreen mode Exit fullscreen mode

This will install TailwindCSS, PostCSS and other dependencies. It will also add the configuration and a basic app.css file with Tailwind imported.

Top comments (0)