The easy way to add Tailwind CSS to Gatsby
Posted
Updated
Europeโs developer-focused job platform
Let companies apply to you
Developer-focused, salary and tech stack upfront.
Just one profile, no job applications!
There are several ways of adding Tailwind CSS to Gatsby, and a lot of them are too complex or adding unnecessary code. Tailwind CSS can be added with PostCSS in a simple way.
Create two configs for Tailwind CSS
and PostCSS
.
touch tailwind.config.js
touch postcss.config.js
๐ฐ The Pragmatic Programmer: journey to mastery. ๐ฐ One of the best books in software development, sold over 200,000 times.
Copy basic theme styling to tailwind.config.js. This can be adjusted to your needs, refer to the official docs.
const colors = require('tailwindcss/colors');
module.exports = {
theme: {
colors: {
gray: colors.coolGray,
blue: colors.lightBlue,
red: colors.rose,
pink: colors.fuchsia,
},
fontFamily: {
sans: ['Graphik', 'sans-serif'],
serif: ['Merriweather', 'serif'],
},
extend: {
spacing: {
'128': '32rem',
'144': '36rem',
},
borderRadius: {
'4xl': '2rem',
},
},
},
variants: {
extend: {
borderColor: ['focus-visible'],
opacity: ['disabled'],
},
},
};
postcss.config.js:
module.exports = () => ({
plugins: [require('tailwindcss')],
});
npm install --save gatsby-plugin-postcss tailwindcss
To get PostCSS to trigger properly in the build process, you have to add the gatsby-plugin-postcss
to your Gatsby config.
gatbsy-config.js
module.exports = {
"plugins": [
// All other plugins
`gatsby-plugin-postcss`
]
})
The last step is to import Tailwind CSS in the gatsby-browser.js
.
import 'tailwindcss/base.css';
import 'tailwindcss/components.css';
import 'tailwindcss/utilities.css';
๐๐๐ Congratulations! ๐๐๐ You have successfully added TailwindCSS to your website and can simply add Tailwind classes to your HTML.
Thanks for reading and if you have any questions, use the comment function or send me a message @mariokandut. If you want to know more about Gatsby, have a look at these Gatsby Tutorials.
References (and Big thanks):
Never miss an article.