Add Tailwind CSS to Next.js

Pavithra Kodmad
InstructorPavithra Kodmad
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

We will learn the various steps necessary to add tailwindcss support to a nextjs application.

Step 1 - Install tailwindcss, postcss and autoprefixer

Step 2 - Run npx tailwind init -p to create the required configuration. Add purge filters here.

Step 3 - Add a styles folder and a tailwind.css file. In this file import the important tailwind classes.

Step 4 - Override nextjs' app component to include tailwind classes.

Now you can start using tailwindcss classes to style your nextjs application

Pavithra Kodmad: [0:00] Hello, everyone. In this video, we'll demonstrate how to add Tailwind CSS to a Next.js project. This is a Next.js project with one page as index.js, where I've written a few lines of HTML. We'll npm install tailwindcss, postcss, and autoprefixer as developer dependencies first.

[0:26] Next, we can run the Tailwind command with npx to initialize the configuration. This will generate a tailwind.config.js, which has room for all the parameters we would use later in the application. For example, I have added a few custom colors that I'll be referring to later. We can also add purge paths to purge unnecessary CSS.

[0:56] In postcss.config.js, we see that tailwindcss and autoprefixer have already been configured as plugins.

[1:08] Next, we create a folder called styles, inside which we can create a CSS file named, for example, tailwind.css. This is up to you. In this CSS file, we'll import the major class groups of tailwindcss that will be important for us in the application. These are base, components, and utilities.

[1:39] In the pages folder, we will now create the secret _app.js file, which constitutes Next.js's internal app, and include tailwind.css as default for all components. We will then export the component with the right page properties.

[2:08] After all this configuration, we can now directly add the tailwindcss classes to our JSX component home page. You can see here that I've added various classes to nicely style my home page.

[2:26] In summary, we learned that to add Tailwind CSS to Next.js, we'd have to perform certain steps, the first of which is to add tailwindcss, postcss, and autoprefixer as developer dependencies, after which we'll configure the tailwind.config.js and postcss.config.js with all the parameters as required, and then create a tailwind.css file and import all the class groups of tailwind that we'll be needing, then use _app.js file to import tailwind.css as default for all pages in Next.js.

[3:12] This is how you can style your pages in Next.js with Tailwind CSS.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today