Sass Mini Project
Time to put everything together. In this project you'll build a small, themeable design system — buttons and cards — step by step. Each step adds one Sass technique on top of the previous one, and the preview shows the result.
By the end you'll have used: maps for tokens, functions, mixins with @content, @each loops, BEM with &, sass:color for derived shades, and CSS custom properties for runtime themes.
In a real project, each step would go into its own partial (7-1 style):
scss/
abstracts/_tokens.scss ← step 1
abstracts/_functions.scss ← step 2
abstracts/_mixins.scss ← step 3
base/_theme.scss ← step 4
components/_button.scss ← step 5
components/_card.scss ← step 6
main.scssHere we keep everything in one playground per step.
Step 1 — Design tokens
Start with tokens in maps. Nothing is output yet — tokens are pure data. We print one value with @debug to check it:
@use "sass:map";
$colors: (
brand: #cc6699,
ink: #2d2a32,
surface: #ffffff,
muted: #6b6470,
);
$space: (1: 4px, 2: 8px, 3: 12px, 4: 16px, 6: 24px);
$radius: (sm: 4px, md: 8px, pill: 999px);
@debug map.get($colors, brand);Debug (line 13): #cc6699
Step 2 — Helper functions
Functions give safe, readable access to the tokens. An unknown key fails loudly:
@use "sass:map";
$space: (1: 4px, 2: 8px, 3: 12px, 4: 16px, 6: 24px);
$radius: (sm: 4px, md: 8px, pill: 999px);
@function space($step) {
@if not map.has-key($space, $step) {
@error "Unknown space step #{$step}";
}
@return map.get($space, $step);
}
@function radius($name) {
@return map.get($radius, $name);
}
.preview {
padding: space(2) space(4);
margin-bottom: space(6);
border-radius: radius(md);
}.preview {
padding: 8px 16px;
margin-bottom: 24px;
border-radius: 8px;
}
Step 3 — Mixins
A focus ring and a "button reset" will be shared by every interactive component:
@use "sass:color";
@mixin focus-ring($color: #cc6699) {
&:focus-visible {
outline: 3px solid color.change($color, $alpha: 0.45);
outline-offset: 2px;
}
}
@mixin button-reset {
appearance: none;
border: 0;
font: inherit;
cursor: pointer;
}
.demo {
@include button-reset;
@include focus-ring;
}.demo {
appearance: none;
border: 0;
font: inherit;
cursor: pointer;
}
.demo:focus-visible {
outline: 3px solid rgba(204, 102, 153, 0.45);
outline-offset: 2px;
}
color.change sets the alpha channel to an exact value, so the ring is always 45% opaque whatever colour you pass in.
Step 4 — Themes as custom properties
Generate a light and a dark theme from the brand colour with sass:color, and publish them as custom properties:
@use "sass:color";
$brand: #cc6699;
$themes: (
light: (
bg: #ffffff,
text: #2d2a32,
surface: #faf5f8,
brand: $brand,
brand-strong: color.scale($brand, $lightness: -20%),
brand-soft: color.scale($brand, $lightness: 85%),
),
dark: (
bg: #17131a,
text: #f1edf3,
surface: #241e28,
brand: color.scale($brand, $lightness: 25%),
brand-strong: color.scale($brand, $lightness: 40%),
brand-soft: color.scale($brand, $lightness: -70%),
),
);
@each $name, $theme in $themes {
.theme-#{$name} {
@each $key, $value in $theme {
--#{$key}: #{$value};
}
}
}.theme-light {
--bg: #ffffff;
--text: #2d2a32;
--surface: #faf5f8;
--brand: #cc6699;
--brand-strong: rgb(72%, 24%, 48%);
--brand-soft: rgb(97%, 91%, 94%);
}
.theme-dark {
--bg: #17131a;
--text: #f1edf3;
--surface: #241e28;
--brand: rgb(85%, 55%, 70%);
--brand-strong: rgb(88%, 64%, 76%);
--brand-soft: rgb(27%, 9%, 18%);
}
Step 5 — The button component
Now the button: one BEM block with size and style modifiers generated from maps. All colours come from custom properties, so the button works in both themes automatically.
@use "sass:map";
@mixin button-reset {
appearance: none;
border: 0;
font: inherit;
cursor: pointer;
}
$sizes: (sm: 4px 10px, md: 8px 16px, lg: 12px 22px);
.btn {
@include button-reset;
padding: map.get($sizes, md);
border-radius: 8px;
background: var(--brand);
color: var(--bg);
font-weight: 600;
transition: background 150ms;
&:hover { background: var(--brand-strong); }
@each $name, $pad in $sizes {
&--#{$name} { padding: $pad; }
}
&--soft {
background: var(--brand-soft);
color: var(--brand-strong);
}
&--pill { border-radius: 999px; }
}.btn {
appearance: none;
border: 0;
font: inherit;
cursor: pointer;
padding: 8px 16px;
border-radius: 8px;
background: var(--brand);
color: var(--bg);
font-weight: 600;
transition: background 150ms;
}
.btn:hover {
background: var(--brand-strong);
}
.btn--sm {
padding: 4px 10px;
}
.btn--md {
padding: 8px 16px;
}
.btn--lg {
padding: 12px 22px;
}
.btn--soft {
background: var(--brand-soft);
color: var(--brand-strong);
}
.btn--pill {
border-radius: 999px;
}
The preview below sets the theme custom properties from step 4 by hand, since this playground only contains the button:
Step 6 — The card component and final assembly
Finally, combine everything — tokens, functions, mixins, themes, button and card — into one stylesheet, as main.scss would produce after loading all the partials:
@use "sass:color";
@use "sass:map";
// abstracts
$brand: #cc6699;
$space: (2: 8px, 3: 12px, 4: 16px);
@function space($step) { @return map.get($space, $step); }
@mixin focus-ring {
&:focus-visible { outline: 3px solid var(--brand-soft); outline-offset: 2px; }
}
// themes
$themes: (
light: (bg: #ffffff, text: #2d2a32, surface: #faf5f8, brand: $brand,
brand-strong: color.scale($brand, $lightness: -20%),
brand-soft: color.scale($brand, $lightness: 85%)),
dark: (bg: #17131a, text: #f1edf3, surface: #241e28,
brand: color.scale($brand, $lightness: 25%),
brand-strong: color.scale($brand, $lightness: 40%),
brand-soft: color.scale($brand, $lightness: -70%)),
);
@each $name, $theme in $themes {
.theme-#{$name} {
@each $key, $value in $theme { --#{$key}: #{$value}; }
background: var(--bg);
color: var(--text);
}
}
// components
.btn {
border: 0;
font: inherit;
cursor: pointer;
padding: space(2) space(4);
border-radius: 999px;
background: var(--brand);
color: var(--bg);
&:hover { background: var(--brand-strong); }
@include focus-ring;
}
.card {
background: var(--surface);
border-radius: 12px;
padding: space(4);
max-width: 220px;
&__title { margin: 0 0 space(2); color: var(--brand-strong); }
&__text { margin: 0 0 space(3); line-height: 1.5; }
}.theme-light {
--bg: #ffffff;
--text: #2d2a32;
--surface: #faf5f8;
--brand: #cc6699;
--brand-strong: rgb(72%, 24%, 48%);
--brand-soft: rgb(97%, 91%, 94%);
background: var(--bg);
color: var(--text);
}
.theme-dark {
--bg: #17131a;
--text: #f1edf3;
--surface: #241e28;
--brand: rgb(85%, 55%, 70%);
--brand-strong: rgb(88%, 64%, 76%);
--brand-soft: rgb(27%, 9%, 18%);
background: var(--bg);
color: var(--text);
}
.btn {
border: 0;
font: inherit;
cursor: pointer;
padding: 8px 16px;
border-radius: 999px;
background: var(--brand);
color: var(--bg);
}
.btn:hover {
background: var(--brand-strong);
}
.btn:focus-visible {
outline: 3px solid var(--brand-soft);
outline-offset: 2px;
}
.card {
background: var(--surface);
border-radius: 12px;
padding: 16px;
max-width: 220px;
}
.card__title {
margin: 0 0 8px;
color: var(--brand-strong);
}
.card__text {
margin: 0 0 12px;
line-height: 1.5;
}
Ideas to extend the project
- Add a third theme (e.g.
oceanfrom#0277bd) — only the$themesmap changes. - Add
.card--featuredwith a border invar(--brand). - Move each step into partials and wire them together with
@use/@forward. - Add a
prefers-color-scheme: darkblock that applies the dark theme automatically.
Test yourself
Why do the button and card use var(--brand) instead of $brand directly?
- Sass variables can't hold colours
- So the same CSS rules work in every theme at runtime
- It compiles faster
- Custom properties are required by BEM
Which part of the project produced no CSS output on its own?
- The theme loop
- The button rules
- The token maps and functions
- The card rules
How would you add a new 'ocean' theme?
- Rewrite every component
- Add an
oceanentry to the$themesmap - Create a new mixin for each component
- Use @extend on .theme-light