Using TailwindCSS with SvelteJS

June 01, 2019

If you haven't check TailwindCSS, you might want to. I don't want to repeat the awesomeness here 🤪

TL;DR. Full implementation can be found on my Github muhajirdev/svelte-tailwind-template

Get Svelte default template

npx degit sveltejs/template my-svelte-project
cd my-svelte-project

Install dependencies

npm install --save-dev tailwindcss postcss-import @fullhuman/postcss-purgecss postcss rollup-plugin-postcss autoprefixer

Setup rollup-plugin-postcss

// rollup.config.js

import postcss from 'rollup-plugin-postcss'

export default {
    plugins: [
         postcss({extract: true}),

Here's the end result


import svelte from 'rollup-plugin-svelte';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss'

const production = !process.env.ROLLUP_WATCH;

export default {
	input: 'src/main.js',
	output: {
		sourcemap: true,
		format: 'iife',
		name: 'app',
		file: 'public/bundle.js'
	plugins: [
			extract: true
			// enable run-time checks when not in production
			dev: !production,
			// we'll extract any component CSS out into
			// a separate file — better for performance
			css: css => {

		// If you have external dependencies installed from
		// npm, you'll most likely need these plugins. In
		// some cases you'll need additional configuration —
		// consult the documentation for details:
		resolve({ browser: true }),

		// Watch the `public` directory and refresh the
		// browser on changes when not in production
		!production && livereload('public'),

		// If we're building for production (npm run build
		// instead of npm run dev), minify
		production && terser()
	watch: {
		clearScreen: false

Create postcss.config.js

const production = !process.env.ROLLUP_WATCH;
const purgecss = require("@fullhuman/postcss-purgecss");

module.exports = {
  plugins: [
    // Only purge css on production
    production &&
        content: ["./**/*.html", "./**/*.svelte"],
        defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || []

Create src/main.css

@import "tailwindcss/base";

@import "tailwindcss/components";

@import "tailwindcss/utilities";

Import src/main.css in src/main.js

// src/main.js
import App from "./App.svelte";
import "./main.css";

const app = new App({
  target: document.body,
  props: {
    name: "world"

export default app;


Remove public/global.css And also remove

 <link rel="stylesheet" href="global.css" />

from index.html as we already have normalize.css from tailwind

And finally, now you can just write tailwind class

Try it <div class="bg-black">test</div> should give you a div with black background.

Any feedback would be very appreciated :)


twitter icon


Send me a DM
twitter icon

Shoot me an mail