Sass Exercises
Practise what you've learned. Each exercise has a starter playground — edit it, press ▶ Run, and compare the compiled CSS with what the task asks for. When you're done (or stuck), open the solution.
Tip: The output panel shows the CSS your code produces right now. A task is solved when your output matches the "expected CSS" described in the task.
Exercise 1 — Variables
Replace the repeated values with three variables: $brand (#cc6699), $ink (#2d2a32) and $space (12px). The compiled CSS must stay exactly the same.
.header {
background: #cc6699;
color: #2d2a32;
padding: 12px;
}
.footer {
border-top: 2px solid #cc6699;
color: #2d2a32;
padding: 12px;
}.header {
background: #cc6699;
color: #2d2a32;
padding: 12px;
}
.footer {
border-top: 2px solid #cc6699;
color: #2d2a32;
padding: 12px;
}
Show solution
$brand: #cc6699;
$ink: #2d2a32;
$space: 12px;
.header {
background: $brand;
color: $ink;
padding: $space;
}
.footer {
border-top: 2px solid $brand;
color: $ink;
padding: $space;
}Exercise 2 — Nesting and &
Rewrite these flat rules using nesting so that .tab appears only once in your source. Use & for the hover state and the --active modifier.
.tab { padding: 8px 14px; }
.tab:hover { background: #f6e3ec; }
.tab--active { border-bottom: 3px solid #cc6699; }
.tab .tab-icon { margin-right: 6px; }.tab {
padding: 8px 14px;
}
.tab:hover {
background: #f6e3ec;
}
.tab--active {
border-bottom: 3px solid #cc6699;
}
.tab .tab-icon {
margin-right: 6px;
}
Show solution
.tab {
padding: 8px 14px;
&:hover { background: #f6e3ec; }
&--active { border-bottom: 3px solid #cc6699; }
.tab-icon { margin-right: 6px; }
}Exercise 3 — A mixin with arguments
Write a mixin size($w, $h: $w) that sets width and height. Use it so that .avatar is 40px × 40px and .banner is 100% × 120px.
// Write the mixin here
.avatar {
border-radius: 50%;
}
.banner {
background: #fbe9f2;
}.avatar {
border-radius: 50%;
}
.banner {
background: #fbe9f2;
}
Show solution
@mixin size($w, $h: $w) {
width: $w;
height: $h;
}
.avatar {
@include size(40px);
border-radius: 50%;
}
.banner {
@include size(100%, 120px);
background: #fbe9f2;
}Exercise 4 — A rem() function
Write a function rem($px) that converts pixels to rem with a 16px base, using math.div. Expected: h1 { font-size: 2.25rem; } and p { font-size: 1.125rem; }.
@use "sass:math";
// Write the function here
h1 { font-size: 36px; }
p { font-size: 18px; }h1 {
font-size: 36px;
}
p {
font-size: 18px;
}
Show solution
@use "sass:math";
@function rem($px) {
@return math.div($px, 16px) * 1rem;
}
h1 { font-size: rem(36px); }
p { font-size: rem(18px); }Exercise 5 — Generate classes with @each
Using the map below, generate a .btn-<name> class for each entry that sets background to the colour. Expected output: .btn-primary, .btn-danger and .btn-dark rules.
$buttons: (
primary: #cc6699,
danger: #d32f2f,
dark: #2d2a32,
);
// Loop here
.btn {
color: white;
}.btn {
color: white;
}
Show solution
$buttons: (
primary: #cc6699,
danger: #d32f2f,
dark: #2d2a32,
);
.btn {
color: white;
}
@each $name, $color in $buttons {
.btn-#{$name} {
background: $color;
}
}Exercise 6 — A spacing scale with @for
Generate .p-1 to .p-5 where each class sets padding to the number × 4px (so .p-5 is 20px).
$unit: 4px;
// Generate .p-1 … .p-5 here
.box {
border: 1px solid #ccc;
}.box {
border: 1px solid #ccc;
}
Show solution
$unit: 4px;
.box {
border: 1px solid #ccc;
}
@for $i from 1 through 5 {
.p-#{$i} {
padding: $i * $unit;
}
}Exercise 7 — Colour shades without darken()
Replace the deprecated functions with sass:color equivalents so the compiler prints no warnings. Use color.adjust with the same amounts.
$brand: #cc6699;
.link {
color: $brand;
&:hover { color: darken($brand, 10%); }
&:visited { color: lighten($brand, 10%); }
}.link {
color: #cc6699;
}
.link:hover {
color: rgb(75%, 25%, 50%);
}
.link:visited {
color: rgb(85%, 55%, 70%);
}
Deprecation warning (line 5): Global built-in functions are deprecated and will be removed in Dart Sass 3.0.0.
Deprecation warning (line 5): darken() is deprecated. Suggestions:
Deprecation warning (line 6): Global built-in functions are deprecated and will be removed in Dart Sass 3.0.0.
Deprecation warning (line 6): lighten() is deprecated. Suggestions:
Show solution
@use "sass:color";
$brand: #cc6699;
.link {
color: $brand;
&:hover { color: color.adjust($brand, $lightness: -10%); }
&:visited { color: color.adjust($brand, $lightness: 10%); }
}Exercise 8 — A breakpoint mixin
Write a mixin up($bp) that looks up $bp in $breakpoints with map.get and wraps @content in @media (min-width: …). Then make .layout use flex-direction: row from the md breakpoint upwards.
@use "sass:map";
$breakpoints: (sm: 576px, md: 768px, lg: 1024px);
// Write the mixin here
.layout {
display: flex;
flex-direction: column;
}.layout {
display: flex;
flex-direction: column;
}
Show solution
@use "sass:map";
$breakpoints: (sm: 576px, md: 768px, lg: 1024px);
@mixin up($bp) {
@media (min-width: map.get($breakpoints, $bp)) {
@content;
}
}
.layout {
display: flex;
flex-direction: column;
@include up(md) {
flex-direction: row;
}
}Exercise 9 — Placeholder and @extend
Create a placeholder %chip with display: inline-block, padding: 2px 8px and border-radius: 999px. Make .tag and .status extend it, so the compiled CSS contains one shared rule .status, .tag { … } (or .tag, .status).
.tag {
background: #eee;
}
.status {
background: #e8f5e9;
}.tag {
background: #eee;
}
.status {
background: #e8f5e9;
}
Show solution
%chip {
display: inline-block;
padding: 2px 8px;
border-radius: 999px;
}
.tag {
@extend %chip;
background: #eee;
}
.status {
@extend %chip;
background: #e8f5e9;
}Test yourself
In Exercise 7, why does replacing darken() remove the warnings?
- darken() is slower
- Global color functions are deprecated in favour of the sass:color module
- darken() produces invalid CSS
- Warnings only appear for lighten()
Which loop best fits Exercise 5 (one class per map entry)?
- @for
- @while
- @each
- @if