86

For some reason, TailwindCSS v3 is not rendering properly in Next.js. I'm wondering if something is wrong with my settings?

styles/tailwind.css

@tailwind base;

/* Write your own custom base styles here */

/* Start purging... */
@tailwind components;
/* Stop purging. */

/* Write you own custom component styles here */
.btn-blue {
  @apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
}

/* Start purging... */
@tailwind utilities;
/* Stop purging. */

/* Your own custom utilities */

_app.js

import React from "react";
// import "styles/global.scss";
import 'styles/tailwind.css'

import NavbarCustom from "components/Layout/NavbarCustom";
import Footer from "components/Layout/Footer";
import "util/analytics.js";
import { ProvideAuth } from "util/auth.js";

function MyApp({ Component, pageProps }) {
  return (
    <ProvideAuth>
      <>
        <NavbarCustom
          bg="white"
          variant="light"
          expand="md"
          logo="icons/Logo_512px.png"
        />

        <Component {...pageProps} />
      <>
    </ProvideAuth>
  )
}

What am I doing wrong? So confused, usually this sort of setup is fine.

tailwind.config.js

module.exports = {
  future: {
    removeDeprecatedGapUtilities: true,
  },
  purge: ['./components/**/*.{js,ts,jsx,tsx}', './pages/**/*.{js,ts,jsx,tsx}'],
  theme: {
    extend: {
      colors: {
        'accent-1': '#333',
      },
    },
  },
  variants: {},
  plugins: [],
}

postcss.config.js

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

{
  "name": "MoodMap",
  "version": "0.1.0",
  "private": true,
  "keywords": [
    "MoodMap"
  ],
  "dependencies": {
    "@analytics/google-analytics": "0.2.2",
    "@stripe/stripe-js": "^1.5.0",
    "analytics": "0.3.1",
    "fake-auth": "0.1.7",
    "mailchimp-api-v3": "1.13.1",
    "next": "9.5.3",
    "query-string": "6.9.0",
    "raw-body": "^2.4.1",
    "rc-year-calendar": "^1.0.2",
    "react": "16.12.0",
    "react-dom": "16.12.0",
    "react-hook-form": "4.10.1",
    "react-query": "2.12.1",
    "react-transition-group": "^4.4.1",
    "stripe": "^8.52.0"
  },
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "stripe-webhook": "stripe listen --forward-to localhost:3000/api/stripe-webhook"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "postcss-flexbugs-fixes": "^4.2.1",
    "postcss-preset-env": "^6.7.0",
    "stylelint": "^13.7.1",
    "stylelint-config-standard": "^20.0.0",
    "tailwindcss": "^1.8.9"
  }
}
2

48 Answers 48

120

For devs that created their project with nextJS.

be aware that the content of the tailwind.config.js needs the correct paths to the files.

module.exports = {
  content: [
    "./src/pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./src/components/**/*.{js,ts,jsx,tsx,mdx}",
    "./src/app/**/*.{js,ts,jsx,tsx,mdx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

For example, if you create a project with nextjs for example, you have a pages folder, where your index.js file is located. Therefore the code snippet (see below) on https://tailwindcss.com/docs/installation/using-postcss is not perfectly matching. Change it to the above or your own liking.

module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {},
  },
  plugins: [],
}
Sign up to request clarification or add additional context in comments.

4 Comments

You should also add your components folder to the content.
I forgot './src'
You save my day: I have an ui/ folder next to the app/ and if so, I had to add the path for it ("./src/ui/**/*.{js,ts,jsx,tsx,mdx}"). Obvious but it was blowing my mind ^^
Also make sure to add separate path for custom folders that you have added to your project. If you see some tailwind classes do not apply, this might be the potential issue for it!
104

In my case, I had set up the Tailwind config file correctly, but tailwind styles were not applying. Deleting the .next folder and running next dev helped.

5 Comments

Not sure why but this was it for me too. Thanks
I had to do that after rerunning npx tailwindcss init with the -p that I forgot the first time.
I deleted the .next folder, like above, then run npm run dev. That rebuilt the .next folder and all the Tailwind stuff started working. Thank you!!! @gaurav-thakur
I believe if you have ran npm run dev before adding tailwindcss than these are cached and don't load.
This worked for me as well. All I had to do was delete the .next folder and rerun npm run dev.
76

The same issue with my project, but I try to change the globals.css like this:

before

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900');

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

after

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import './base.css';
@import 'tailwindcss/utilities';

10 Comments

what about the '/base.css'; ? is it another file or import from tailwind?
@NicoGulo your custom css
but my custom css is inside globals.css. should I move to the base.css?
I wonder why is @tailwind doens't work and @import does work?
I got the same problem as OP. Tried AndriyFM's fix and it works. Then I tried a brand new create-next-app to see if the problem still persists. There's no problem. Then I went back to the previous project and replaced all @import rules with @tailwind rules. It still works. Considering that there's no such issue opened at Tailwind, I guess the problem has something to do with Next.js not purging properly.
|
51

I followed the official tutorial to install TailwindCSS into my NextJS app from here — https://tailwindcss.com/docs/guides/nextjs - but still couldn't get things working.

It turns out that I had forgotten to import global.css into my main app file.

Adding -

import "../styles/globals.css";

into _app.tsx solved it for me.

8 Comments

I migrated to the new /app directory and needed to add it to app/layout.tsx
Thank god this has 19 upvotes lol. Would've been a little worried about my life choices if it was only me lol.
Also, the relative path if you're using the page folder will be import "../app/globals.css"; inside the index.tsx of your page folder. If you're using the default npx create-next-app
Thanks, I deleted all css related things in layout.tsx when start a new project and forgot to add import "./globals.css" back using app router.
Thank you so much for this one line great solution. I have spent around one hour and cursed nextjs a lot. finally, this line relaxed me. Thank you again....
|
18

For me the solution was adding ./src/... to content sources in tailwind.config.js. Official Next.JS + Tailwindcss example doesn't support src folder.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
15

Importing like this in the global.css resolved the issue for me:

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

Comments

15

For anyone coming to this in 2023, and none of the above solutions work, it might sound obvious, but you need to make sure you have a postcss.config.js file, even if it's just empty, in your Next.js app.

If you don't, the styles won't run. I spent a little while trying to figure this out, including deleting build folders etc.

If you follow the docs, you should have one already, but if you don't (or accidentally deleted it), you can either run this in your terminal:

npx tailwindcss init -p 

Important note about running init command above - it will overwrite your tailwind config. Make sure you take a copy of it before running the above.

Or just create a postcss.config.js file in the root of your directory with following content:

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

5 Comments

My hero, I'd gotten fat fingered before committing and drag/dropped a folder before a lunch break leading to a fair bit of confusion... Interesting there is no 404/real error! Thanks!
you sir deserves a shower of beers. thanks a lot for this. it feels utterly wrong that in 2023 such a dark error can still happen.
yeah i agree, such a weird little issue but figured it out eventually
Sir, you are a Gentleman and a Scholar.
Another tip of the hat to you for this, as in my case running npx tailwindcss init -p didn't actually create this file, only the tailwind.config.js file.
14

One of my projects had those package versions installed

"next": "11.1.0",
"autoprefixer": "^10.3.3",
"postcss": "^8.3.6",
"tailwindcss": "^2.2.8"

this setting in tailwind.config.js was working:

module.exports = {
  content: [],
  theme: {
    extend: {},
  },
  plugins: [],
};

Recently I started a new project with these packages:

"next": "12.0.8",
"autoprefixer": "^10.4.2",
"postcss": "^8.4.5",
"tailwindcss": "^3.0.15"

The previous config setting was not working. So I changed it to :

module.exports = {
  content: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};

Comments

9

I got mine to work in the weirdest way after hours of fiddling with it; I just added a class to an element in one of my components and then wrote custom CSS for that class in the global.css file, then all tailwinds classes reflected. This might be a bug in tailwind's code that they need to trace and fix.

