DEV Community

Cover image for How to use Tailwind CSS in Nextjs
Sampson Ovuoba for Devwares

Posted on • Originally published at devwares.com on

How to use Tailwind CSS in Nextjs

Tailwind CSS is a CSS framework that can be included in any of your projects.

Tailwind CSS is not like other CSS frameworks like bootstrap, material, etc., in the sense that it is based on utility-first.

This means it doesn’t focus on pre-designed components like buttons, cards, etc. It’s a low-level utility class that allows you to build a custom website from scratch without leaving your HTML file.

In this tailwind CSS tutorial, we will look at how to include Tailwind CSS in Nextjs project.

Table of content

  • Setting the Next.js
  • Install Tailwind CSS
  • Setting up the config files
  • Purging of Tailwind
  • Including Tailwind in your Application
  • Conclusion

Setting the Next.js

To set up a nextjs project is easy. Although we have to set this up before we go ahead to install our Tailwind CSS to the technology. To set up your nextjs project all you have to do is simply write the following commands on your terminal

npx create-next-app -e with-tailwindcss my-project
cd my-project
Enter fullscreen mode Exit fullscreen mode

Install Tailwind CSS

once the nextjs application is running you can go ahead to add Tailwind CSS. You can do this by running the following code on your terminal

npm install -D tailwindcss@latest postcss@latest autoprefixer@latest
Enter fullscreen mode Exit fullscreen mode

This will install tailwind CSS and its dependencies.

Setting up the config files

At this point we have installed our Tailwind CSS but in order to use it we need to include our configuration files. We can do this by running the following command on our terminal

npx tailwindcss init -p
Enter fullscreen mode Exit fullscreen mode

This command will create the tailwind.config.js and posts.config.js files. This files are important in configuring our Tailwind CSS properly.

Purging of Tailwind

Now, that we have installed our Tailwind CSS to our nextjs project. There are a few things we need to complete in our Configuration file in order to be set. You can go ahead and open the tailwind.config.js file now. In our configuration file we are going to purge for our Next.js file.
If you are using the Tailwind version 2 you can change the configuration file to the ones below.

purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
Enter fullscreen mode Exit fullscreen mode

while those that are using the version 3 can change theirs to look like the configuration below.

content: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
Enter fullscreen mode Exit fullscreen mode

Including Tailwind in your Application

We can now include Tailwind to our global CSS file on Nextjs application. These can be done by simply creating a CSS file on the Nextjs application and add the following code to the file.

@tailwind base; 
@tailwind components; 
@tailwind utilities;
Enter fullscreen mode Exit fullscreen mode

Also make sure to import this Global CSS file you created is imported into the App.js file.
With this you are ready to start using Tailwind CSS on your Nextjs file.
Happy coding.

Design and code tailwind CSS websites 3x faster

We created a tool to visually build tailwind CSS components, prototypes, websites, and web apps. Ship projects faster using an intuitive tailwind builder and editor.Try Windframe out for free.

WINDFRAME

Resources

Top comments (0)