Writing Reusable Functions in SASS/SCSS

Tutorial 3 of 5

Writing Reusable Functions in SASS/SCSS

1. Introduction

In this tutorial, our primary goal is to teach you how to write reusable functions in SASS/SCSS, which can help you to handle complicated styling tasks effortlessly.

You will learn:

  • The basics of SASS/SCSS functions
  • How to write your own reusable functions
  • Best practices for creating reusable functions

Prerequisites:

  • Basic knowledge of CSS
  • Familiarity with SASS/SCSS syntax

2. Step-by-Step Guide

Just like in any programming language, functions in SASS are blocks of code designed to perform a particular task. In the case of SASS, these tasks usually involve computations or manipulations related to CSS properties and values.

  1. Defining a Function: We define a function using the @function directive, followed by the function name and a pair of parentheses (). Inside these parentheses, we put any parameters that the function needs to do its job.

  2. Returning a Value: To get a result from a function, we use the @return directive. This tells SASS what value to give back when the function is called.

Example:

@function double($number) {
  @return $number * 2;
}

This function, named double, takes one parameter $number and returns the double of that number.

  1. Calling a Function: To use a function, we simply write its name, followed by a pair of parentheses. Inside these parentheses, we put any arguments that we want to pass to the function.

Example:

p {
  font-size: double(10px);  // Calls the 'double' function with 10 as an argument
}

This will compile to p { font-size: 20px; } in CSS.

3. Code Examples

Let's look at more practical examples:

Example 1: Creating a function to convert pixels to ems

Code Snippet:

@function pxToEm($pixels, $base: 16) {
  @return #{$pixels/$base}em;
}

p {
  font-size: pxToEm(18px);  // Calls the 'pxToEm' function with 18 as an argument
}

In this example, we created a function pxToEm that converts pixel values to em values. The $base parameter has a default value of 16, but you can change it when you call the function if you want to use a different base.

Example 2: Creating a function to calculate percentage

Code Snippet:

@function percentage($target, $container) {
  @return #{$target/$container*100}%;
}

div {
  width: percentage(200px, 960px);  // Calls the 'percentage' function with 200 and 960 as arguments
}

This function takes a target value and a container value, and it returns the percentage of the target relative to the container.

4. Summary

In this tutorial, we've learned how to create reusable functions in SASS/SCSS, which can make your code cleaner and easier to maintain. We've also looked at some practical examples to illustrate these concepts.

To continue learning, you might want to look into more advanced topics such as control directives and data types in SASS.

5. Practice Exercises

Exercise 1: Create a function that multiplies any number by 10.

Exercise 2: Create a function that converts pixels to rems.

Exercise 3: Create a function that returns the average of three numbers.

Make sure to test your functions and use them in your CSS rules. Happy coding!