3 Comments

Please share a code example.
This worked for me; something similar to it did. I removed a style in globals.css, saved, then added it back. Tailwind then worked.
I did the same after trying and failing for more than a day and it worked like a charm, thank you. In my .tsx file I did this: <p className='myclass'>Some text</p> and in my global.css file I did that: @tailwind base; @tailwind components; @tailwind utilities; @layer { .myclass { @apply text-gray-600; }}
8

For people who are still having an issue with following versions:

"autoprefixer": "^10.4.7",
"postcss": "^8.4.13",
"tailwindcss": "^3.0.24",

in a Nx + Next.js environment, you can simply use following config files:

// postcss.config.js
const { join } = require('path');

module.exports = {
  plugins: {
    tailwindcss: {
      config: join(__dirname, 'tailwind.config.js'),
    },
    autoprefixer: {},
  },
};

// tailwind.config.js
const { join } = require('path');

module.exports = {
  content: [
    join(__dirname, './pages/**/*.{js,ts,jsx,tsx}'),
    join(__dirname, './src/**/*.{js,ts,jsx,tsx}'),
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};

3 Comments

ah thank you very much, it solves my issue! I've used tailwind 1.1.4 and postcss-preset-env and I want to upgrade to tailwind with version 3, and it solved by looking for this.
Sorry but I want to correct myself. I had pages and components in src folder and I just had to add ./src/ in tailwind.config.js. Thanks :)
Just wanted to say thank you. We are using NX and this was exactly what we need to get it sorted.
6

The issue for me was with a 'create-next-app' I did not have /pages/_app.js and therefore the global.css was not parsed.

Creating /pages/_app.js with the following content resolved the issue

import '../styles/globals.css'

// This default export is required in a new `pages/_app.js` file.
export default function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

See https://nextjs.org/docs/basic-features/built-in-css-support

2 Comments

FWIW, i just created with create-next-app and the file at src/pages/_app.js was created.
Yes and remember to create the styles folder inside the src folder if it does not exist.. And you can basically copy the content of the globals.css inside the app folder
4

I had just migrated my app from CRA to create-next-app. so my CRA App had a folder named features and under it was a folder for each feature was its Redux slicer and componentes, etc.

What solved it for me is to include the features folder in tailwind.config.js:

before:

content: [
  "./pages/**/*.{js,ts,jsx,tsx}",
  "./components/**/*.{js,ts,jsx,tsx}",
]

after

content: [
  "./pages/**/*.{js,ts,jsx,tsx}",
  "./components/**/*.{js,ts,jsx,tsx}",
  "./features/**/*.{js,ts,jsx,tsx}",
]

Comments

3

Add a _app.js file inside pages folder then add this code. The globals.css path should be correct.

import '../styles/globals.css'

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp

Comments

3

The problem for me was creating the application via npx create-next-app@latest and opting in for the new src folder structure:

? Would you like to use `src/` directory with this project? › No / Yes

If you select Yes, the following addition is required to the tailwind.config.js file:

