Skip to content
the poor coder

tailwindcss nextjs setup

Poor Man
Published
Reading time
2 min read
On this page

Setting up TailwindCSS with Next.js#

If you're looking to set up TailwindCSS with Next.js, you're in the right place. I recently had to set up this combination for a project, and it took some trial and error. Here's what worked for me:

Step 1: Install TailwindCSS#

You can install TailwindCSS using npm. Open up your terminal and run:

Bash
npm install tailwindcss

This will install TailwindCSS in your project.

Step 2: Create a TailwindCSS configuration file#

Next, you'll need to create a configuration file for TailwindCSS. You can do this by running:

Bash
npx tailwindcss init

This will create a default configuration file called "tailwind.config.js" in your project root directory.

Step 3: Configure TailwindCSS in your Next.js project#

To configure TailwindCSS in your Next.js project, you'll need to create a CSS file and import TailwindCSS into it. You can do this by creating a new file called "styles.css" in your "public" directory and adding the following lines:

CSS
@tailwind base;
@tailwind components;
@tailwind utilities;

Then, in your Next.js project, create a new file called "_app.js" in your "pages" directory and add the following lines:

JavaScript
import '../public/styles.css'

function MyApp({ Component, pageProps }) {
  return 
}

export default MyApp

This will import your "styles.css" file into your Next.js project and apply the TailwindCSS styles to your components.

Step 4: Add PostCSS to your Next.js project#

To enable TailwindCSS in your Next.js project, you'll need to add PostCSS. You can do this by running:

Bash
npm install postcss-preset-env postcss-flexbugs-fixes

Then, create a new file called "postcss.config.js" in your project root directory and add the following lines:

JavaScript
module.exports = {
  plugins: [
    'postcss-flexbugs-fixes',
    [
      'postcss-preset-env',
      {
        autoprefixer: {
          flexbox: 'no-2009',
        },
        stage: 3,
      },
    ],
    'tailwindcss',
  ],
};

This will enable PostCSS and apply the necessary fixes and configurations for TailwindCSS.

Step 5: Start your Next.js project#

Finally, start your Next.js project and make sure that TailwindCSS is working correctly. You can do this by running:

Bash
npm run dev

This will start your Next.js project in development mode. Open up your browser and navigate to "http://localhost:3000". If everything is set up correctly, you should see the TailwindCSS styles applied to your components.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC