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.
Open the ./src/styles/global.css file and delete the existing styles. Then use the @tailwind directive as shown below to include Tailwind's base, components, and utilities styles, replacing the original file contents:
/* ./src/styles/global.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
Tailwind will swap these directives out at build-time with all of the styles it generates based on your configured design system.
Remember our layout.js component is wrapped around the contents of every page and ../styles/global.css is already imported into it so the Tailwind styles will now be available.
Install the VS Code extension Tailwind CSS IntelliSense by Tailwind Labs. You can search for it on the extensions panel and then click install. Alternatively, click the link above and click install a couple times.
Open the command palette View > Command Palette and type settings and then choose Preferences: Open Settings (JSON)
We don't need to hard-code a specific hex value for the gray background because Tailwind has a design system built in with the same gray color.
Normally Tailwind suggests placing css classes directly on the element (see code example below but DO NOT try to make this change) they apply to but since Gatsby doesn't give us access to the index.html page easily we are placing the style in globabl.css.
There are other solutions to this problem like using React Helment but I think this is the simplest solution.
Don't make this change. The code below is shown for demonstration purposes only.
Re-apply the styles we already set throughout the site using CSS, CSS Modules, and Emotion. The finished code for each file is shown below but I recommend manually translating the first couple files to get a feel for Tailwind's Utility-first workflow.