4 min read
Tailwind CSS is a great CSS framework. Utility first, fully customizable and just a pleasure to use.
I had been seeing it around and wanted to try it myself, however the set up instructions can feel a bit tedious, especially if you’re not familiar with all the front-end build tools and CSS preprocessors.
So, after going through it myself, I decided to write a guide on the fastest way to set up Tailwind CSS.
src/css/app.css
build/css/app.css
Laravel Mix is a great tool that greatly simplifies webpack configuration. I have written a previous article on it here.
While we’re going to use it only to compile CSS here, it can do so much more. Check out my previous article and the official documentation.
npm init -y
npm install laravel-mix --save-dev
cp node_modules/laravel-mix/setup/webpack.mix.js ./
We also need to add some scripts to our package.json
"scripts": {
"dev": "NODE_ENV=development webpack --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "NODE_ENV=development webpack --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"hot": "NODE_ENV=development webpack-dev-server --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"production": "NODE_ENV=production webpack --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
}
Now we install Tailwind
npm install tailwindcss
npx tailwind init
Next, we’ll edit our src/css/app.css
file to add tailwind:
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
We will now edit webpack.mix.js
to the following:
let mix = require('laravel-mix');
mix.postCss('src/css/app.css', 'build/css', [
require('postcss-import'), // So we can use @import statements
require('tailwindcss'),
require('autoprefixer'), // to add prefixes like --webkit
]);
We’re using two other PostCSS plugins: postcss-import
and autoprefixer
. And our Tailwind CSS
To compile our CSS, we have several options:
npm run dev
npm run watch
npm run production
Building in development mode skips a couple things like minification. Makes the builds faster, and makes it easier to debug.
While developing, I just run npm run watch
and code away!
The main Tailwind CSS compiled file is relatively big.
Before using in production, we are advised to remove unused CSS.
We can do this by adding a custom configuration file. We place this at ./tailwind.config.js
. It should look like this:
module.exports = {
theme: {},
// Specify the paths to all of the template files in your project
purge: [
'../templates/*.html',
'./templates/*.vue',
'./templates/*.jsx',
],
variants: {}
}
When we build our CSS for production by running npm run production
, all unused CSS classes will be purged giving us a much smaller final file size.
We can do this with a PostCSS plugin called Purgecss. There is a more detailed article about this in the Tailwind CSS documentation.
To add Purgecss to our Laravel Mix configuration is pretty straightforward:
First, we install Purgecss
npm install @fullhuman/postcss-purgecss --save-dev
Next we add modify our webpack.mix.js
to look like this:
const purgecss = require('@fullhuman/postcss-purgecss')({
// Specify the paths to all of the template files in your project
content: [
'./templates/*.html',
'./templates/*.vue',
'./templates/*.jsx',
],
// Include any special characters you're using in this regular expression
defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || []
})
let mix = require('laravel-mix');
mix.postCss('src/css/app.css', 'build/css', [
require('postcss-import'), // So we can use @import statements
require('tailwindcss'),
require('autoprefixer'), // to add prefixes like --webkit
...process.env.NODE_ENV === 'production'? [purgecss] : [] // Purge only in production
]);
Tailwind CSS can seem pretty tedious to set up, but using Laravel Mix, it can become a pretty straightforward process. Following the above steps should get you up and running in minutes.
Let me know any questions, or extra tips you may have in the comments section.
Laravel Mix provides a fluent API for defining Webpack build steps for your application using several common CSS and JavaScript pre-processors. The ...
1 min read
Most techniques to style broken images rely on browser hacks and are very inconsistent. Let me show you a consistent way that works the same way across every browser.
2 min read
Comments