Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 39 ยท Projects and Practice

Sass Practice Set and Interview Questions

Test your Sass skills with eight hands-on exercises with solutions and a set of common Sass interview questions on modules, mixins, maps, extend, performance and migration.

Reading about Sass is not the same as writing it. This last lesson gives you eight exercises that cover the whole tutorial, each with a hint and a full solution, followed by interview questions you can use to check your understanding or to prepare for a job conversation. Try each exercise before opening the solution.

All solutions use the module system. The playground just below holds working versions of several of them so you can modify and extend them.

Warm-up playground

Exercise sandbox

Exercises

1. A pixel to rem function

Write to-rem($px, $base: 16px) that converts pixels to rem, so to-rem(24px) is 1.5rem.

Hint: division uses math.div; multiply the result by 1rem. Avoid the name rem, which is a built-in CSS function.

SCSS
@use "sass:math";
@function to-rem($px, $base: 16px) {
  @return math.div($px, $base) * 1rem;
}

2. Strip the unit

Write strip-unit($n) that turns 12px into 12.

Hint: dividing a number by a number of the same unit cancels the unit. Make that number from the input.

SCSS
@use "sass:math";
@function strip-unit($n) {
  @return math.div($n, $n * 0 + 1);   // 12px / 1px -> 12
}

3. A truncate mixin

Write @mixin truncate($lines: 1). For one line, use ellipsis on a single line; for more, use -webkit-line-clamp.

Hint: @if $lines == 1 { ... } @else { ... }.

SCSS
@mixin truncate($lines: 1) {
  @if $lines == 1 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  } @else {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: $lines;
    overflow: hidden;
  }
}

4. Width utilities

Generate .w-25, .w-50, .w-75 and .w-100 with a loop.

Hint: @for $i from 1 through 4, multiply by 25, and use math.percentage of a fraction.

SCSS
@use "sass:math";
@for $i from 1 through 4 {
  .w-#{$i * 25} { width: math.percentage(math.div($i * 25, 100)); }
}

5. Contrast color

Write contrast-text($bg) that returns near-black for light backgrounds and white for dark ones.

Hint: read the lightness with color.channel($bg, "lightness", $space: hsl), compare with a threshold, and return one of two colors. (Lightness is a simple approximation; real contrast uses relative luminance as defined by WCAG.)

