Sass and Preprocessors

Variables, nesting, mixins and partials compiled to CSS; still common in existing codebases even though native CSS has caught up.

Beginner · about 3 hours · difficulty 2/5

Learn first: Custom Properties and Modern CSS Features, Build Tools and Bundlers

Sass (using the SCSS syntax) was the tool that made large stylesheets bearable before CSS had variables and nesting. You write .scss files and a compiler outputs plain CSS. Many design systems, Bootstrap themes and enterprise apps still use it, so you should be able to read and change it.

Core features

  • Variables: $brand: #3355ff; resolved at compile time (unlike CSS custom properties, which live at runtime).
  • Nesting: write child selectors inside parents. Keep it shallow; deep nesting produces over-specific selectors.
  • Partials and modules: split styles into _buttons.scss and load them with @use, which namespaces members. @import is deprecated.
  • Mixins and functions: reusable chunks such as a respond-to(md) media query mixin, or colour maths.
  • Loops and maps: generate utility classes from a map of spacing values.

Sass vs native CSS today

Native CSS now has custom properties, nesting, color-mix() and cascade layers. That means new projects often need no preprocessor. Sass still shines for heavy compile-time generation and in codebases already built on it.

Pitfalls

  • Using Sass variables for things that should change at runtime (themes). Use CSS custom properties there.
  • Nesting four levels deep to mirror HTML. This creates fragile, high-specificity selectors.
  • Mixing @import and @use in one codebase.

Good enough to move on

You can read an SCSS codebase, add a partial, write a breakpoint mixin and explain when a Sass variable vs a CSS custom property is the right choice.

Key concepts

  • SCSS syntax and compilation
  • Variables vs CSS custom properties
  • Nesting and the parent selector &
  • @use, partials and namespaces
  • Mixins and functions
  • Maps and loops
  • BEM naming: A class-naming convention often paired with Sass

Other options for this step

  • Tailwind CSS: Utility-first styling that keeps design decisions consistent and colocated with markup; our recommended default for new projects.
  • CSS Modules: Write normal CSS files whose class names are scoped to one component automatically, with zero runtime cost.
  • CSS-in-JS: Style components with JavaScript (styled-components, Emotion) or zero-runtime tools (vanilla-extract, Panda); know the trade-offs.

Mini project

Convert a plain CSS stylesheet into SCSS partials with a breakpoint mixin, a spacing map that generates margin utilities, and CSS custom properties for a light/dark theme.

Check yourself

Why can a Sass variable not power a runtime dark mode toggle while a CSS custom property can?

Free resources