Installation
Setting up Tailwind CSS in a Gatsby project.
Start by creating a new Gatsby project if you don't have one set up already. The most common approach is to use Gatsby CLI.
gatsby new my-projectcd my-projectInstall @tailwindcss/webpack and its peer dependencies via npm.
npm install tailwindcss @tailwindcss/webpackCreate a gatsby-node.js file in the root of your project (or add to the existing one) and configure the @tailwindcss/webpack loader.
exports.onCreateWebpackConfig = ({ actions, getConfig }) => { const config = getConfig() config.module.rules.push({ test: /\.css$/i, use: ['@tailwindcss/webpack'], }) actions.replaceWebpackConfig(config)}Create a ./src/styles/global.css file and add an @import for Tailwind CSS.
@import "tailwindcss";Create a gatsby-browser.js file at the root of your project if it doesn't already exist, and import your newly-created ./src/styles/global.css file.
import './src/styles/global.css';Run your build process with gatsby develop.
gatsby developStart using Tailwind's utility classes to style your content.
export default function IndexPage() { return ( <Layout> <h1 className="text-3xl font-bold underline"> Hello world! </h1> </Layout> )}