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
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
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
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}'],
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}'],
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;
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.
Resources
- Get Contrast PRO
Tailwind grid-How to use tailwind CSS grid templates in your project
How to create a Beautiful Responsive Navigation bar Using Tailwind CSS
Tailwind form-How to create and style a Responsive Form using Tailwind CSS
How to use tailwind CSS padding, margin and border in your project
How to create a beautiful React Bootstrap select with icons.
How to create a Beautiful Responsive Navigation bar Using Tailwind CSS
Tailwind Modal-How to create a React Modal using Tailwind CSS.
How to Implement a React Step Progress Bar Using Tailwind CSS
Top comments (0)