skip to content

PostCSS

PostCSS parses CSS into an AST and runs plugins such as Autoprefixer and postcss-preset-env over it, aimed by a Browserslist query. Interviewers ask what each plugin changes and why order matters.

on this pageshow

explore

questions

6

What does PostCSS do with a stylesheet, and why does the output stay unchanged until you add plugins such as Autoprefixer?

level: juniorimportance: must knowfreq 22%

answer

  1. a host, not a language
  2. parse, then print back
  3. a tree of typed nodes
  4. Root, AtRule, Rule, Declaration, Comment
  5. every change comes from a plugin

basics

~20 s

PostCSS parses CSS into a tree of nodes (Root, AtRule, Rule, Declaration, Comment), lets plugins read and rewrite that tree, then prints it back as CSS. PostCSS itself changes nothing: prefixes, lowering and minification all come from plugins.

solid answer

~40 s

PostCSS is a tool for transforming CSS with JavaScript plugins, not a preprocessor with its own language. It parses a stylesheet into an abstract syntax tree: a `Root` for the file, `AtRule` nodes for statements such as `@media` or `@import`, `Rule` nodes for a selector and its block, `Declaration` nodes for `property: value` pairs, and `Comment` nodes. Plugins walk that tree and change it, and a stringifier prints the tree back as CSS. With an empty plugin list the CSS comes out as written. So everything a pipeline does is decided by its plugins: Autoprefixer adds vendor prefixes from Can I Use data, postcss-preset-env lowers newer syntax for older browsers, cssnano minifies, and the CSS Modules transform rewrites class names.

go deeper

for a junior

Recall the three steps (parse, plugins, stringify) and the five node types with one example each. Say plainly that PostCSS changes nothing without plugins.

for a middle

Explain how plugins work on the tree, what each everyday plugin changes, and why the output depends on plugin order and on the Browserslist targets those plugins read.

for a senior

Show you can debug a pipeline that seems to do nothing: which config file was loaded, which plugins ran, which targets they resolved, and how custom syntaxes such as postcss-scss differ from compiling.

for a principal

Frame PostCSS as a platform choice: one parser shared by prefixing, lowering, minifying and linting, against the cost of owning a plugin list and keeping its targets consistent across tools.

## What PostCSS is, and what it is not **PostCSS is a tool for transforming CSS with JavaScript plugins.** It is not a language and not a preprocessor in the way Sass is: it defines no variables, mixins or functions of its own, and any valid CSS can go through it. What it provides is a framework (a parser, a tree API and a stringifier) on which plugins are built. Autoprefixer, the postcss-preset-env plugin pack, the cssnano minifier, the CSS Modules transform and many linters are all PostCSS plugins, which is why PostCSS often sits in a project's `node_modules` even when nobody chose it directly. The practical consequence is simple and is what interviewers check first: **PostCSS on its own changes nothing.** Every visible effect in the output, from a `-webkit-` prefix to a minified file, was put there by a specific plugin, aimed by its own options. ## The pipeline: parse, transform, stringify 1. **Tokenize and parse.** PostCSS splits the source string into tokens (words, brackets, colons, comments) and builds an **abstract syntax tree (AST)** from them. Keeping tokenizing separate from tree-building lets the slow character-by-character step stay fast and the tree-building step stay readable. 2. **Run plugins.** Each plugin receives the tree and reads or changes nodes through a DOM-like API: move to a parent or a sibling, insert a node, clone one, remove one, change a property. 3. **Stringify.** A stringifier turns the tree back into CSS text, plus a source map when one is requested. With an **empty plugin list**, step 2 has nothing to do and `process()` hands the CSS back as written. When someone reports that "PostCSS did nothing", the first question is which plugins actually ran. ## The node types | Node | Represents | Example source | Useful fields | |---|---|---|---| | `Root` | the whole file | the stylesheet itself | `nodes` | | `AtRule` | a statement starting with `@` | `@media (screen) { }`, `@import "hall.css";` | `name`, `params` | | `Rule` | a selector with its block | `input, button { }` | `selector` | | `Declaration` | a property and its value | `color: black` | `prop`, `value`, `important` | | `Comment` | a stand-alone comment | `/* gallery map */` | `text` | Two details matter when you read or write a plugin: - Comments that sit **inside** a selector, an at-rule's parameters or a value are not `Comment` nodes. They are kept in the owning node's `raws`, together with the original whitespace, so untouched parts of the file keep their formatting. - A `Declaration` whose property starts with `--` is a custom property, and PostCSS marks it with the `variable` flag, so a plugin can tell `--gutter: 24px` apart from `margin: 24px`. ## Plugins do all the work - **Autoprefixer** adds the vendor prefixes your target browsers need, and removes outdated ones, using Can I Use data for the browsers your Browserslist configuration selects. - **postcss-preset-env** lowers newer standard CSS (nesting, newer colour syntax, custom media queries) into forms older targets understand, and runs Autoprefixer inside itself. - **postcss-import** inlines `@import`ed files so that later plugins see one tree. - **postcss-nesting** flattens nesting written to the CSS Nesting specification; **postcss-nested** unwraps Sass-style nesting instead. - **cssnano** minifies. - The **CSS Modules transform** (postcss-modules and its companion plugins) rewrites class names per file; the scoping rules themselves belong to the CSS Modules material. Because every change is a plugin, **the order of the plugins and the browser targets they read decide the output**. Those are the two follow-up topics an interviewer usually moves to. ## Other syntaxes The parser and the stringifier are replaceable. A custom syntax such as `postcss-scss` lets plugins read and rewrite SCSS source, which is how linters and formatters can work on SCSS files, but it **does not compile** SCSS to CSS. Sass still compiles; PostCSS then usually runs on the CSS that Sass emits. Other custom syntaxes parse styles inside HTML `<style>` tags or Markdown code blocks. ## When PostCSS "did nothing" - **Was a config found?** A `postcss.config.js` in the project root, a `postcss` section in `package.json`, or the PostCSS options inside the build tool's own configuration. - **Is the plugin list non-empty, and are the plugins installed?** A plugin that is listed but never loaded cannot transform anything. - **Are the targets what you think?** Autoprefixer emitting no prefix for `border-radius` is correct behaviour: no current browser needs one. A junior who can say "PostCSS parses, plugins transform, the stringifier prints" and name the node types already answers the screening question; the rest of this topic is about which plugin does what, in which order, for which browsers.

  • Can PostCSS process SCSS files?
    It can parse them with a custom syntax such as `postcss-scss`, which lets plugins like linters read and rewrite SCSS source. It does not compile SCSS: variables, mixins and `@use` are still Sass's job. In a typical build Sass compiles first and PostCSS runs on the CSS that Sass emits.
  • Where does PostCSS keep a comment written inside a selector or a value?
    Only stand-alone comments become `Comment` nodes. A comment inside a selector, an at-rule's parameters or a declaration's value is stored in that node's `raws`, along with the original whitespace, so the output keeps the source formatting of nodes no plugin changed.

  • PostCSS is a preprocessor with its own syntax, like Sass.
  • Installing PostCSS adds vendor prefixes by itself.
  • PostCSS edits the CSS text with regular expressions, not a parsed tree.
  • Every comment in the file becomes a Comment node, including ones inside values.
  • Adding postcss-scss makes PostCSS compile SCSS into CSS.
open as a page

How does Autoprefixer decide which vendor prefixes to add or remove, and where does it read its target browsers from?

level: middleimportance: must knowfreq 16%

basics

~20 s

Autoprefixer checks each unprefixed property, value and selector against Can I Use data for the browsers a Browserslist query selects, from .browserslistrc or package.json, else defaults. It adds only the prefixes those browsers need and removes outdated ones.

open as a page

In a postcss.config.js listing postcss-import, postcss-preset-env and cssnano, why does plugin order matter, and which order do you use?

level: middleimportance: should knowfreq 12%

basics

~20 s

Plugins run in the order listed, each on the tree the previous ones left. Put postcss-import first so later plugins see one tree, non-standard plugins before postcss-preset-env (which already runs Autoprefixer), and cssnano last so it compresses everything the others emitted.

open as a page

In postcss-preset-env, what do the stage and features options control, and why does it leave some modern CSS untransformed?

level: middleimportance: should knowfreq 8%

basics

~20 s

postcss-preset-env picks features by spec maturity (stage, 0 to 4 or false, default 2) and per-feature overrides (features), then lowers a feature only if a Browserslist target lacks support. Syntax every target supports passes through untouched.

open as a page

When would you replace a PostCSS pipeline of postcss-preset-env and cssnano with Lightning CSS, and what does the switch give up?

level: seniorimportance: nice to knowfreq 4%

basics

~20 s

Lightning CSS is a single Rust-based transformer that lowers modern syntax for browser targets, prefixes and minifies in one fast tool. Switch when the pipeline is only standard lowering and minifying; stay on PostCSS while you depend on PostCSS plugins.

open as a page

When writing a PostCSS 8 plugin that rewrites a legacy --gutter-units declaration, which listeners do you use, and how do you avoid an endless re-visit loop?

level: seniorimportance: nice to knowfreq 5%

basics

~20 s

Export a creator, flagged postcss = true, returning postcssPlugin and listeners. Use a Declaration listener keyed to --gutter-units, prepare(result) for per-file state, and make each change stop matching, since PostCSS 8 re-visits every changed node.

open as a page