Sass and preprocessors
What Sass adds on top of CSS, how @use and @forward replace the deprecated @import, and which of its features native CSS has since absorbed.
Sass and preprocessors
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Sass is a language that compiles to plain CSS. Its variables, loops and functions are resolved at build time and leave no trace in the output, which is the main difference from custom properties.
- Sass still offers things CSS does not: `@mixin` with arguments, `@each` and `@for` loops, maps, and functions that run at build time. Native CSS has absorbed variables, nesting and many math and colour functions.
- `@use` loads another file as a namespaced module (`colors.$brand`), and `@forward` re-exports from an index file. They replace `@import`, which is deprecated and scheduled for removal in Dart Sass 3.0.0.
- Use `math.div(a, b)` rather than `a / b` for division, because `/` is now treated as a separator in Sass.
- Choose Sass when you need its build-time logic or have an existing codebase. For new projects, check whether custom properties, nesting and `@layer` already cover your needs.
Why Sass existed
Early CSS had no variables, no nesting, and no way to split a stylesheet without extra network requests. Sass (2006) and later Less and Stylus added those as a language you compile: you write a .scss file, a compiler turns it into .css, and the browser only sees the CSS. The frontend roadmap’s CSS preprocessors and PostCSS lesson tells that story. This lesson focuses on the current form, because CSS has caught up with a lot of it.
SCSS is the syntax that looks like CSS (braces and semicolons). Dart Sass, installed from npm as sass, is the reference implementation. The older node-sass is deprecated.
What Sass adds
I compiled this with Dart Sass 1.108.2 to check:
@use "sass:math";
@use "sass:color";
$brand: #3366cc;
@mixin focus-ring($c: $brand) {
outline: 2px solid $c;
outline-offset: 2px;
}
@function rem($px) { @return math.div($px, 16) * 1rem; }
.card {
padding: rem(24);
border: 1px solid color.adjust($brand, $lightness: 30%);
&__title { font-size: rem(20); }
&--featured { border-color: $brand; }
&:focus-visible { @include focus-ring; }
@media (min-width: 40em) { padding: rem(32); }
}
@each $name, $c in (ok: #2a7, warn: #c80) {
.badge--#{$name} { color: $c; }
}
Output (trimmed): .card { padding: 1.5rem; ... }, .card__title { font-size: 1.25rem; }, .card--featured { border-color: #3366cc; }, .card:focus-visible { outline: 2px solid #3366cc; outline-offset: 2px; }, a @media block with padding: 2rem, and .badge--ok and .badge--warn.
- Variables (
$brand): replaced at compile time. - Mixins (
@mixin,@include): reusable blocks of declarations that take arguments. - Functions (
@function, built-ins likecolor.adjust): run at build time, so they can compute values. - Control flow (
@each,@for,@if): generate repeated rules from data, such as a spacing scale of utility classes. - Interpolation (
#{ }): build selectors and property names from values. - Nesting with
&suffixes:&__titlebecomes.card__title, string-gluing that native CSS nesting does not do. It is the main reason Sass and BEM became associated.
@use and @forward: the module system
@import merged every file into one global scope. A variable defined in one file silently collided with another, and the same file could be processed repeatedly. The module system fixes this.
// _tokens.scss
$brand: #3366cc;
// card.scss
@use "tokens";
.card { border-color: tokens.$brand; }
// with an alias
@use "tokens" as t;
.card { border-color: t.$brand; }
@use makes a file’s members available under a namespace, and only loads it once. @forward re-exports another module, so an _index.scss can gather a folder into one entry point:
// abstracts/_index.scss
@forward "tokens";
@forward "mixins";
Members whose names start with - or _ stay private to their file. @import is deprecated: compiling @import "x" with Dart Sass 1.108 prints a warning that it “will be removed in Dart Sass 3.0.0”. Libraries that still tell you to @import them are behind. The video is devoted to this change.
Division
A slash used to mean divide in Sass. It is now a plain separator, because CSS uses it for things like font: 12px/1.5 and aspect-ratio: 16/9. Use math.div(a, b) after @use "sass:math". In my compile test, width: 10px / 2 came out as 10px/2, not 5px.
What native CSS has taken over
| Sass feature | Native CSS today |
|---|---|
| Variables | Custom properties, which also change at runtime |
| Nesting | CSS nesting, without & suffix gluing |
math.div, min(), max() |
calc(), min(), max(), clamp() |
color.adjust |
color-mix(), relative colour syntax |
@import partials |
@import (render-blocking) or a bundler |
Not yet covered natively in a way you can rely on: @mixin with arguments, loops, and maps. CSS functions (@function) are in Chrome 139 and Safari 27.3 but not Firefox per MDN’s compatibility data, so are not ready for general use.
Do you need it?
Reasonable reasons to use Sass: an existing codebase, a design system that generates utilities from a map, or a team that already knows it. For a new small project, native features plus a bundler might be enough. Sass has a cost: a compile step, and one more place a value can be defined. A useful test is whether your Sass does anything that custom properties and nesting could not. If not, removing it reduces what a new teammate has to learn.
Sass is a transformation of text. PostCSS and Lightning CSS, next, are the other kind of tool: they work on CSS itself.
What the video covers
Kevin Powell’s “Stop using @import with Sass | @use and @forward explained” (February 2021, about 13 minutes) is, from its title, about the change from @import to @use and @forward. It is from 2021, so any version numbers or removal timelines it mentions may have moved on. It is a module-system video, not a from-scratch Sass introduction, so the language basics above carry that part. I could not read a transcript. Fireship’s two-minute overview is in further viewing.
Where this fits
Sass is the build-time end of the spectrum that CSS architecture works on, and nesting is the clearest case of CSS adopting a Sass feature.
Check yourself
A Sass variable `$brand` and a custom property `--brand` both hold a colour. What can the custom property do that the Sass variable cannot?
It exists in the browser, so it can change per element, inside a media query or via a class (for example a dark theme), and be read or set from JavaScript. It also inherits. A Sass variable is replaced during compilation and the output CSS contains only the final value.
What does `@use "config" as c;` followed by `c.$brand` do, and why is it better than `@import`?
It loads `config` as a module and makes its members available under the namespace `c`. Names do not leak into the global scope, so two files can both define `$padding` without clashing, and a file is loaded once however many times it is used. `@import` put everything in one global scope and re-evaluated files each time.
Why does `width: 10px / 2` not compile to `5px` in current Sass?
A bare slash is now a separator (it keeps `font: 12px/1.5` valid), not division. The deprecation warning points to `math.div(10px, 2)` with `@use "sass:math"`. I compiled `width: 10px / 2` with Dart Sass 1.108 and it came out as `10px/2`.
What would you still reach for Sass for in a project that uses native nesting and custom properties?
Build-time logic: a loop that generates a set of utility classes from a map, a mixin that emits a long block of declarations with arguments, or functions that compute values. Native CSS functions and mixins are arriving, but have far less support so far.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.