In this tutorial, we will learn how to add TailwindCSS to a Hugo project. Tailwind is a utility CSS library that has been gaining a lot of popularity in the past few years. Utility libraries are great because you won’t end up fighting against a framework when it comes to building your own custom designs.
And with Tailwind, you can build your sites faster because you won’t be jumping between your HTML and CSS all the time. In this tutorial, we’re going to learn how to add TailwindCSS to a Hugo project.
Prerequisites
To follow along with this tutorial, you will need to have Hugo and NodeJS installed
Installation guides:
Creating Hugo project and theme
Let’s start by creating a new project. Navigation into the directory where you keep all your projects, and run the command to create a new hugo site. We’re going to name our site “hugotails”.
|
|
Then change directories into our new “hugotails” site and create a new theme. We will call the theme “windytheme”.
|
|
Initialising NPM and installing dependencies
Now that we’ve got our hugo site all ready to go, let’s install tailwind. To follow along with this section of the tutorial, you will need to have Nodejs installed. So if you haven’t installed it already, you might want to do so before continuing.
Make sure you’re in the “hugotails” project root because we will be initialising NPM here. We can run the npm init
command with the --yes
flag so we don’t have to go through all the setup questions.
After this command is successfully run, we will have a newly-created package.json
in our project root.
|
|
Now that our project has been initialised with NPM, we can go ahead and install the necessary packages. Run:
|
|
We are passing in the -save-dev
flag because we want to save these packages as dev dependencies.
If you noticed, we aren’t just installing the tailwind package, we have a few other accompanying packages. Below is a brief description of what each of them are for:
Postcss
- a tool for transforming CSS with JavaScript.postcss-cli
- the command-line tool we can use to execute Postcss commands in the terminalpostcss-import
- used to resolve the path of an @import ruleautoprefixer
- helps us add vendor prefixes to our CSStailwindcss
- a library of utility classes to help us build unique custom layouts without doing too much
Setting up config files
Next up, we will tell our site to use our new windytheme
theme, by adding the following line to our hugotails/config.toml
file:
|
|
Now we can move onto adding tailwind as a PostCSS plugin. Since we’re working with a brand new theme, we don’t have a css
directory yet. Let’s create one. Running mkdir with the -p
flag will create nested directories if they don’t already exist.
|
|
Next we need to create configuration files for both PostCSS and tailwind. We will also need a main styles.scss
file:
|
|
Let’s tackle the postcss config first. Open up the postcss.config.js
file
|
|
Phew, that was a lot. But we’re not done yet, let’s head over to our tailwind.config.js
and add a basic configuration file for TailwindCSS. We also need to specify the files we’d like Tailwind to watch
|
|
Updating styles.scss
Now we have to include the necessary tailwind imports in our stylesheet. Open up our stylesheet, styles.scss
, and add the following imports:
|
|
Importing CSS in head tag
When we created our new theme, Hugo helped us out by creating some starting templates files. One of these starter files is the head.html
partial.
|
|
So what’s going on in our code above? Let’s go over it line-by-line:
- Line 1
{{ $time := now }}
- We’re getting the current time so we can use to bust our cache in local development
- Line 2 - 6
$styles := resources.Get "css/styles.scss"
- We got our stylesheet as a resource and stored it in a variable called styles| toCSS
- We used Hugo pipes to first convert the SCSS file to CSS, so the browser can understand it| postCSS (dict "config" "./assets/css/postcss.config.js")
- We point to where our PostCSS config file lives| resources.ExecuteAsTemplate (printf "styles.%s.css" $time) $time
- We execute our stylesheet as a template so we can append a timestamp to the end of the filename
- Line 8 - 9
- We’re going to check if we’re in our local dev environment by using the IsServer variable, if we are, we will link to the stylesheets relative URL
- Line 10 - 13
- However, if we’re in a production environment, we’re going to use Hugo Pipes to minify our css file
- We also want to pipe it through fingerprint so that we know the asset has not been tampered with. This also has a nice side-effect of cache-busting, so we also know we’re serving the latest version
Testing some TailwindCSS classes
We’ve set up all the configuration we need. Now let’s set up our index
page too. This file can be found at themes/windytheme/layouts/index.html
.
|
|
Now we can imported our styles and transformed them in a few useful ways, let’s see if everything actually works.
Let’s create a basic header in the themes/windytheme/layouts/partials/header.html
file:
|
|
While still in themes/windytheme/layouts/partials/header.html
, let’s add some tailwind utility classes:
|
|
We should also start our Hugo server:
|
|
Open your browser and visit http://localhost:1313, you should be able to see your changes there.
Conclusion
In this tutorial, you learnt how to add tailwindcss to your hugo site using Hugo Pipes. If you enjoyed this article and you’d like more, consider following Div Rhino on YouTube.
Congratulations, you did great. Keep learning and keep coding!