How To Use Sass Functions

SASS functions can be used to perform common calculations, create reusable code snippets, and even generate dynamic styles based on inputs. In this tutorial, we’ll cover the basics of creating and using SASS functions, including syntax, parameters, return values, and some common use cases.

Creating a SASS Function

To create a SASS function, you first need to define it using the @function directive, followed by the function name and any parameters it may take. Here’s an example of a simple function that calculates the area of a rectangle based on its width and height;

@function calculate-area($width, $height) {
  @return $width * $height;

In this example, we’ve defined a function called calculate-area that takes two parameters, @width and @height, and returns their product using the @return directive. To use this function, we simply call it like any other SASS function, passing in the necessary arguments;

$area: calculate-area(10px, 20px);

This will set the value of the $area to 200px.

Using SASS Functions

SASS functions can be used in a variety of ways to make your stylesheets more modular and easier to maintain. Here are a few examples;

  • Creating a Color Function
@function tint($color, $percent) {
  @return lighten($color, $percent);

This function will lighten a color by a certain percentage, making it easy to create tints of a base color.

  • Generating Responsive Styles
@function responsive($property, $values) {
  $output: '';
  @each $value in $values {
    $output: #{$output}#{$property}: $value;
  @return $output;

This function takes a CSS property and an array of values, and generates a set of responsive styles that apply to different breakpoints. For example;

h1 {
  font-size: responsive('font-size', (16px, 20px, 24px));

This will generate the following CSS;

h1 {
  font-size: 16px;

@media screen and (min-width: 768px) {
  h1 {
    font-size: 20px;

@media screen and (min-width: 1200px) {
  h1 {
    font-size: 24px;


SASS functions are a powerful tool that can help you write more maintainable and modular stylesheets. By creating custom functions for common calculations, generating dynamic styles, and more, you can make your stylesheets easier to read, write, and maintain. With the help of SASS functions, you can take your CSS to the next level and create truly amazing stylesheets.

Other Blogs about Sass:


Leave a Comment

Your email address will not be published. Required fields are marked *