SASS/SCSS / Mixins and Functions

Writing Reusable Functions in SASS/SCSS

This tutorial will guide you on how to write reusable functions in SASS/SCSS, enabling you to handle more complex styling tasks with ease.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Teaches how to create reusable code using mixins and functions in SASS/SCSS.

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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

Image Converter

Convert between different image formats.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help