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 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
@use "theme";
body {
color: theme.$text;
background: theme.$surface;
} 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. 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
@forward "stack";
@use "components";
.page {
@include components.stack;
} Defaults live in the partial
$brand: #cc6699 !default;
$text: #1a202c !default;
$surface: #ffffff !default;
$space: 1rem !default; The entry overrides them @use "theme" with (
$brand: #319795,
$space: 0.75rem
); Parent selector .card {
padding: theme.$space;
&__title {
font-weight: 700;
}
&:hover &__title {
color: theme.$brand;
}
@media (min-width: 48rem) {
padding: 1.5rem;
}
} 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;
}
} 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%);
}
} 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;
}
} 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;
} Compile-time in, runtime out :root {
--brand: #{theme.$brand};
--space: 1rem;
}
.card {
color: var(--brand);
padding: var(--space);
margin-block: calc(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. |