How to configure Tailwind in a Next.js project
Stewart Granger Flores - 11 Noviembre, 2020
Upgrade 2021
npm install tailwindcss -D
npx tailwindcss init --full
module.exports = {
plugins: ["postcss-import", "tailwindcss", "autoprefixer"],
};
We continue to install postcss-preset-env which helps convert your CSS code into something that all browsers can understand when building your project.
npm install postcss-preset-env -D
Now we will create inside the styles folder created by Next.js a file named tailwind.css and inside we will put the following:
/* purgecss start ignore */
@tailwind base;
@tailwind components;
/* purgecss end ignore */
@tailwind utilities;
and we import it in _app.js so that it is contemplated in each page that we make in our project.
import "../styles/globals.css";
import "../styles/tailwind.css"; // <---- aquí
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
export default MyApp;
Now in order to minimize the amount of CSS that is created in our build, we will use PostCSS PurgeCSSwhose task is to identify ALL the CSS that we are not using in our project and purge it, to make a smaller bundle.
npm install @fullhuman/postcss-purgecss -D
npm install autoprefixer
npm install postcss-import
With all this, you should now have your project working with TailwindCSS.🎉