skip to content

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%

answer

  1. data, not a fixed list
  2. Can I Use support tables
  3. .browserslistrc or the package.json key
  4. defaults when nothing is configured
  5. remove defaults to true

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.

solid answer

~40 s

Autoprefixer is a PostCSS plugin: you write unprefixed CSS, it resolves your target browsers through Browserslist and checks each property, value and selector against Can I Use data. Browserslist takes the `BROWSERSLIST` environment variable if it is set; otherwise it uses the nearest `.browserslistrc` file, `browserslist` key in `package.json` or `browserslist` file, searching upward from the stylesheet's directory; with none of them it uses `defaults`, which is `> 0.5%, last 2 versions, Firefox ESR, not dead`. Named sections such as `production` are picked by `BROWSERSLIST_ENV` or `NODE_ENV`, falling back to `production`. Prefixes are added only where a target needs them, and the `remove` option (default `true`) also strips prefixes no target needs any more. Keep targets in the shared Browserslist config rather than Autoprefixer's `overrideBrowserslist`, so other tools read the same list.

code

json · 8 lines
json
{
  "name": "exhibition-guide",
  "private": true,
  "browserslist": {
    "production": ["defaults", "iOS >= 12"],
    "development": ["last 1 chrome version", "last 1 safari version"]
  }
}

go deeper

for a junior

Remember that Autoprefixer adds vendor prefixes from Can I Use data and that its browser list comes from Browserslist, not from you writing prefixes by hand.

for a middle

Walk through the lookup order for the target list, the defaults query, named environments, and the remove option that deletes outdated prefixes.

for a senior

Diagnose why two builds disagree: nearest config, environment variables, caniuse-lite data in the lockfile, and overrides that split the target list from the other tools that read it.

for a principal

Treat the browser list as a product decision owned in one shared config, with a named reason for every explicit target such as kiosk devices, reviewed when usage data changes.

## What Autoprefixer does, and what it does not **Autoprefixer** is a PostCSS plugin that adds vendor prefixes (`-webkit-`, `-moz-` and so on) to CSS written without them. It decides per property, value, selector and at-rule parameter, using **Can I Use** support data for a list of **target browsers**. Three boundaries are worth stating in an interview: - **It only prefixes.** It does not polyfill new features; lowering newer syntax is postcss-preset-env's job. - **It needs the unprefixed standard form.** If a stylesheet contains only `-webkit-gradient` and never the standard `gradient`, Autoprefixer has nothing to work from and adds nothing. - **It also removes.** With the `remove` option at its default `true`, prefixes that no target browser needs any more are deleted, so old hand-written prefixes disappear from the output. ## Where the target list comes from Autoprefixer does not hold its own browser list; it asks **Browserslist**, which takes the query from one of these sources: 1. The `BROWSERSLIST` environment variable, which wins when it is set (`BROWSERSLIST_CONFIG` can instead point at a config file). 2. Otherwise the nearest config found by walking up from the stylesheet's directory: a `.browserslistrc` file, a `browserslist` key in `package.json`, or a `browserslist` config file. Two of them in the same directory is an error. 3. If none of them produced a valid result: the **`defaults`** query, `> 0.5%, last 2 versions, Firefox ESR, not dead`. Autoprefixer resolves this per processed file, starting from the file's own path, so in a monorepo the config nearest to each stylesheet wins. Its own `overrideBrowserslist` option exists, but the README asks you not to use it: a shared config is also read by other tools in the build, and an override silently splits the target list. A config can hold **named environments** (`production`, `development`, any name). Autoprefixer's `env` option selects one explicitly; otherwise Browserslist picks one by `BROWSERSLIST_ENV`, then `NODE_ENV`, and if neither is set it looks for `production` and then falls back to `defaults`. ## Widening the list for gallery kiosks The exhibition-guide site must keep working on older iPads mounted in the galleries. `defaults` alone may not include them, since it follows usage share and recent versions, so the production query names them explicitly with the `iOS` browser name (Safari on iOS), while development keeps a short list for fast builds. Running `npx browserslist` in the project prints the resolved list, which is the quickest way to confirm the kiosks are in it. ## The options that change output | Option | Default | Effect | |---|---|---| | `add` | `true` | add prefixes the targets need | | `remove` | `true` | delete prefixes no target needs | | `flexbox` | `true` | `false` disables flexbox prefixing; `"no-2009"` adds only the final and IE 10 syntaxes | | `grid` | `false` | `"autoplace"` / `"no-autoplace"` enable IE 10-11 Grid translations | | `supports` | `true` | prefix `@supports` parameters | | `cascade` | `true` | align prefixed lines visually when the CSS is not compressed | | `env` | unset | which Browserslist environment to use | Two escape hatches work inside the CSS: a `/* autoprefixer: off */` control comment disables prefixing for a whole block, and `/* autoprefixer: ignore next */` skips only the next property or selector. A deliberate browser hack is written as a prefixed declaration **after** the unprefixed one. ## Why two projects get different output from the same CSS - **Different queries**: a different `.browserslistrc`, a parent-directory config nobody noticed, or a `BROWSERSLIST` variable in CI. - **Different environment**: `NODE_ENV=production` in one build and `development` in another selects different sections. - **Different data**: Can I Use data ships in the `caniuse-lite` package pinned in each lockfile; usage-based queries such as `> 0.5%` resolve differently on older data. The `update-browserslist-db` tool refreshes it. - **Overrides**: an `overrideBrowserslist` in one config, or a `browsers` option on postcss-preset-env, which passes its list to the Autoprefixer inside it. When output looks wrong, compare `npx browserslist` in both projects before reading any prefix by hand. ## What to say in the interview A middle-level answer has four parts: the data source (Can I Use), the target source (Browserslist and its lookup order), the fallback (`defaults`), and the two directions of change (add what targets need, remove what none of them need). A senior answer adds the diagnosis: which config file was found for this stylesheet, which environment was selected, which data version the lockfile pins, and whether anything overrides the shared list. Everything else follows from those, including the surprise most developers meet first: modern CSS often gets **no** prefixes at all, because no target browser needs one.

  • Two projects run the same Autoprefixer release on the same CSS and get different prefixes. What do you check?
    Run `npx browserslist` in each to see the resolved targets. Then check which config each picked up (a `BROWSERSLIST` variable, or the nearest `.browserslistrc` or `package.json` key, possibly in a parent directory), which environment `NODE_ENV` or `BROWSERSLIST_ENV` selected, whether an `overrideBrowserslist` or preset-env `browsers` option overrides it, and which `caniuse-lite` data each lockfile pins.
  • A stylesheet contains only `-webkit-` prefixed declarations. Why does Autoprefixer add no other prefixes?
    Autoprefixer works from the unprefixed standard property or value; it does not infer the standard form from a prefixed one. Write the standard declaration and let Autoprefixer generate the prefixes, or run a plugin that unprefixes legacy code, such as postcss-unprefix, before Autoprefixer.
  • How do you stop Autoprefixer touching one block of CSS?
    Use a control comment: `/* autoprefixer: off */` inside a block disables prefixing for that whole block, and `/* autoprefixer: ignore next */` skips only the next property, selector or at-rule parameters. Do not put both `off` and `on` in the same block.

  • Autoprefixer adds every known prefix for every browser, just in case.
  • Autoprefixer only adds prefixes; old hand-written ones always stay in the output.
  • Autoprefixer polyfills new CSS features for older browsers.
  • Target browsers belong in Autoprefixer's own options inside postcss.config.js.
  • Without a Browserslist config, Autoprefixer targets every browser ever released.