Updated Nov 18 2019
Psssst! The 2023 WEB DEVELOPMENT BOOTCAMP is starting on FEBRUARY 01, 2023! SIGNUPS ARE NOW OPEN to this 10-weeks cohort course. Learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much more! ✨
I recently set out to move my blog CSS to Tailwind.
Tailwind is an interesting framework because instead of providing a set of widgets like Bootstrap or others, it provides utilities.
I find it resonates a lot with how I work with HTML.
I introduced how I use Tailwind with Vue in a previous post, but without a build tool in place already, it can be hard to get the correct setup right, and I decided to write this blog post even just for me to remember later on 🙃
In this post I explain how to use Tailwind with any kind of project.
First step is to install Tailwind, using npm or yarn:
npm init -y
npm install tailwindcss
Next, use this command to create a configuration file:
npx tailwind init
This will create a tailwind.config.js
file in the root of your project, adding the basic Tailwind configuration.
Now you need to tweak the PostCSS configuration to make sure Tailwind runs. Add:
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer')
]
}
to your postcss.config.js
. Create one if it does not exist.
I also added autoprefixer for convenience, you’ll likely need it. Install it with npm install autoprefixer
.
Oh, also make sure you installed PostCSS (npm install -g postcss-cli
)
Now create a CSS file where you want, like in tailwind.css
and add
@tailwind base;
@tailwind components;
@tailwind utilities;
Now open your package.json
file, and add a scripts section if you don’t have it:
"scripts": {
"build:css": "postcss src/tailwind.css -o static/dist/tailwind.css"
}
Now from the command line run npm run build:css
will build the final CSS file.
The resulting file is in static/dist/tailwind.css
(you can change the location in the above command).
Every time I change something in the theme HTML (stored in the layouts
folder in my case), I want to regenerate the CSS, and trigger the purge and minification I set up.
How to do this?
Install the watch
npm package:
npm install watch
and add the watch
script to your package.json
file. You already had build:css
from before, we just add a script that watches the layouts folder and runs build:css
upon every change:
"scripts": {
"build:css": "postcss src/tailwind.css -o static/dist/tailwind.css",
"watch": "watch 'npm run build:css' ./layouts"
}
Now run npm run watch
and you should be good to go!
If you check, the resulting file is huge. Even if you don’t use any Tailwind class in your HTML, all of the framework is included by default, because that’s the default configuration in the tailwind.js
file.
They decided to include all, to avoid people missing things. It’s a design choice. We now need to remove stuff, and it turns out we can use purgecss to remove all the unused CSS classes.
I also want to remove all comments from the CSS and make it as small as possible. cssnano is what we’re looking for.
We can automate this stuff! First, install those utilities:
npm install cssnano
npm install @fullhuman/postcss-purgecss
Then we add this to our PostCSS configuration file postcss.config.js
:
const purgecss = require('@fullhuman/postcss-purgecss')
const cssnano = require('cssnano')
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
cssnano({
preset: 'default'
}),
purgecss({
content: ['./layouts/**/*.html', './src/**/*.vue', './src/**/*.jsx'],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
})
]
}
Why? Every step you add slows down the feedback cycle while developing. I use this config to only add prefixes and removing comments in production:
postcss.config.js
const purgecss = require('@fullhuman/postcss-purgecss')
const cssnano = require('cssnano')
module.exports = {
plugins: [
require('tailwindcss'),
process.env.NODE_ENV === 'production' ? require('autoprefixer') : null,
process.env.NODE_ENV === 'production'
? cssnano({ preset: 'default' })
: null,
purgecss({
content: ['./layouts/**/*.html', './src/**/*.vue', './src/**/*.jsx'],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
})
]
}