CamKode

How to install Tailwind CSS in Next.js

Avatar of Kosal Ang

Kosal Ang

Thu Jul 28 2022

How to install Tailwind CSS in Next.js

Tailwind is a popular utility first CSS framework for rapidly building custom User Interfaces. It works by scanning all of your HTML files, JavaScript components, and any other templates for class names, generating the corresponding styles and then writing them to a static CSS file. You can learn more about Tailwind CSS here.

Next.js is a flexible React framework that gives you building blocks to create fast web applications. Next.js allow the web page to be rendered on the server, which is great for performance and SEO. You can learn more about Next.js here.

Step 1: Create your project

Start by creating a new Next.js project if you don’t have one set up already. You can create a new Next application using the command below.

1npx create-next-app my-project
2cd my-project
3

Step 2: Install Tailwind CSS

After your next project is created, Install tailwindcss and its peer dependencies via npm

1npm install -D tailwindcss postcss autoprefixer
2

Step 3: Install Tailwind CSS

Then run the init command to generate postcss.config.js and tailwind.config.js.

1npx tailwindcss init -p
2

The above command will create two configuration files in your project.

1// tailwind.config.js
2/** @type {import('tailwindcss').Config} */
3module.exports = {
4  content: [],
5  theme: {
6    extend: {},
7  },
8  plugins: [],
9}
10
1// postcss.config.js
2module.exports = {
3  plugins: {
4    tailwindcss: {},
5    autoprefixer: {},
6  },
7}
8

Step 4: Configure your template paths

Add the paths to all of your template files in your tailwind.config.js file. So, in your tailwind.config.js file, add the following configuration.

1// tailwind.config.js
2/** @type {import('tailwindcss').Config} */
3module.exports = {
4  content: [
5    "./pages/**/*.{js,ts,jsx,tsx}",
6    "./components/**/*.{js,ts,jsx,tsx}",
7  ],
8  theme: {
9    extend: {},
10  },
11  plugins: [],
12}
13

Step 5: Add Tailwind directives to your global CSS

In your ./styles/globals.css file, add the directives of tailwind CSS.

1@tailwind base;
2@tailwind components;
3@tailwind utilities;
4

Step 6: Testing Tailwind in your project

To verify that everything will work as expected, replace all of the default code in pages/index.js with the following:

1export default function Home() {
2  return (
3    <div class="flex justify-center">
4      <div class="rounded-lg shadow-lg bg-white max-w-sm">
5        <a href="#!" data-mdb-ripple="true" data-mdb-ripple-color="light">
6          <img class="rounded-t-lg" src="https://mdbootstrap.com/img/new/standard/nature/182.jpg" alt="" />
7        </a>
8        <div class="p-6">
9          <h5 class="text-gray-900 text-xl font-medium mb-2">Card title</h5>
10          <p class="text-gray-700 text-base mb-4">
11            Some quick example text to build on the card title and make up the bulk of the card's
12            content.
13          </p>
14          <button type="button" class=" inline-block px-6 py-2.5 bg-blue-600 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out">Button</button>
15        </div>
16      </div>
17    </div>
18  )
19}
20

Final step: Start your project up and running

1npm run dev
2

Go to http://localhost:3000, you should get a result as below:

How to install Tailwind CSS in Next.js

I hope you got something from this article that will help you in your journey. Thanks for reading!!!

Related Posts

Create a Responsive Navigation Bar using HTML, CSS, and JavaScript

Create a Responsive Navigation Bar using HTML, CSS, and JavaScript

Creating a beautiful menu bar using HTML, CSS, and JavaScript involves creating the structure with HTML

Asynchronous JavaScript: Promises and Async/Await Explained

Asynchronous JavaScript: Promises and Async/Await Explained

JavaScript uses asynchronous programming. Promises and `async`/`await` are two powerful features that facilitate asynchronous operations in JavaScript

How to Merge Enumerations in TypeScript

How to Merge Enumerations in TypeScript

There are scenarios where you might want to merge multiple enums into one, combining their respective values into a single type

Understanding Closures and Scope in JavaScript

Understanding Closures and Scope in JavaScript

JavaScript's closures and scope are fundamental concepts that significantly impact how code behaves

Creating a Responsive Sidebar with Icons and Expandable Menus

Creating a Responsive Sidebar with Icons and Expandable Menus

If you're looking to build a responsive sidebar with icons and expandable menus, this HTML and CSS template can serve as a great starting point

Working with Arrays and Objects in JavaScript

Working with Arrays and Objects in JavaScript

Arrays and objects in JavaScript is fundamental for managing and manipulating data

ES6 Features: Arrow Functions, Destructuring, and Spread Operators

ES6 Features: Arrow Functions, Destructuring, and Spread Operators

ECMAScript 6, also known as ES6 or ECMAScript 2015, introduced several powerful features that significantly enhanced the capabilities of JavaScript

Get Monday and Sunday of the Week in JavaScript

Get Monday and Sunday of the Week in JavaScript

To get the Monday and Sunday of the week, use the setDate() method to set the day of the month of a date to the Monday and Sunday

Find all Permutations in JavaScript

Find all Permutations in JavaScript

Generate all permutations of string and filter unique string by using JavaScript

JavaScript Fundamentals: Understanding the Basics

JavaScript Fundamentals: Understanding the Basics

JavaScript is a versatile scripting language primarily used for client-side web development. It enables interactivity and dynamic content on web pages.

© 2024 CamKode. All rights reserved

FacebookTwitterYouTube