0Pricing
CSS Academy · Lesson

Sass Functions and Control Flow

Write dynamic CSS logic using Sass built-in functions, @if, @each, and @for directives.

Sass Functions and Control Flow is a free CSS Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Sass Built-in Functions

Sass provides many built-in functions for color manipulation, math, string operations, and more. These run at compile time.

$base: #3b82f6;

.darker  { background: darken($base, 15%); }
.lighter { background: lighten($base, 15%); }
.faded   { background: transparentize($base, 0.5); }
.mixed   { background: mix($base, white, 60%); }

Color Functions

Key Sass color functions:

  • darken($color, $amount)
  • lighten($color, $amount)
  • saturate($color, $amount)
  • mix($color1, $color2, $weight)
  • rgba($color, $alpha)
  • color.scale($color, $lightness: 20%)

Custom Functions with @function

Define reusable functions that return values (not CSS rules):

@function rem($px, $base: 16) {
  @return ($px / $base) * 1rem;
}

.heading { font-size: rem(32); /* 2rem */ }
.body    { font-size: rem(18); /* 1.125rem */ }

@if @else Control Flow

Conditionally output CSS based on variable values:

@mixin text-variant($variant) {
  @if $variant == "large" {
    font-size: 1.5rem;
    line-height: 1.3;
  } @else if $variant == "small" {
    font-size: 0.875rem;
    line-height: 1.4;
  } @else {
    font-size: 1rem;
    line-height: 1.5;
  }
}

@each Loop

Iterate over a list or map to generate multiple rules:

$colors: (primary: royalblue, danger: crimson, success: seagreen);

@each $name, $color in $colors {
  .btn-#{$name} {
    background: $color;
    color: white;
  }
}

@for Loop

Generate CSS for a range of values:

@for $i from 1 through 5 {
  .col-#{$i} {
    width: ($i / 5) * 100%;
  }
}
/* Generates: .col-1 (20%), .col-2 (40%), ..., .col-5 (100%) */

@while Loop

Loop while a condition is true. Use sparingly — @each and @for cover most cases:

$i: 1;
@while $i <= 4 {
  .item-#{$i} { transform: rotate(#{$i * 45}deg); }
  $i: $i + 1;
}

Sass Maps

Maps are key-value data structures in Sass:

$breakpoints: (
  "sm": 480px,
  "md": 768px,
  "lg": 1024px,
  "xl": 1280px
);

@mixin break($size) {
  @media (min-width: map.get($breakpoints, $size)) {
    @content;
  }
}

@error @warn @debug

Sass provides three diagnostic functions:

  • @error: stops compilation with a message
  • @warn: logs a warning without stopping
  • @debug: logs a value during development

Interpolation #{...}

Use #{$variable} to insert variable values into selectors, property names, or media queries:

$property: "color";

.element {
  #{$property}: royalblue; /* color: royalblue */
}

Quick Check

What is the difference between a Sass @mixin and a Sass @function?

Recap

Sass built-in color functions (darken, lighten, mix) enable compile-time color manipulation. Custom @function returns a computed value. @if/@else adds conditional output. @each iterates over lists and maps. @for iterates over number ranges. These features enable powerful, DRY stylesheets with minimal repetition.

Frequently asked questions

Is the “Sass Functions and Control Flow” lesson free?

Yes — the full text of “Sass Functions and Control Flow” is free to read here on the web, and the CSS Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO.

What will I learn in “Sass Functions and Control Flow”?

Write dynamic CSS logic using Sass built-in functions, @if, @each, and @for directives. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CSS Academy?

No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sass Functions and Control Flow” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CSS Academy lesson?

Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Variables Nesting and Partials
  2. Mixins and Includes
  3. Extend and Placeholder Selectors
  4. Sass Functions and Control Flow
← Back to CSS Academy