SCSS
@use "sass:color";
@function contrast-text($bg) {
  @return if(color.channel($bg, "lightness", $space: hsl) > 60%, #111827, #ffffff);
}

6. Sum a map

Write sum($map) that adds up all values of a map of numbers, so sum((a: 1, b: 2)) is 3.

Hint: start with 0, loop with @each $key, $value in $map.

SCSS
@function sum($map) {
  $total: 0;
  @each $key, $value in $map {
    $total: $total + $value;
  }
  @return $total;
}

7. Safe lookup

Write token($name) that reads from $tokens but raises an error listing valid names for unknown ones.

Hint: map.has-key, @error, map.keys.

SCSS
@use "sass:map";
$tokens: (primary: #4f46e5, danger: #b91c1c);

@function token($name) {
  @if not map.has-key($tokens, $name) {
    @error "Unknown token `#{$name}`. Available: #{map.keys($tokens)}";
  }
  @return map.get($tokens, $name);
}

8. A module with configuration

Create _card.scss with a configurable $radius (default 8px) and $padding (default 1rem), and load it with a 12px radius.

Hint: !default in the module, with (...) in the user.

SCSS
// _card.scss
$radius: 8px !default;
$padding: 1rem !default;
.card { border-radius: $radius; padding: $padding; }

// main.scss
@use "card" with ($radius: 12px);

Interview questions

Short, direct answers, as you would give them in a conversation.

1. What is Sass, and how does it differ from CSS? Sass is a CSS preprocessor: a language that compiles to CSS. It adds variables, nesting, mixins, functions, modules, maps and control flow that run at build time. The browser only receives plain CSS.

2. SCSS or indented syntax: what is the difference? SCSS uses braces and semicolons and is a superset of CSS, so any valid CSS is valid SCSS. The indented .sass syntax uses indentation and no braces. They compile identically; SCSS is far more common.

3. What is the difference between a Sass variable and a CSS custom property? A Sass variable exists only at compile time and is replaced by its value. A custom property lives in the browser, follows the cascade, and can change at runtime (themes, JavaScript). Use Sass variables for build-time math and media queries, custom properties for runtime values.

4. Explain @use versus @import. @import pastes a file into one global scope, evaluating it each time and allowing name clashes. @use loads a module once into a namespace, supports privacy and explicit dependencies, and @forward re-exports. @import is deprecated.

5. What does @forward do? It re-exports another module's members to anyone who loads the current file, optionally filtered with show/hide or renamed with as prefix-*. It is how index files publish a folder's public API.

6. Mixin versus function? A mixin emits CSS declarations or rules (@include). A function computes and returns a value (@return). Use a function when you need a value; a mixin when you need output.

7. What is @content for? It lets a mixin accept a block of styles from the caller and place it inside its own output, for example a media query wrapper or a state selector.

8. Placeholder selectors and @extend: when to use them, and what are the downsides? A placeholder (%name) emits nothing alone; @extend makes other selectors share its rules by joining selector lists. It gives compact output for constant style bundles. Downsides: long selector lists, reordering of rules, no extending across @media. Mixins are the safer default.

9. How do maps work, and how do you loop over them? A map is a set of key-value pairs: (primary: #4f46e5, danger: #b91c1c). Read with map.get, change by creating a new map with map.merge, and loop with @each $key, $value in $map. Maps are immutable.

10. How do you do division in modern Sass? math.div($a, $b) after @use "sass:math". The / operator for division is deprecated because it conflicts with the slash as a separator in CSS.

11. How do you organize a large Sass project? With the 7-1 pattern (abstracts, vendors, base, layout, components, pages, themes) plus main.scss, index files with @forward, one component per file, and one-directional dependencies. Abstracts output no CSS.

12. How would you implement dark mode? Define theme maps with the same semantic keys, output them as CSS custom properties with a mixin on :root, [data-theme="dark"] and prefers-color-scheme: dark, and have components read var(--token).

13. How do you reduce the size of generated CSS? Limit generators, purge unused CSS, keep nesting shallow, prefer mixins to avoid huge selector lists, compress and serve with brotli, and measure compressed size.

14. How do you handle a deprecation warning? Read the id and the suggestion, fix it (often with sass-migrator), use quietDeps only for third-party code, and use --fatal-deprecation in CI to prevent regressions.

15. How does Sass handle nested selectors and the & character? & is the parent selector. It can be a prefix (.a &), a suffix (&-b, &__c), or sit within a compound (&.is-open). @at-root escapes the nesting.

16. When would you choose plain CSS over Sass? For small projects without generation needs, when native nesting, custom properties and color-mix() suffice, or when a build step is not wanted. Sass remains stronger for loops, maps, build-time math, and module-based design systems.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does strip-unit divide by $n * 0 + 1?

    12px divided by 1px gives 12 because identical units cancel.
  2. 2. In @if $lines == 1 { ... } @else { ... }, what does == compare?

    == tests whether two Sass values are equal.
  3. 3. Which is the right way to configure a module's $radius when loading it?

    with takes variable names and values, and the module marks them !default.
  4. 4. What makes an interview answer about @extend strong?

    Balanced answers show you understand trade-offs, not just syntax.
  5. 5. Which tool helps migrate @import code automatically?

    sass-migrator module rewrites @import to @use and updates references.

Summary

  • Exercises 1 to 8 practice functions, mixins, loops, maps, error handling and module configuration.
  • Interview answers should state what a feature does, why it exists, and its trade-offs.
  • You now know the language, the module system, the architecture and tooling, and have built three projects.

That completes the Sass tutorial. Keep practicing by rebuilding the stylesheet of a site you like, and refer back to the Sass track page for any chapter.

Next: Back to the Sass track