Tailwind is a utility-first CSS framework for rapidly building custom user interfaces. This guide will show you how to get started with Gatsby and Tailwind CSS.
There are three ways you can use Tailwind with Gatsby:
- Standard: Use PostCSS to generate Tailwind classes, then you can apply those classes using
- CSS-in-JS: Integrate Tailwind classes into Styled Components.
- SCSS: Use gatsby-plugin-sass to support Tailwind classes in your SCSS files.
You have to install and configure Tailwind for all of these methods, so this guide will walk through that step first, then you can follow the instructions for PostCSS, CSS-in-JS or SCSS.
This guide assumes that you have a Gatsby project set up. If you need to set up a project, head to the Quick Start guide, then come back.
- Install Tailwind
- Generate Tailwind config file (optional)
Note: A config file isn’t required for Tailwind 1.0.0+
To configure Tailwind, you’ll need to add a Tailwind configuration file. Luckily, Tailwind has a built-in script to do this. Just run the following command:
- Install the Gatsby PostCSS plugin gatsby-plugin-postcss.
- Include the plugin in your
- Configure PostCSS to use Tailwind
postcss.config.js in your project’s root folder with the following contents.
- Use the Tailwind Directives in your CSS
You can now use the
@tailwind directives to add Tailwind’s utilities, preflight, and components into your CSS. You can also use
@apply and all of Tailwind’s other directives and functions!
To learn more about how to use Tailwind in your CSS, visit the Tailwind Documentation
These steps assume you have a CSS-in-JS library already installed, and the examples are based on Styled Components.
- Install Tailwind Babel Macro
tailwind.macro isn’t currently compatible with Tailwind 1.0.0+. However, a new forked project can be found at
twin.macro that supports Tailwindcss v1.2 classes. It’s currently in pre-release so not all plugins are supported at the time of writing. Alternatively, you can revert to Tailwind 0.7.4.
Option 1: Install
twin.macro and use Tailwind 1.2.0+
- Install Twin and Emotion
- Import the Tailwind base styles
- Enable the Gatsby emotion plugin
Option 2: Install stable
tailwind.macro and use Tailwind 0.7.4
- Use the Babel Macro (
tailwind.macro) in your styled component
- Install the Gatsby SCSS plugin gatsby-plugin-sass and
- To be able to use Tailwind classes in your SCSS files, add the
tailwindcsspackage into the
postCSSPluginsparameter in your
Note: Optionally you can add a corresponding configuration file (by default it will be
If you are adding a custom configuration, you will need to load it after