• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License

Add Tailwind CSS to Next.js

Aug 3, 2022

Learn how to install and use Tailwind CSS in your Next.js website

Tailwind CSS is the hottest CSS framework in the past few years: it's fun and easy to use, with wonderful defaults that make designing beautiful UIs a breeze.

To install TailwindCSS in your Next.js project, run the following command:

text
npm install -D tailwindcss postcss

Initialize the Tailwind CSS configuration

Afterward, we run the following command to initialize the Tailwind configuration:

text
npx tailwindcss init

This will create a file named tailwind.config.js in your root folder. We have to change the content glob to use files with the tsx extension:

js
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./**/*.{tsx}"],
theme: {
extend: {},
},
plugins: [],
}

Add the PostCSS configuration file

Additionally, we have to create a postcss configuration that Next.js will automatically pick up. To do so, create a file names postcss.config.js in your root folder and add the following content:

js
module.exports = {
plugins: {
tailwindcss: {},
},
};

Import Tailwind CSS in your styles

At this point, we have to import Tailwind CSS in your global styles, normally located at styles/index.css. To do so add the following content to your main CSS file:

css
@tailwind base;
@tailwind components;
@tailwind utilities;

To test if this works, visit your home page at pages/index.tsx and add some styles such as:

html
<h1 className='text-5xl'>
This is Tailwind CSS with Next.js!
</h1>

Installing the Tailwind CSS Prettier plugin

If you're using Prettier, you can install a plugin to automatically sort the class names in your HTML tags according to the best practices prescribed by the Tailwind CSS team, so to make your code clean and consistent.

To install the Prettier plugin, use the following command:

text
npm i prettier-plugin-tailwindcss

If it's all good, you don't have to do anything else, it will simply work when running Prettier, which we recommend doing each time you save your files.