Tailwindcss not working with next.js; what is wrong with the configuration?
Solution 1
I think your setup is too big. You can achieve this with much simpler stuff nowdays.
First, I don't think CSS needs to be loaded into nextjs anymore and modules are supported natively. (So you can delete this withCSS stuff)
Second, tailwind doesn't need such elaborate setup anymore, if you are using the newer versions.
So you will need to install postcss-preset-env, but it does remove the need for big config now.
Check out this example https://github.com/vercel/next.js/tree/canary/examples/with-tailwindcss
Solution 2
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';
Solution 3
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: [],
};
Solution 4
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
-
LeCoda about 2 years
For some reason,
tailwind
is not rendering properly in next.js.I'm wondering if something is wrong with my settings?
Styles folder - 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} />
What am I doing wrong? so confused, usually this sort of setup is fine.
This is the site btw - https://mmap-1xr646x4a.vercel.app/ .
using the information below, made changes and still same issue weirdly.
https://mmap-hewlbern.moodmap.vercel.app/ is the site example.
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" } }
Thanks!
-
hypercloud over 3 yearsDoes your css import in _app.js work properly? Try to delete next.config.js as well :)
-
doublejosh almost 3 yearsThis is a non-answer. "Your setup is too complicated" is a useless opinion.
-
NicoGulo over 2 yearswhat about the
'/base.css';
? is it another file or import from tailwind? -
AndriyFM over 2 years@NicoGulo your custom css
-
NicoGulo over 2 yearsbut my custom css is inside globals.css. should I move to the base.css?
-
joshuaiz over 2 yearsThis is what worked for me. Thank you!
-
Admin over 2 yearsYour 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.
-
Pushp Singh about 2 yearsReadme is not giving complete information. Also what you have is the basic tailwind CSS. trying that is not solving the issue.
-
Ahmed Elsayed about 2 yearsworked for me thank you
-
Akshay K Nair about 2 yearsThat last bit worked flawlessly.
-
Nurul Huda about 2 yearsI wonder why is
@tailwind
doens't work and@import
does work? -
kldavis4 almost 2 yearsthis worked for me. can someone explain why this fixes it?
-
Arjun Nayak almost 2 yearsLooks like something has changed in newer version of tailwind.. I had made a project few months back and it was fine.. Today I created a new one this fix was required for it to work.
-
bytrangle almost 2 yearsI 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.