CSS Preprocessors & PostCSS

Intermediate · 28 min

Why Sass added variables, nesting, and mixins before CSS had them natively, and how PostCSS transforms CSS at build time to patch cross-browser gaps.

Before thisCSS Architecture & Naming Conventions

Kevin Powell · 28 min

CSS Preprocessors & PostCSS

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

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A preprocessor like Sass compiles an extended CSS-like language down to plain CSS at build time, adding features the browser itself can't run.
  • Sass nesting lets you write selectors that mirror your HTML's structure, compiling down to the flat selectors a browser actually needs.
  • Sass mixins package up a reusable block of declarations (optionally parameterized) so you're not retyping the same group of properties across rules.
  • Partials and @use let you split a stylesheet into multiple files that compile into one output file, which is how large stylesheets stay organized.
  • PostCSS isn't a language — it's a tool that parses real CSS into an AST and runs plugins over it, and autoprefixer is the canonical plugin, adding vendor prefixes based on a browser support target.
  • Sass and PostCSS solve different problems and are commonly used together: Sass for authoring convenience, PostCSS (via autoprefixer) for compatibility after the fact.

CSS used to ship features slowly, so people built around it

For a long time, plain CSS had no variables, no nesting, no way to define a reusable chunk of styles with parameters. If you used the same color in 40 places and needed to change it, you edited it in 40 places. If you wanted to express “this rule only applies inside .card,” you wrote out .card .title by hand instead of nesting .title visually inside a .card block. These weren’t hypothetical annoyances — they were the daily reality of writing CSS at any real scale, and the CSS standards process moves slowly by design, so the ecosystem didn’t wait. Preprocessors were built to add these missing conveniences as an extended language that compiles down to plain CSS before it ever reaches a browser. Sass (and its sibling syntax, SCSS) became the dominant one.

It’s worth being precise about what a preprocessor is: it’s not a browser feature, and the browser never executes Sass. A build step reads your .scss files, resolves every variable, nested selector, and mixin, and writes out ordinary .css — the exact same kind of file you’d have written by hand, just generated instead of typed.

Variables: a value defined once, used everywhere

$brand-color: #3b5bdb;
$spacing-unit: 8px;

.button {
  background-color: $brand-color;
  padding: $spacing-unit * 2;
}

This looks almost identical to a CSS custom property, and that’s not a coincidence — custom properties were partly inspired by what Sass variables already offered. The difference worth remembering: a Sass variable is resolved once, at compile time, and the output CSS contains the literal value with no trace of the variable left. It can’t be read or changed by JavaScript, and it can’t vary at runtime the way a CSS custom property can. Sass variables are for organizing your source code; CSS custom properties are for values that need to be live in the browser.

Nesting: write selectors the way you think about structure

.card {
  padding: 16px;

  &__title {
    font-size: 1.25rem;
  }

  &:hover {
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  }
}

compiles to:

.card { padding: 16px; }
.card__title { font-size: 1.25rem; }
.card:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.1); }

The & symbol refers back to the parent selector, which is how &__title produces .card__title — a handy way to pair Sass nesting with a BEM naming convention. Nesting is genuinely convenient for mirroring your HTML’s structure visually in your CSS, but it comes with a real trap: each level of nesting adds another selector to the compiled output, and that increases specificity exactly the way writing those selectors by hand would. Nesting five levels deep produces a five-selector-deep compiled rule, with all the specificity problems you’d expect from that — the convenience of nesting doesn’t exempt you from the cascade’s rules, it just hides the cost until you look at the compiled output.

Mixins: reusable blocks, not just reusable values

A variable holds one value. A mixin holds a reusable block of declarations, and can take parameters:

@mixin flex-center($direction: row) {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: $direction;
}

.toolbar {
  @include flex-center;
}

.modal {
  @include flex-center(column);
}

