What is a CSS preprocessor?
A CSS preprocessor is a tool that lets you write styles in an extended version of CSS, with extra features such as variables, nesting, functions and loops, and then converts (compiles) that code into ordinary CSS that browsers understand.
Browsers never see the preprocessor code itself. The process looks like this:
- You write your styles in a file such as
styles.scss(for Sass). - A compiler (a program that converts code from one form to another) reads that file.
- It outputs a regular
styles.cssfile, which you link to from your pages.
Here's a tiny example. This Sass code:
$primary-color: #1d4ed8;
.button {
background-color: $primary-color;
}
compiles to this plain CSS:
.button {
background-color: #1d4ed8;
}
Preprocessors became hugely popular because CSS once lacked many of these features. The interesting twist is that CSS has since caught up in many areas. In this post we'll look at each preprocessor feature alongside its modern, native CSS equivalent (where one exists), so you can decide whether you need a preprocessor at all.
Tip: the code examples use Sass's SCSS syntax, which looks just like CSS with extras, as it's by far the most widely used preprocessor today.
Variables
Variables let you store a value, such as a colour, once and reuse it everywhere. If you change the variable, every place that uses it updates.
Sass variables
$primary-color: #1d4ed8;
.button {
background-color: $primary-color;
}
.link {
color: $primary-color;
}
Native CSS custom properties
CSS now has its own variables, called custom properties. They start with two dashes, and you read them with var():
:root {
--brand: #1d4ed8;
}
.button {
background-color: var(--brand);
}
/* Any section can override the value for everything inside it */
.promo {
--brand: #be185d;
}
Default: --brand: #1d4ed8
Inside a section that sets --brand: #be185d
The buttons above use exactly the same code. The second pair simply sits inside an element that sets a different --brand value.
The key difference: Sass variables only exist while compiling. Once the CSS is produced, they're gone and replaced with fixed values. CSS custom properties are live in the browser: they can change inside a media query, for a dark mode theme, for one section of a page, or from JavaScript. For most projects, custom properties are now the better choice.
Nesting
Nesting lets you write selectors inside other selectors, mirroring the structure of your HTML and keeping related styles together.
Sass nesting
.navbar {
background-color: #1f2937;
a {
color: #ffffff;
&:hover {
text-decoration: underline;
}
}
}
The & symbol means "the current selector", so &:hover becomes .navbar a:hover.
Native CSS nesting
The exact same code now works in plain CSS. Native nesting has been supported in all major browsers since late 2023, with no compiler needed. The navigation bar below is styled with native nesting (hover over the links, or press Tab to move through them):
nav {
display: flex;
gap: 4px;
background: #1f2937;
a {
padding: 8px 14px;
color: #ffffff;
&:hover,
&:focus-visible {
background: #374151;
text-decoration: underline;
}
}
}
One difference: Sass can join the & onto text to build new class names. For example, .card { &__title { … } } creates .card__title. Native CSS nesting can't do this, because & always refers to the whole selector. If your project relies on this pattern (common with the BEM naming convention), Sass still helps.
Tip: avoid nesting more than two or three levels deep. Deep nesting creates long, very specific selectors that are hard to override, whether you're using Sass or native CSS.
Splitting styles into files
Large stylesheets are easier to manage when they're split into smaller files, such as one for colours, one for buttons and one for the layout.
Sass modules with @use
In Sass, files that start with an underscore are partials: pieces meant to be loaded into another file rather than compiled on their own. You load them with @use:
// _colors.scss
$primary-color: #1d4ed8;
// styles.scss
@use "colors";
.button {
background-color: colors.$primary-color;
}
Everything from the loaded file is accessed through a namespace (here, colors.), which prevents clashes between files that happen to use the same variable names. When you compile styles.scss, all the partials are combined into a single CSS file.
Out of date: older tutorials use Sass's @import rule for this. It was officially deprecated in Dart Sass 1.80.0 and is due to be removed in Dart Sass 3.0, so use @use (and @forward, for passing a file's contents on) in new code.
Native CSS
Plain CSS has its own @import rule, but each imported file is a separate download that the browser only discovers after loading the first stylesheet, which can slow pages down. Combining files into one is still the job of a build tool, whether that's a preprocessor or a bundler (a tool that combines your files for the live site), such as Vite.
Functions and mixins
Functions
A function takes some values, works something out and returns a result. Here's a Sass function that converts pixels to rem units:
@use "sass:math";
@function px-to-rem($px, $base: 16px) {
@return math.div($px, $base) * 1rem;
}
.title {
font-size: px-to-rem(24px); // compiles to 1.5rem
}
Native CSS now has plenty of built-in maths and colour functions, such as calc(), min(), max(), clamp() and color-mix(). Unlike Sass functions, they work out their results in the browser, so they can mix units and use custom properties. For example, here's a full colour palette created from a single --brand value with color-mix():
.swatch-light { background: color-mix(in oklch, var(--brand), white 30%); }
.swatch { background: var(--brand); }
.swatch-dark { background: color-mix(in oklch, var(--brand), black 25%); }
Change --brand, and every shade updates in the browser. With Sass colour functions, you'd need to recompile.
CSS is also gaining custom functions with its own @function rule. Here's the pixels-to-rem converter in native CSS:
@function --px-to-rem(--px) {
result: calc(var(--px) / 16 * 1rem);
}
.title {
font-size: --px-to-rem(24); /* 1.5rem */
}
At the time of writing, native custom functions work in Chrome and Edge (version 139 onwards) but not yet in Firefox or Safari, so they're one to watch rather than one to rely on.
Mixins
A mixin is a reusable group of declarations that you can include wherever you need them, optionally with settings:
@mixin focus-ring($color: #1d4ed8) {
outline: 3px solid $color;
outline-offset: 2px;
}
.button:focus-visible {
@include focus-ring;
}
.danger-button:focus-visible {
@include focus-ring(#b91c1c);
}
Mixins are one feature plain CSS doesn't have yet. Native CSS mixins have been proposed, but they aren't available in browsers.
Tip: mixins were often used to add vendor prefixes (browser-specific versions of properties, such as -webkit-box-shadow). You don't need to write these by hand any more: properties like box-shadow haven't needed prefixes for years, and a tool called Autoprefixer can add any that are still required automatically.
Loops and maps
This is where preprocessors still clearly win. Sass can store lists of values in maps (sets of names and values) and loop over them to generate CSS:
$spacing: (
small: 4px,
medium: 8px,
large: 16px,
);
@each $name, $size in $spacing {
.gap-#{$name} {
gap: $size;
}
}
This compiles to:
.gap-small {
gap: 4px;
}
.gap-medium {
gap: 8px;
}
.gap-large {
gap: 16px;
}
The #{…} part is called interpolation: it inserts a value into a selector or property name. Sass also has @for and @while loops and @if conditions. Plain CSS has no loops, so generating sets of classes like this is still a strong reason to use a preprocessor.
Popular preprocessors today
Sass
Sass (Syntactically Awesome Style Sheets) is the most widely used and actively developed preprocessor. It has two syntaxes: SCSS (files ending in .scss), which uses braces and semicolons like CSS, and the older indented syntax (files ending in .sass), which uses indentation instead. Almost everyone uses SCSS.
Good to know: the official version is Dart Sass, installed from npm as the sass package. Older versions called LibSass and node-sass are deprecated and no longer receive new features, so if an old project uses node-sass, it's worth switching.
Less
Less has a syntax very close to CSS, with variables that start with @. It was famously used by Bootstrap 3, though Bootstrap moved to Sass from version 4. It's still maintained, but far less common in new projects.
@primary-color: #1d4ed8;
.button {
background-color: @primary-color;
}
Stylus
Stylus has the most minimal syntax, letting you leave out braces, colons and semicolons. It sees little use today, so it's best kept to existing projects.
primary-color = #1d4ed8
.button
background-color primary-color
What about PostCSS?
PostCSS is often mentioned alongside preprocessors, but it works differently: it's a tool that runs plugins over CSS. Popular plugins include Autoprefixer and postcss-preset-env, which lets you write modern CSS and converts it for older browsers. Many build setups use PostCSS, sometimes together with Sass.
Getting started with Sass
If you have Node.js installed, you can add Sass to a project and compile a file from the command line:
npm install --save-dev sass
npx sass styles.scss styles.css --watch
The --watch option recompiles automatically every time you save. If you use a build tool such as Vite, installing the sass package is usually all you need, and you can import .scss files directly.
To learn more, visit the official sites for Sass, Less and Stylus.
Do you still need one?
Here's how plain CSS compares with Sass today:
| Feature | Plain CSS today | Sass |
|---|---|---|
| Variables | Yes: custom properties, live in the browser | Yes: fixed when compiled |
| Nesting | Yes, in all major browsers | Yes, plus building class names with & |
| Maths and colour | Yes: calc(), clamp(), color-mix() and more | Yes: the sass:math and sass:color modules |
| Custom functions | Chrome and Edge only, so far | Yes |
| Mixins | Not yet | Yes |
| Loops, maps and conditions | No | Yes |
| Combining files | Needs a bundler | Yes, with @use |
| Build step needed | No | Yes |
For small and medium sites, modern CSS covers most of what people once used preprocessors for, with no build step. Custom properties and native nesting alone handle the majority of everyday needs.
A preprocessor still earns its place when you need mixins, loops to generate many classes, or a large design system split across many files, or when you're working on an existing project or framework (such as Bootstrap) that's built with Sass.
Wrapping up
CSS preprocessors transformed how we wrote stylesheets, and many of their best ideas have now been built into CSS itself. Learn the native features first: custom properties, nesting and functions such as color-mix(). Then reach for Sass when a project needs the extra power of mixins, loops and modules.