I recently upgraded my site to use TailwindCSS v4. In this post, Iโll guide you through the upgrade process, just as I did for my own website.
Upgrading to v4
There are a few breaking changes, as stated in their documentation. Some of these will change the way we structure the Hugo project.
- TailwindCSS v4 now automatically detects the content directory unlike the previous version, where we manually listed the
layoutsandcontentsfiles. - Everything moves into your
styles.cssfile; we no longer use thetailwind.config.js. - The PostCSS plugin now lives in the new
@tailwindcss/postcsspackage.
Step 1: Installing new dependencies
To upgrade your project from v3 to v4:
npx @tailwindcss/upgradeThen install the new @tailwindcss/postcss package:
npm install @tailwindcss/postcssFrom here, you can safely remove postcss, postcss-cli, and autoprefixer from your package.json.
npm uninstall postcss postcss-cli autoprefixerWhen deploying the site on Vercel, make sure to install the dependencies as regular dependencies instead of DevDependencies to avoid deployment error.
Error: error building site: POSTCSS: failed to transform "/css/styles.css" (text/css):Error: Loading PostCSS Plugin failed: Cannot find module '@tailwindcss/oxide-linux-x64-gnu'Step 2: Removing old files
Since tailwind.config.js is no longer used, migrate your custom styles to styles.css and add @import "tailwindcss". Hereโs what it looks like now:
@import "tailwindcss"; // New import method
@plugin "@tailwindcss/typography";
@theme { --color-custom-100: oklch(0.98 0.04 113.22); --color-custom-200: oklch(0.94 0.11 115.03); ...}Step 3: Small adjustments (optional)
I renamed my postcss.config.js to postcss.config.mjs (this may not be necessary) to match the TailwindCSS docs, so we need to update the css.html with the new file name:
{{- $styles := resources.Get "css/styles.css" | postCSS (dict "config""./assets/css/postcss.config.mjs") -}}Step 4: Running the site ๐
You can now run the site locally using hugo server and check for the applied TailwindCSS styles.
For those who are starting a new Hugo project with TailwindCSS v4, you can use the Hugo TailwindCSS Starter Template to quickly get up and running. This template is a clean starting point with TailwindCSS v4 already configured, so you can focus on building your content.