Skip to main content
Quick Reference

Sass Cheatsheet

Sass on one page — partials, @use and @forward, variables, nesting, mixins, functions, maps, @extend, and the line between a Sass variable and a CSS variable.

10 sections 10 tables 14 code snippets

Install & compile

Dart Sass

npm install --save-dev sass
npx sass src/styles/main.scss dist/main.css
npx sass --watch src/styles/main.scss:dist/main.css

The entry file

// src/styles/main.scss
@use "theme";

body {
  color: theme.$text;
  background: theme.$surface;
}

What the compiler sees

FileRole
main.scssThe entry. This is the file you pass to the compiler
_theme.scssA partial. The leading _ means it is not a separate CSS file
@use "theme"Loads _theme.scss. Leave the underscore and the extension off
theme.$textA member of that module. The namespace is the file name
sass src:distCompiles every file in the folder that is not a partial

This page is SCSS, the CSS-shaped Sass syntax, compiled with Dart Sass. The npm package is sass. node-sass is the old LibSass binding and does not understand @use.

@use & @forward

Load once, under a name

@use "theme" as t;
@use "sass:map";

.card {
  color: t.$text;
  padding: map.get(t.$space, "md");
}

A folder barrel

// components/_index.scss
@forward "stack";

// main.scss
@use "components";

.page {
  @include components.stack;
}

Module rules

RuleEffect
@use is onceA second @use of the same file does not load it again
@use is firstOnly comments and other @use / @forward rules may sit above it
as tRenames the namespace. as * drops it and is the one to avoid
@forwardRe-exports members to whoever loads this file. It does not make them local
show / hide@forward "theme" show $text, $brand limits what leaves the file
sass:mapA built-in module. It emits no CSS. Also math, color, list, meta, string, selector

Variables

Defaults live in the partial

// _theme.scss
$brand: #cc6699 !default;
$text: #1a202c !default;
$surface: #ffffff !default;
$space: 1rem !default;

The entry overrides them

@use "theme" with (
  $brand: #319795,
  $space: 0.75rem
);

How assignment works

FormMeaning
$brand: #cc6699A compile-time name. It does not exist in the browser
!defaultUse this value only when the loader did not pass one
with (...)The configuration. Every name in it must be !default
First load onlyA second @use that adds with is an error. Configure the module once
Block scopeA variable written inside a rule or mixin is local to that block
!globalWrites the outer variable from inside a block. Library code rarely needs it

Nesting

Parent selector

.card {
  padding: theme.$space;

  &__title {
    font-weight: 700;
  }

  &:hover &__title {
    color: theme.$brand;
  }

  @media (min-width: 48rem) {
    padding: 1.5rem;
  }
}

What it compiles to

WrittenSelector
&__title.card__title. The & is the parent, glued on
&:hover.card:hover
&:hover &__title.card:hover .card__title. A descendant, not one class
.title nested in .card.card .title. Specificity goes up a level
Nested @mediaSass bubbles the query out and keeps the selector inside it
Three levelsThe usual ceiling. Deeper nests become selectors nobody can override

Mixins

A recipe, then a slot

@use "sass:map";

$bp: (
  "md": 48rem,
);

@mixin stack($gap: 1rem) {
  display: flex;
  flex-direction: column;
  gap: $gap;
}

@mixin up($name) {
  $min: map.get($bp, $name);
  @if $min == null {
    @error "Unknown breakpoint #{$name}.";
  }
  @media (min-width: $min) {
    @content;
  }
}

.menu {
  @include stack(0.75rem);

  @include up("md") {
    flex-direction: row;
  }
}

Mixin pieces

PieceRole
@mixinA group of declarations you can reuse. Arguments are allowed
$gap: 1remA default. Callers can omit it
@includeCopies the declarations here, with the arguments you pass
@contentThe block the caller wrote inside @include. This is how a mixin wraps @media
@errorStops the compile with a message. @warn prints and continues
Missing map keymap.get returns null. Passing that to min-width emits invalid CSS

Functions & color

Division and a hover color

@use "sass:math";
@use "sass:color";

@function rem($px) {
  @return math.div($px, 16px) * 1rem;
}

