SASS / SCSS
Beginner
1 min read
Creating and Accessing SASS Maps
Example
// ── Defining SASS maps ────────────────────────────────────────────────────────
@use 'sass:map';
// Simple key-value map
$font-sizes: (
'xs' : 0.75rem,
'sm' : 0.875rem,
'base': 1rem,
'lg' : 1.125rem,
'xl' : 1.25rem,
'2xl' : 1.5rem,
'3xl' : 1.875rem,
'4xl' : 2.25rem
);
// Accessing map values
body { font-size: map.get($font-sizes, 'base'); }
small { font-size: map.get($font-sizes, 'sm'); }
.lead { font-size: map.get($font-sizes, 'lg'); }
// ── Generating utility classes from a map ─────────────────────────────────────
@each $name, $size in $font-sizes {
.text-#{$name} { font-size: $size; }
}
// ── Nested maps for structured data ──────────────────────────────────────────
$palette: (
'blue': (
'100': #dbeafe,
'300': #93c5fd,
'500': #3b82f6,
'700': #1d4ed8,
'900': #1e3a8a
),
'green': (
'100': #dcfce7,
'300': #86efac,
'500': #22c55e,
'700': #15803d,
'900': #14532d
)
);
// Access a nested map value
$blue-500 : map.get(map.get($palette, 'blue'), '500');
$green-700: map.get(map.get($palette, 'green'), '700');
.link { color: $blue-500; }
.success { color: $green-700; }
// map.has-key for safe access
@mixin use-palette($color, $shade) {
@if map.has-key($palette, $color) {
$shades: map.get($palette, $color);
@if map.has-key($shades, $shade) {
background: map.get($shades, $shade);
} @else {
@warn "Shade `#{$shade}` not found in `#{$color}`.";
}
} @else {
@warn "Color `#{$color}` not found in palette.";
}
}
Related Resources
SASS / SCSS Reference
Complete tag & property list
SASS / SCSS How-To Guides
Step-by-step practical guides
SASS / SCSS Exercises
Practice what you've learned
More in SASS / SCSS