🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Styling••By Komal Nakrani

Astro PostCSS Setup: Config File, Plugins and Gotchas

Astro PostCSS works through Vite: add a postcss.config.mjs at the project root and list plugins. Autoprefixer, nesting, Tailwind 4 notes and inline config, with code.

#Astro#PostCSS#Styling#CSS

Astro PostCSS support comes from Vite: create a postcss.config.mjs file in the project root, list your plugins, and Astro runs them over every stylesheet, including scoped <style> blocks in .astro files. There is no integration to install and nothing to add to astro.config.mjs.

Key Takeaways

  • Vite picks up postcss.config.mjs, .cjs or .js from the project root automatically.
  • Plugins apply to imported CSS files, <style> blocks and CSS modules.
  • Tailwind CSS 4 in Astro uses the @tailwindcss/vite plugin, so you do not need PostCSS for Tailwind.
  • You can also pass PostCSS config inline through vite.css.postcss.

Add a PostCSS config

Install the plugins you want. Autoprefixer is the most common:

npm install -D postcss autoprefixer
// postcss.config.mjs
import autoprefixer from 'autoprefixer';

export default {
  plugins: [autoprefixer()],
};

Restart the dev server after adding or changing the file. Autoprefixer reads your browserslist setting, so add one to package.json to control which browsers get prefixes:

{
  "browserslist": ["> 0.5%", "last 2 versions", "not dead"]
}

Common plugins

// postcss.config.mjs
import autoprefixer from 'autoprefixer';
import postcssNesting from 'postcss-nesting';
import postcssCustomMedia from 'postcss-custom-media';

export default {
  plugins: [postcssCustomMedia(), postcssNesting(), autoprefixer()],
};

Order matters: plugins run top to bottom, so syntax transforms such as custom media and nesting go before Autoprefixer. Native CSS nesting is supported by current browsers, so add postcss-nesting only if you target older ones.

With custom media, you can define breakpoints once and use them in any component:

/* src/styles/global.css */
@custom-media --md (min-width: 48rem);
---
// src/components/Hero.astro
---
<section class="hero"><slot /></section>
<style>
  .hero { padding: 2rem; }
  @media (--md) {
    .hero { padding: 4rem; }
  }
</style>

Custom media defined in one file is only visible to that file unless the plugin is configured to import it, so check the plugin’s options for shared definitions.

Inline config instead of a file

If you prefer one config file, pass PostCSS options through Vite:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import autoprefixer from 'autoprefixer';

export default defineConfig({
  vite: {
    css: {
      postcss: { plugins: [autoprefixer()] },
    },
  },
});

When vite.css.postcss is set, Vite does not look for a postcss.config.* file, so keep only one of the two.

Tailwind CSS and PostCSS

Tailwind 3 projects often have a postcss.config with tailwindcss and autoprefixer. Tailwind 4 in Astro runs as a Vite plugin (@tailwindcss/vite) and handles prefixing and nesting itself, so you can delete the Tailwind entries from PostCSS after upgrading. Our Tailwind CSS setup guide covers the current setup, and the Sass setup tip covers the other common preprocessor.

Troubleshooting

  • Plugin seems ignored. Restart the dev server; config files are read at startup.
  • “Unexpected token export”. Your package.json lacks "type": "module" and the file is .js. Rename it to .mjs.
  • Prefixes missing. Check browserslist. If every target supports a property unprefixed, Autoprefixer adds nothing.

The Vite PostCSS docs describe how config is discovered. Our Astro themes ship without a PostCSS config unless a plugin is needed, and the theme styling docs explain where to add one.

FAQ

Does Astro use PostCSS by default?

Vite processes CSS with PostCSS whenever it finds a PostCSS config, but Astro ships no plugins of its own. Without a config file, your CSS passes through unchanged apart from scoping and minification.

Does Astro add vendor prefixes automatically?

No. Add Autoprefixer through a PostCSS config if you need prefixes for older browsers. Most current browsers need very few.

Do I need PostCSS for Tailwind CSS 4 in Astro?

No. Tailwind 4 integrates through the @tailwindcss/vite plugin, which npx astro add tailwind sets up. PostCSS is only needed for other plugins you choose to use.