Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Sveltekit and Tailwind CSS

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.

like image 309
Nick Avatar asked Mar 25 '21 14:03

Nick


People also ask

Can Tailwind be used with SCSS?

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.

Can I use custom CSS with Tailwind?

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.

Why you should not use Tailwind CSS?

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.

Is CSS better than Tailwind?

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.


2 Answers

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.

like image 131
Geoff Rich Avatar answered Oct 23 '22 01:10

Geoff Rich


Luckily, setting up Tailwind CSS in Sveltekit is easy.

1. Install Sveltekit

If you don't have a Sveltekit project already, now's the time to create one.

npm init svelte@next
npm install

2. Install Tailwind CSS

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

3. Run Tailwind setup

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'],
...
}

4. Create styles.css

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>

5. Connect Sveltekit with Tailwind

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.

like image 33
Nick Avatar answered Oct 23 '22 00:10

Nick