CSS Academy · Lesson

Sass Functions and Control Flow

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

Lesson 4 of 412 steps

Sass Functions and Control Flow is a free CSS Academy lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The CSS Academy course includes 4 lessons in total.

Sass Built-in Functions

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

<div class="darker">darken($base, 15%)</div>
  <div class="lighter">lighten($base, 15%)</div>
  <div class="faded">transparentize($base, 0.5)</div>
  <div class="mixed">mix($base, white, 60%)</div>

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):

<h2 class="heading">Heading (2rem)</h2>
  <p class="body">Body text (1.125rem)</p>

@if @else Control Flow

Conditionally output CSS based on variable values:

<p class="text-large">Large text variant</p>
  <p class="text-small">Small text variant</p>
  <p class="text-default">Default text variant</p>

@each Loop

Iterate over a list or map to generate multiple rules:

<button class="btn-primary">Primary</button>
  <button class="btn-danger">Danger</button>
  <button class="btn-success">Success</button>

@for Loop

Generate CSS for a range of values:

<div class="col-1">col-1 (20%)</div>
  <div class="col-2">col-2 (40%)</div>
  <div class="col-3">col-3 (60%)</div>
  <div class="col-4">col-4 (80%)</div>
  <div class="col-5">col-5 (100%)</div>

@while Loop

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

<div class="item-1">1</div>
  <div class="item-2">2</div>
  <div class="item-3">3</div>
  <div class="item-4">4</div>

Sass Maps

Maps are key-value data structures in Sass:

<div class="box">Responsive box (resize wider than 768px to see color change)</div>

@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:

<p class="element">Interpolated property (#{$property})</p>

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.

Free to start

Learn CSS with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

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. 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. The CSS Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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