Debugging Styles with @debug

Tutorial 2 of 5

1. Introduction

Goal of the Tutorial

This tutorial aims to introduce the @debug directive in SASS/SCSS and how to use it for debugging purposes to print the value of expressions.

Learning Objectives

Upon completion of this tutorial, you will be able to:
- Understand what the @debug directive is.
- Use the @debug directive to print the value of variables, expressions, or functions.
- Debug your SASS/SCSS code more effectively.

Prerequisites

You should have a basic understanding of:
- HTML/CSS
- SASS/SCSS syntax

2. Step-by-Step Guide

The @debug directive is a debugging tool in SASS/SCSS. It prints the value of an expression to the standard error output. This can be very useful when debugging your SASS/SCSS code as it enables you to check the values of variables, expressions, or functions at any point in your code.

Using @debug

To use @debug, simply pass the variable, expression, or function you want to check as an argument to the @debug directive, like so:

$color: blue;
@debug $color;

This will output the following in your console:

DEBUG: blue

Tips and Best Practices

  • Only use @debug for debugging purposes. Remove all @debug directives once you're done debugging.
  • Be specific with what you debug. Too many @debug directives can make the output confusing.

3. Code Examples

Example 1: Debugging a Variable

$font-size: 16px;
@debug $font-size; // This will print: DEBUG: 16px

Example 2: Debugging an Expression

$padding: 10px;
$margin: 20px;
@debug $padding + $margin; // This will print: DEBUG: 30px

Example 3: Debugging a Function

@function sum($a, $b) {
  @return $a + $b;
}
@debug sum(5, 10); // This will print: DEBUG: 15

4. Summary

In this tutorial, we learned about the @debug directive in SASS/SCSS and how to use it for debugging purposes to print the value of expressions. This is a powerful tool for debugging that can help you understand and fix issues in your code.

For further learning, consider exploring other SASS/SCSS directives such as @warn and @error, which provide additional debugging capabilities.

5. Practice Exercises

Exercise 1: Debugging a Variable

Create a variable $border-width and set its value to 5px. Use @debug to print its value.

Solution

$border-width: 5px;
@debug $border-width; // This will print: DEBUG: 5px

Exercise 2: Debugging an Expression

Create two variables $width and $height, setting their values to 100px and 200px respectively. Use @debug to print their sum.

Solution

$width: 100px;
$height: 200px;
@debug $width + $height; // This will print: DEBUG: 300px

Exercise 3: Debugging a Function

Create a function that multiplies two numbers and use @debug to print the result of multiplying 5 and 20.

Solution

@function multiply($a, $b) {
  @return $a * $b;
}
@debug multiply(5, 20); // This will print: DEBUG: 100

Always practice with more complex exercises to enhance your understanding of @debug and its applications in debugging SCSS/SASS.