If your project uses PostCSS, it's probably for three things: newer CSS syntax like nesting, vendor prefixes, and minifying. That usually takes a few plugins, like postcss-preset-env and autoprefixer, each one parsing your CSS and passing it to the next.
Lightning CSS does all three in one tool. It's written in Rust by the team behind Parcel, and it's fast. Vite supports it without any extra plugins.
If you're on Vite 8
Vite 8 already includes Lightning CSS and uses it to minify your CSS when you build. That step also adds vendor prefixes and flattens nesting for the browsers you're targeting (more on that in "Picking your browsers" below). So you may be able to remove autoprefixer and postcss-preset-env without changing anything else.
The rest of this article covers using Lightning CSS for all your CSS, so dev matches your production build and you can use features like custom media queries.
Switching your CSS to Lightning CSS
Before you switch, know that:
- Your PostCSS config stops running. Vite uses one or the other, not both.
- CSS Modules options move.
css.modules is ignored. Use css.lightningcss.cssModules instead.
- Vite still calls this experimental.
On Vite 7 or earlier, install it first:
npm install -D lightningcss
Then set css.transformer in your Vite config:
import { defineConfig } from 'vite';
export default defineConfig({
css: {
transformer: 'lightningcss',
},
});
On Vite 7 or earlier, also add cssMinify: 'lightningcss' under build so it handles minifying too. Vite 8 does this by default.
For more on the config file, see How to Configure Your Vite Config File.
Picking your browsers
Lightning CSS only adds prefixes and flattens nesting for browsers that need it. To decide that, it needs to know which browsers you want to support. If you don't tell it, Vite picks a default that covers browsers from roughly the last few years (Chrome 111, Firefox 114, Safari 16.4 and newer).
To pick your own, use Browserslist. It's a common way to describe the browsers you support with a short query like >= 0.25%, not dead (any browser with at least 0.25% usage that's still getting updates).
Install both packages. (Even on Vite 8, you need lightningcss installed in your project to import from it in your config.)
npm install -D lightningcss browserslist
Then pass your query to Lightning CSS:
import { defineConfig } from 'vite';
import { browserslistToTargets } from 'lightningcss';
import browserslist from 'browserslist';
export default defineConfig({
css: {
transformer: 'lightningcss',
lightningcss: {
targets: browserslistToTargets(browserslist('>= 0.25%, not dead')),
},
},
});
If you already list your browsers in package.json or a .browserslistrc file, call browserslist() with no query and it'll read them from there.
There's one gotcha. When you build, Vite minifies your CSS as a last step, and that step ignores the browser list above. It uses a different setting, build.cssTarget. If that setting targets newer browsers than your list, the minifier can remove prefixes your older browsers need.
So if you support older browsers, add build.cssTarget to your vite.config.js too, listing the oldest version of each browser you support. Pick versions that roughly match your browser list:
import { defineConfig } from 'vite';
import { browserslistToTargets } from 'lightningcss';
import browserslist from 'browserslist';
export default defineConfig({
css: {
transformer: 'lightningcss',
lightningcss: {
targets: browserslistToTargets(browserslist('>= 0.25%, not dead')),
},
},
build: {
cssTarget: ['chrome90', 'firefox88', 'safari14'],
},
});
Nesting just works. If any of your target browsers don't support it, Lightning CSS flattens it into regular selectors:
.card {
padding: 1rem;
& .title {
font-size: 1.25rem;
}
}
Custom media queries let you name a media query once and reuse it, like a variable. Most browsers don't support them yet, so Lightning CSS leaves them off by default. To turn them on, add drafts inside the lightningcss block in your Vite config:
css: {
transformer: 'lightningcss',
lightningcss: {
drafts: { customMedia: true },
},
},
Then you can write:
@custom-media --medium (width >= 768px);
@media (--medium) {
.card {
padding: 2rem;
}
}
Since most browsers don't support @custom-media yet, Lightning CSS replaces --medium with the real query.
When to keep PostCSS
Stick with PostCSS if:
You'll still get Lightning CSS minifying either way. It's the default on Vite 8, and on older versions you can set build.cssMinify: 'lightningcss' and keep PostCSS for everything else.
If your project is plain CSS or CSS Modules, switch.
Walt is a software engineer, startup founder and previous mentor for a coding bootcamp. He has been creating software for the past 20+ years.