Next.jsMedium

How do you use PostCSS in Next.js?

Next.js runs PostCSS on all CSS automatically. Add a postcss.config.mjs (or .js/.json) at the project root to customize plugins. Tailwind v4 is itself a PostCSS plugin (@tailwindcss/postcss), and when you provide a config, it replaces Next.js's defaults entirely.

Defaults (no config file)

Next.js uses Autoprefixer and postcss-flexbugs-fixes, with postcss-preset-env features such as targeting your browserslist. CSS nesting and modern syntax are handled for supported browsers.

Custom config

  • Write plugin names as strings, not imported functions: { plugins: { "@tailwindcss/postcss": {} } }. Next.js loads them itself so it can cache the work.
  • Once you add a config, Next.js does not merge in its defaults. Add autoprefixer back if you still need it (Tailwind v4 already handles vendor prefixes through Lightning CSS).
  • Browser targets come from browserslist in package.json.

Turbopack

Turbopack supports PostCSS configs. Since 16.4, experimental.turbopackLocalPostcssConfig allows per-directory configs, and plugin execution can run in worker threads with turbopackPluginRuntimeStrategy.

Alternatives

  • experimental.useLightningcss: true swaps in Lightning CSS (Rust) for faster CSS processing.
  • Native CSS nesting and modern color functions reduce the need for many PostCSS plugins.

Common plugins

@tailwindcss/postcss, autoprefixer, postcss-preset-env, postcss-nesting and cssnano (Next.js already minifies CSS in production).

Code example

tsx
1// postcss.config.mjs - Tailwind v4 only
2const config = {
3 plugins: {
4 "@tailwindcss/postcss": {},
5 },
6};
7export default config;
8
9// postcss.config.mjs - without Tailwind, restoring defaults explicitly
10// export default {
11// plugins: {
12// "postcss-flexbugs-fixes": {},
13// "postcss-preset-env": {
14// autoprefixer: { flexbox: "no-2009" },
15// stage: 3,
16// features: { "custom-properties": false },
17// },
18// },
19// };
20
21// package.json
22// "browserslist": ["chrome 111", "edge 111", "firefox 111", "safari 16.4"]

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app