module.exports = {
  content: [
    './src/**/*.{js,ts,jsx,tsx}',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};

Comments

2

After trying everything in this question, what worked was updating nextjs:

npm install next@latest

Comments

2

This doesn't sound like a sane person's solution, but my solution came down to simply editing globals.css. There is nothing more specific about the solution to share.

I started with a completely fresh Next.js project and followed instructions to add Tailwind CSS. I tried all the solutions listed in this project.

  • Used the example Next.js project creation script (worked)
  • Copied and pasted each file from the example project
  • Used the exact same versions
  • Deleted .next and reinstalled all packages
  • Used a variety of different ways to include Tailwind.

But the precise solution was this: I cut the contents of globals.css such that it only included the @tailwind directives (and some font @import directives above it), which suddenly activated Tailwind CSS. Pasting what I had just cut out maintained this newfound 'working' state.

I don't know how this could be, as there shouldn't be anything cached (I deleted the .next build folder) and I had been editing globals.css for other purposes (but I had not touched anything other than the tailwind-related directives). It's very mysterious and very annoying.

I hope this might save someone else from tearing their hair out.

Comments

2

Follow the official guide to install Tailwind.

https://tailwindcss.com/docs/guides/nextjs

If it still doesn't work, delete node_modules and package-lock.json, then run

npm i

again.

Comments

2

I just passed from Pages Router router to App Router.

In Pages Router Tailwindcss was asking to add this directive in tailwind.config.js when working on a NextJs project: important: "#__next" because the root node from the Nextjs app had an id of __next.

Please consider removing the important directive, from tailwind.config.js when working on NextJs App Router.

Comments

2

In _app.js add this line:

import "../styles/globals.css";

Replace code of tailwind.config.js with this:

module.exports = {
      content: [
        "./pages/**/*.{js,ts,jsx,tsx}",
        "./components/**/*.{js,ts,jsx,tsx}",
      ],
      theme: {
        extend: {},
      },
      plugins: [],
    };

Comments

2

For me tailwindcss broke as soon as I opened the (Chrome) Dev Tools while using next dev --turbopack.

Using it without --turbopack fixed this problem.

1 Comment

same here, thank you
2

I had an issue – tailwind styles were applied only after:

  1. Deleting .next folder
  2. Running npm run dev after
  3. For new styles repeat (1) and (2)

After some hours of debugging I found out that the problem is next.config.

For who is using TS, must use next.config.ts not next.config.js or next.config.mjs.

Comments

1

I had the same issue in some files.

After removing all inline tailwind classes and putting them in CSS files with @apply it works well.

Comments

1

Another simple potential cause is a misspelt components / pages folder - make sure they're spelt correctly.

Comments

1

I just experienced this. I think, my main problem was 'missing postcss file config' because I do tailwind initialization with npx tailwind init, not npx tailwind init -p (which also generate postcss fileconfig).

1 Comment

Exactly what happened to me when I started to follow the regular Installation guide tailwindcss.com/docs/installation the switched to the one for NextJS tailwindcss.com/docs/guides/nextjs
1

Another solution so that you can fix the problem in next js is this

in the tailwind.config.js add this configuration:

/** @type {import('tailwindcss').Config} */
module.exports = {
  purge: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  subject: {
    extend: {},
  },
  plugins: [require("@tailwindcss/typography"),require('@tailwindcss/forms'),],
  
};

and in postcss.config.js

module.exports = {
  plugins: ['tailwindcss','postcss-preset-env'],
};

lastly in the next js config you will add this to make it work for you.

/** @type {import('next').NextConfig} */
const nextConfig = {
  swcMinify: true,
  reactStrictMode: true,
  experimental: {
    concurrentFeatures: false, // <- Set this option to false.
    serverComponents: true,
  },
}

module.exports = nextConfig

and you need creating in styles and adding the style in global.css

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

Comments

1

I have just re-installed my Next.js project and went with Tailwind's guide and it worked!

Documentation: https://v2.tailwindcss.com/docs/guides/nextjs

Comments

1

After trying everything for 2 hours, just running:

npm i [email protected]

worked for me.

Comments

1

for me what was wrong that I unknowingly put spaces in regex. so my code became-

content: [
"./pages/**/*.{ js, ts, jsx, tsx}",
"./components/**/*.{ js, ts, jsx, tsx}",
],

and this was what causing error. so I again copy pasted from official website. to make it look like

content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],

I know it was a silly mistake but sometimes you do it unintentionally. :)

Comments

1

My issue was that I did not follow the set up steps properly. I forgot to run npx tailwind init which was the cause of my troubles. I created the tailwind.config.js but forgot to create proper postcss.config.js.

P/S: Make sure the content is pointing to the files you want covered, if its pointing to pages and components while you are using src file structure, tailwind will not work,

Happy coding :)

Comments

1

I struggled with this for 3 days. Turns out the main css file was called globals.css instead of globals.scss. You must use SASS instead of regular CSS because the @tailwind annotation only works in SASS.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.