.button {
  padding: rem(12px) rem(16px);
  background: theme.$brand;

  &:hover {
    background: color.scale(theme.$brand, $lightness: -12%);
  }
}

The replacements

WriteInstead of
math.div(16px, 2)16px / 2. Slash division warns, and Dart Sass 2 treats / as a separator
calc(100% / 3)Leave / inside calc(). Sass does not treat that slash as division
color.scale($c, $lightness: -12%)A relative change. This is the usual replacement for darken()
color.adjust($c, $lightness: -12%, $space: hsl)An absolute channel change. darken() itself is removed in Dart Sass 2
@returnA function must return a value. It cannot emit declarations. That is a mixin
Built-in callmath.div(...), not a bare div(...). Global built-ins are deprecated

Maps & loops

One map, many classes

@use "sass:map";

$space: (
  "sm": 0.5rem,
  "md": 1rem,
  "lg": 1.5rem,
);

.card {
  padding: map.get($space, "md");
}

@each $name, $size in $space {
  .p-#{$name} {
    padding: $size;
  }
}

@for $i from 1 through 4 {
  .span-#{$i} {
    grid-column: span $i;
  }
}

Loop forms

FormWalks
map.get($space, "md")One value. A missing key is null
@each $name, $size in $spaceEvery pair. One name, then one value
@each $item in $listA list, one item at a time
@for $i from 1 through 41, 2, 3, 4. through includes the end
@for $i from 1 to 41, 2, 3. to stops before the end
#{$name}Interpolation. Required inside a selector or a property name

@extend

A silent placeholder

%button {
  display: inline-flex;
  padding: 0.5rem 1rem;
  border-radius: 0.5rem;
}

.save {
  @extend %button;
  background: theme.$brand;
  color: white;
}

.cancel {
  @extend %button;
  background: white;
}

Extend or mixin

ChoiceResult
%buttonA placeholder. It emits nothing until something extends it
@extend %buttonMerges selectors: .save, .cancel { ... }. The declarations are not copied
MixinCopies the declarations into each caller. Use this when there are arguments
@extend across @mediaA compile error. The two selectors do not live in the same query
Extending a real classYou also inherit every place that class was extended. Placeholders stay quieter
Same module@extend only sees selectors Sass can reach from this module

CSS variables

Compile-time in, runtime out

:root {
  --brand: #{theme.$brand};
  --space: 1rem;
}

.card {
  color: var(--brand);
  padding: var(--space);
  margin-block: calc(var(--space) / 2);
}

Which one to use

NeedUse
A value shared by many rules at build timeA Sass variable, theme.$brand
A value the browser or a theme toggle can changeA custom property, var(--brand)
Math on a Sass lengthmath.div or a unit-checked function
Math on var(--space)calc(var(--space) / 2). Sass never sees the runtime value
Hand a Sass color to CSS#{theme.$brand} so the compiled CSS contains the color
BothSet the custom property from the Sass variable once, on :root

Interview traps

Traps worth memorising

TrapWhat happensPrefer
@import "theme"Global, loaded again each time, and a warning since Dart Sass 1.80. Removed in Dart Sass 3@use "theme"
$text after @use "theme"The name is not in this filetheme.$text
@use below a style ruleCompile error. Modules load before rulesEvery @use at the top
with on a variable that has no !defaultCompile error!default on anything a caller may set
A second @use that adds withCompile error. Configuration happens on the first loadOne @use with with
16px / 2Deprecation now. In Dart Sass 2 the slash is a list separatormath.div(16px, 2)
darken($brand, 10%)Deprecated, and removed in Dart Sass 2color.scale($brand, $lightness: -10%)
node-sassNo @use, no sass: modulesThe sass package
Compiling _theme.scss itselfA partial is a library, not a pageCompile main.scss
@forward and then using the name locally@forward does not import into this fileA separate @use when this file needs the members
@for ... toThe end value is excludedthrough when the end should be included
math.div(var(--space), 2)A custom property is not a Sass numbercalc(var(--space) / 2)

The load

@use "theme" with (
  $brand: #319795
);

The division

math.div(16px, 2)

How to use this page: add the sass package, write _theme.scss with !default variables, and @use it from main.scss. Then add one mixin that takes a gap, and one @each that writes a spacing class per map entry.