This is the kind of repetition a mixin is built for — the same pattern of three or four properties showing up across many unrelated components, sometimes with a small variation. Without a mixin, you’d either retype the block everywhere (and risk them drifting out of sync when one gets updated) or create an artificial shared class purely for styling convenience, which tends to produce HTML that’s organized around CSS implementation details rather than meaning.

Partials and @use: splitting a stylesheet without splitting the output

Once a project has more than a trivial amount of CSS, one giant file becomes unmanageable. Sass solves this with partials — files prefixed with an underscore, like _buttons.scss — that get pulled into a main file with @use:

// _variables.scss
$brand-color: #3b5bdb;

// main.scss
@use 'variables';

.button {
  background-color: variables.$brand-color;
}

Despite being split across files for organization, they all compile down into a single output CSS file (or a small number of them), so there’s no runtime cost to splitting things up this way — it’s purely an authoring convenience, resolved entirely at build time.

PostCSS: not a language, a pipeline

It’s easy to assume PostCSS is “another Sass,” but it’s a fundamentally different kind of tool. Sass is a language with its own syntax that compiles into CSS. PostCSS takes CSS you’ve already written — real, valid CSS — and parses it into an abstract syntax tree (a structured, in-memory representation of the CSS), runs a chain of plugins over that tree, and outputs transformed CSS. PostCSS itself does nothing; its entire value comes from the plugins you plug into it, which is also why you’ll see it described as a build tool rather than a language.

The single most common PostCSS plugin, and the one that explains why PostCSS became near-universal in modern build pipelines, is autoprefixer. Different browsers have historically required vendor-specific prefixes (-webkit-, -moz-, and others) to support certain CSS properties before those properties became fully standardized. Writing all of those by hand for every property that needs them is tedious and easy to get stale as browser support shifts over time. Autoprefixer reads a target browser support list — typically from a browserslist configuration shared across your tooling — and automatically inserts exactly the prefixes still required for that target, nothing more:

/* you write */
.box {
  display: flex;
}

/* autoprefixer outputs, for an older target */
.box {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

Because the prefix list is generated from a support target rather than hardcoded, updating your browserslist config and rebuilding automatically drops prefixes for browsers you’ve stopped supporting, without you touching a single CSS rule.

Using both together

Sass and PostCSS aren’t competitors — most real projects that use Sass also run PostCSS (with autoprefixer, at minimum) as a later step in the same build pipeline. Sass handles authoring convenience: variables, nesting, mixins, splitting files. PostCSS handles what happens to the output of that process: adding compatibility prefixes, and often other plugin-driven transforms like minification. Thinking of them as two stages in one pipeline — write expressive Sass, compile it down, then run the compiled CSS through PostCSS for compatibility — is the accurate mental model, and it’s exactly how most production front-end build tools are wired up by default.

Check yourself

  1. 01What does a Sass file actually become before it reaches the browser?

    Plain CSS. A compiler (the Sass processor) reads the .scss file, resolves its variables, nesting, mixins, and imports, and outputs an ordinary .css file — the browser never sees Sass syntax directly.

  2. 02Why does Sass nesting need to be used carefully instead of just mirroring every level of your HTML?

    Each level of nesting adds another selector to the compiled output, which increases specificity the same way writing those selectors out by hand would. Nesting too deeply produces the same specificity problems as hand-written over-specific selectors, just generated automatically instead of typed out.

  3. 03What's the difference between a Sass mixin and a Sass variable?

    A variable holds a single value, like a color or a spacing number. A mixin holds a reusable block of one or more declarations — an entire chunk of CSS — and can take parameters, so it's for reusing patterns of styles, not single values.

  4. 04What does autoprefixer actually do, and where does it get its information about which prefixes to add?

    It scans your compiled CSS for properties that need vendor prefixes in some browsers and adds those prefixes automatically. It decides which prefixes are still necessary based on a browser support target you configure (commonly a browserslist config), rather than adding every prefix that has ever existed.

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