postImage

How to use and set up Tailwind CSS with Nextjs.

blogImage

By Emmanuel Chinonso

Web Developer and Writer

Last updated: 20 May 2024

Tailwind With Nexjs

Tailwind CSS is a highly customizable, low-level CSS framework that gives you all of the building blocks you need to build bespoke designs without any annoying opinionated styles you have to fight to override. Integrating Tailwind CSS with Nextjs is straightforward and enhances your development workflow. Let’s dive into the setup process.

Prerequisites

Before we dive into the setup, ensure you have the following prerequisites:

  • Node.js: Installed on your machine.
  • Basic knowledge of JavaScript and React.
  • Familiarity with Next.js.

Table of content

  • Step 1: Create a Next.js Project
  • Step 2: Install Tailwind CSS Dependencies
  • Step 3: Configure tailwind.config.js
  • Step 4: Create Your Styles
  • Step 5: Import Tailwind CSS Styles
  • Step 6: Start Your Next.js Project
  • Conclusion

Step 1: Create a Next.js Project

If you haven't already, create a new Next.js project using the following command in your terminal:

bash
npx create-next-app my-nextjs-project

Replace "my-nextjs-project" with your desired project name.

Step 2: Install Tailwind CSS Dependencies

Navigate to your project directory:

bash
cd my-nextjs-project

Install the necessary dependencies for Tailwind CSS:

bash
npm install tailwindcss postcss autoprefixer

This command installs Tailwind CSS, PostCSS, and Autoprefixer. It also generates tailwind.config.js and postcss.config.js files in your project

Step 3: Configure tailwind.config.js

Open the tailwind.config.js file in your code editor. Customize the configuration based on your project requirements. For example, you can define custom colors, fonts, and other styling options.

js
module.exports = {
content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
// Add more paths here if you have them
],
// Other configurations...
}

Step 4: Create Your Styles

Create a new CSS file, for example, styles/tailwind.css, and include the following:

css
/* styles/tailwind.css */
@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

Step 5: Import Tailwind CSS Styles

In your pages _app.js file, import the globals.css stylesheet to apply the styles globally to every route in your application

javascript
// pages/_app.js
import '../styles/tailwind.css';
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
export default MyApp;

Step 6: Start Your Next.js Project

Now that everything is set up, start your Next.js development server:

bash
npm run dev

Visit http://localhost:3000 in your browser, and you should see your Next.js project with Tailwind CSS styles applied.

Conclusion

Congratulations! You’ve successfully installed and set up Tailwind CSS in your Next.js project. By leveraging the power of Tailwind’s utility classes and Next.js features, you can prototype, design, and build responsive user interfaces efficiently.

Resources

Build modern projects using Bootstrap 5 and Contrast

Trying to create components and pages for a web app or website from scratch while maintaining a modern User interface can be very tedious. This is why we created Contrast, to help drastically reduce the amount of time we spend doing that. so we can focus on building some other aspects of the project.

Contrast Bootstrap PRO consists of a Premium UI Kit Library featuring over 10000+ component variants. Which even comes bundled together with its own admin template comprising of 5 admin dashboards and 23+ additional admin and multipurpose pages for building almost any type of website or web app.
See a demo and learn more about Contrast Bootstrap Pro by clicking here.

Related Posts