PostCSS and Lightning CSS

Intermediate · 15 min

How PostCSS and Lightning CSS transform standard CSS at build time (prefixing, lowering syntax, minifying, bundling) and how browserslist shapes the output.

Before thisSass and preprocessorsCSS nesting

Web Dev Simplified · 15 min

PostCSS and Lightning CSS

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Web Dev SimplifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • PostCSS and Lightning CSS both transform real CSS into other real CSS, unlike Sass, which compiles a different language. They are used to make modern syntax work in the browsers you target.
  • PostCSS is a small core that parses CSS into a tree and runs plugins over it. Autoprefixer, `postcss-preset-env` and `cssnano` are the best known.
  • Lightning CSS is a single Rust tool that parses, transforms, prefixes, minifies and bundles CSS, driven by a `targets` setting derived from browserslist.
  • Both read the browser targets you give them, so the output depends on your target list. A wider list produces more fallbacks and bigger files.
  • They lower syntax, not behaviour. Nesting, media range queries and `oklch()` colours can be rewritten for older browsers, but a feature such as anchor positioning cannot.

A compiler for the CSS you already write

Sass asks you to learn a different language. A second kind of tool leaves your CSS as CSS and rewrites it for the browsers you care about. You write modern syntax: nesting, oklch() colours, media range queries. The tool translates it for whatever your target list requires, and minifies the result.

The frontend roadmap’s CSS preprocessors and PostCSS lesson introduces PostCSS and autoprefixer. This lesson covers the current picture, including Lightning CSS.

PostCSS: a parser and a plugin host

PostCSS on its own does nothing useful. It parses CSS into a tree (an AST), hands it to each plugin in turn, and prints the result. Everything interesting is a plugin:

  • Autoprefixer adds vendor prefixes (-webkit-) where your targets need them.
  • postcss-preset-env lets you write newer CSS and compiles it down, choosing transforms from your browser targets.
  • cssnano minifies.
  • Plugins you write, since a plugin is a small function over the tree.
// postcss.config.mjs
import presetEnv from 'postcss-preset-env';

export default {
  plugins: [presetEnv({ stage: 2, browsers: 'safari 14, chrome 90' })],
};

I ran this with PostCSS 8.5 and postcss-preset-env 11 on a small file with nesting, an oklch() colour and a media range query:

.card {
  color: oklch(60% 0.15 250);
  & .title { font-size: 1.25rem; }
  &:hover { translate: 0 -2px; }
  @media (width >= 40em) { padding: 2rem; }
}

The output had the nesting flattened (.card .title { ... }, .card:hover { ... }), the colour converted to an rgb() value, and the media query written as (min-width: 40em).

Lightning CSS: one tool, written in Rust

Lightning CSS (lightningcss on npm) does parsing, transforming, vendor prefixing, minifying and bundling in one program. It is much faster than a JavaScript plugin chain because it is native code, and it makes decisions from the same sort of browser target list.

import { transform } from 'lightningcss';

const { code } = transform({
  filename: 'card.css',
  code: Buffer.from(source),
  minify: true,
  targets: { chrome: 90 << 16, safari: 14 << 16 },  // major << 16
});

Targets are written as major << 16 | minor << 8 | patch, and the package ships a helper, browserslistToTargets, to convert a browserslist query so you do not hand-encode them.

I ran the earlier file through it with those targets. Nesting was flattened, and the media range became (min-width: 40em). The oklch() colour was emitted three times in a row, from oldest to newest: an sRGB hex, then color(display-p3 ...), then lab(...). A browser reads them top to bottom and uses the last one it understands, which is the cascade-fallback idea from the supports lesson.

Lightning CSS also supports CSS Modules (covered in a later lesson) and bundling @imports.

Where they run

You rarely call them by hand.

  • Vite uses PostCSS as its default CSS transformer. You can switch with css: { transformer: 'lightningcss' } and build: { cssMinify: 'lightningcss' }. I confirmed both option names in Vite’s type definitions.
  • Tailwind CSS v4 uses Lightning CSS internally for tasks such as vendor prefixing and minification. It still ships a PostCSS plugin and a Vite plugin as ways to plug it into a build.
  • Parcel, from the same author as Lightning CSS, uses it for CSS.
  • Webpack projects typically use postcss-loader.

The module bundlers and build tools lesson in the frontend roadmap shows where these steps sit in a build.

Browser targets

Both tools rely on a list of the browsers you support, usually written in browserslist syntax (>= 0.5%, last 2 versions, not dead) in package.json or .browserslistrc. The tool compares each feature you use with that list. That makes the target list a performance dial: supporting older browsers adds fallbacks and lowering, which makes files bigger and slower to parse.

Pick targets from your real audience. The default Tailwind v4 baseline, for example, assumes recent browsers and says so in its documentation, and a project that needs more has to plan for it.

Limits

They change syntax, not capabilities. They can unnest, rewrite media queries, prefix, add colour fallbacks, and inline @imports. They cannot give a browser a layout engine feature. Anchor positioning, scroll-driven animations and container queries need a runtime polyfill if you need them in browsers that lack them, or a fallback design.

Which to use

  • Starting a new Vite project and want fewer moving parts: Lightning CSS, if what it supports covers your needs.
  • You need a PostCSS-only plugin (a Tailwind v3 plugin, or your own transform): stay with PostCSS.
  • You use Sass: Sass compiles first, and either tool processes the CSS afterwards.

What the video covers

Web Dev Simplified’s “Learn PostCSS In 15 Minutes” (December 2022, about 15 minutes) is, from its title, an introduction to PostCSS. I could not read a transcript, and the description lists no timestamps, so its specific examples are not described here. It predates the mainstream use of Lightning CSS, so all of the Lightning CSS material above comes from my own tests and the package documentation, not from a video. I did not find a good English video on Lightning CSS, so that part has no video backing. Fireship’s two-minute PostCSS overview is in further viewing.

Where this fits

These tools are what let you use native nesting today without waiting for everyone’s browser to update. They also underpin Tailwind, next.

Check yourself

  1. 01What is the difference between Sass and PostCSS in terms of input?

    Sass takes `.scss`, its own superset language, and outputs CSS. PostCSS takes ordinary CSS (plus whatever syntax your plugins understand), parses it to a tree, runs plugins that edit the tree, and prints CSS again. You can run them together: Sass first, PostCSS on the result.

  2. 02Your browserslist says `safari 14, chrome 90`. What happens to a native nested rule and a `@media (width >= 40em)` query when processed?

    Both are rewritten to older syntax. In my test of Lightning CSS and `postcss-preset-env` with those targets, nesting was flattened to `.card .title { ... }` and `(width >= 40em)` became `(min-width: 40em)`. With newer targets they would be left alone.

  3. 03Why can't a build tool make `position-area` work in a browser that does not support it?

    Transforms rewrite one piece of syntax into an equivalent older piece. Anchor positioning needs a layout engine feature with no old-syntax equivalent. At best a JavaScript polyfill can emulate it at runtime, at a cost in size and accuracy.

  4. 04When would you pick Lightning CSS over a PostCSS pipeline?

    When you want one fast tool that covers parsing, syntax lowering, prefixing, minification and CSS Modules without assembling plugins, and your needs are covered by what it supports. PostCSS is the better choice when you need a specific plugin ecosystem, such as Tailwind v3 plugins or custom transforms.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark