What does PostCSS do with a stylesheet, and why does the output stay unchanged until you add plugins such as Autoprefixer?
answer
- a host, not a language
- parse, then print back
- a tree of typed nodes
- Root, AtRule, Rule, Declaration, Comment
- every change comes from a plugin
basics
~20 sPostCSS 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 sPostCSS 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
Recall the three steps (parse, plugins, stringify) and the five node types with one example each. Say plainly that PostCSS changes nothing without plugins.
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.
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.
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.