CSS Preprocessors & Tools

Sass Basics

Syntactically Awesome Style Sheets - a powerful CSS preprocessor with variables, nesting, and functions.

What is Sass and why use it?

Sass is a CSS preprocessor: you write .scss files with extra features (nesting, variables, mixins, functions, partials) and a compiler turns them into plain CSS. Native CSS now has nesting and custom properties, so Sass matters less than it did, but mixins, loops and @use modules are still useful in large codebases.

// _tokens.scss
$brand: #3b82f6;

// main.scss
@use "tokens";
@mixin card { border-radius: 12px; padding: 1rem; }

.card {
  @include card;
  border: 1px solid tokens.$brand;
  &:hover { border-color: darken(tokens.$brand, 10%); }
}

Complete Sass Example

Code Editor

<p>Browsers can't read SCSS directly. This is the <b>SCSS source</b>:</p>
<pre>$brand: #2563eb;

@mixin card { border-radius: 12px; padding: 1rem; }

.card {
  @include card;
  border: 2px solid $brand;
  h3 { color: $brand; }          // nesting
  &:hover { background: #eff6ff; }
}</pre>
<div class="card"><h3>Compiled card</h3><p>Styled by the CSS tab.</p></div>

Preview

Sass Fundamentals

Variables

// Define variables with $
$primary-color: #3498db;
$secondary-color: #e74c3c;
$font-family: 'Helvetica Neue', Arial, sans-serif;
$base-font-size: 16px;
$line-height: 1.6;
$border-radius: 4px;

// Maps for organized data
$colors: (
  primary: #3498db,
  secondary: #e74c3c,
  success: #2ecc71,
  warning: #f39c12
);

// Use variables
.button {
  background: $primary-color;
  color: white;
  border-radius: $border-radius;
}

// Access map values
.alert {
  background: map-get($colors, warning);
}

Variables store reusable values and make global changes easy. Use maps for organized data structures.

Nesting

// Sass nesting
.navigation {
  background: white;
  padding: 1rem;

  ul {
    list-style: none;
    margin: 0;

    li {
      display: inline-block;

      a {
        text-decoration: none;
        color: #333;

        &:hover {
          color: blue;
        }
      }
    }
  }
}

Nest CSS rules inside other rules for better organization

Parent Selector (&)

// Parent selector usage
.button {
  background: blue;
  color: white;

  // Pseudo-classes
  &:hover {
    background: darkblue;
  }

  // Modifiers
  &--large {
    padding: 1rem 2rem;
    font-size: 1.2rem;
  }

  // Context
  .sidebar & {
    width: 100%;
  }
}

Use & to reference the parent selector for modifiers and states

Mixins & Functions

Mixins - Reusable Code Blocks

// Define mixins
@mixin button-style($bg-color, $text-color: white) {
  background: $bg-color;
  color: $text-color;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;

  &:hover {
    background: darken($bg-color, 10%);
  }
}

@mixin flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

@mixin responsive($breakpoint) {
  @media (min-width: $breakpoint) {
    @content;
  }
}

// Use mixins
.primary-button {
  @include button-style(#3498db);
}

.header {
  @include flex-center;

  @include responsive(768px) {
    padding: 2rem;
  }
}

Mixins accept parameters and can include @content for flexible, reusable styles.

Functions

// Built-in functions
$primary: #3498db;

.button {
  background: $primary;
  border: 1px solid darken($primary, 15%);

  &:hover {
    background: lighten($primary, 10%);
  }
}

// Custom functions
@function rem($pixels) {
  @return $pixels / 16px * 1rem;
}

@function color($key) {
  @return map-get($colors, $key);
}

.text {
  font-size: rem(18px); // 1.125rem
  color: color(primary);
}

Functions return values and can perform calculations or data lookups

Control Directives

// @each loop
$colors: (primary: blue, secondary: red);

@each $name, $color in $colors {
  .text-#{$name} {
    color: $color;
  }
}

// @for loop
@for $i from 1 through 12 {
  .col-#{$i} {
    width: percentage($i / 12);
  }
}

// @if conditional
@mixin theme($type) {
  @if $type == dark {
    background: #333;
    color: white;
  } @else {
    background: white;
    color: #333;
  }
}

Use loops and conditionals to generate dynamic CSS

Setup & Best Practices

Installation & Setup

  • • Dart Sass: npm install -g sass
  • • VS Code: Live Sass Compiler extension
  • • Build Tools: Webpack, Vite, Parcel integration
  • • Watch Mode: sass --watch input.scss output.css
  • • Compressed Output: sass --style compressed
  • • Source Maps: sass --source-map
  • • File Organization: Partials with underscores
  • • Import System: @use and @forward

Best Practices

  • • Use @use instead of @import for better performance
  • • Limit nesting to 3-4 levels maximum
  • • Use meaningful variable names ($primary-color vs $blue)
  • • Organize code with partials (_variables.scss, _mixins.scss)
  • • Prefer mixins for repeated patterns with variations
  • • Use functions for calculations and data access
  • • Follow consistent naming conventions (BEM + Sass)
  • • Document complex mixins and functions

File Structure Example

scss/
├── abstracts/
│   ├── _variables.scss
│   ├── _functions.scss
│   ├── _mixins.scss
│   └── _index.scss
├── base/
│   ├── _reset.scss
│   ├── _typography.scss
│   └── _index.scss
├── components/
│   ├── _buttons.scss
│   ├── _cards.scss
│   ├── _navigation.scss
│   └── _index.scss
├── layout/
│   ├── _header.scss
│   ├── _footer.scss
│   ├── _grid.scss
│   └── _index.scss
└── main.scss // Main entry point

Sass — step by step

1 / 4
1

Install and compile Sass

Install the sass package with npm. Point it at your .scss source files and it compiles them to standard .css that browsers can read.

1 of 4
💡Sass vs CSS Custom Properties
Sass variables are resolved at compile time and disappear in the output. CSS custom properties (--var) exist at runtime and can be changed with JavaScript. Use Sass variables for static design tokens and CSS custom properties for theme switching.

🎯 Quick Check

What Sass feature lets you define reusable blocks of CSS that accept arguments?

Sass (SCSS): summary

Key points

  • SCSS syntax is a superset of CSS: any valid CSS file is valid SCSS.
  • Sass variables ($x) are compiled away. CSS variables (--x) exist at runtime and can change.
  • @use replaced the old @import, which is deprecated in Sass.
  • Deep nesting creates overly specific selectors. Keep nesting to two or three levels.

Frequently asked questions

›Is Sass still worth learning?

Learn modern CSS first: nesting, custom properties and @layer cover most of what people used Sass for. Sass is still common in existing projects and frameworks like Bootstrap, so it's worth knowing the basics.

›What is the difference between Sass and SCSS?

They're two syntaxes of the same language. SCSS uses braces and semicolons like CSS and is by far the more popular. The original "indented" Sass syntax uses indentation instead.