Yogesh Chauhan's Blog

How to create a function in SCSS (Sass)?

in SCSS on June 10, 2021

Functions are one of the best concepts in programming along with some other buddies like arrays, classes, objects, pointers and tone of more.

Functions are nothing but an abstraction of common code pattern.

Functions are a great way to make code reusable so quickly. You can define some complex operations in functions that can be reused throughout the stylesheet and you can import it to another modules too.

Functions are declared using the @function at-rule in SassScript.

SassScript Function Syntax


@function name(args) { 
  ...
}

Any naming rules?

You can use any Sass identifier as name for your function.

Function names in SassScript treat underscores and hyphens as identical. This means that font-color and font_color both refer to the same function.

What’s inside a function?

A function can only contain universal statements. Universal statements are the statements that can be used anywhere in a Sass stylesheet.

Additionally, a function will have @return at-rule like any function that will return the result of the function call.

How to call the function?

You can use the CSS function syntax to call a function.


SCSS Function example

In this example, we are using a for loop inside a function to create a power function.


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Sorting Arrays in JavaScriptJavaScriptHow to create a simple stopwatch using JavaScript?JavaScriptHow to Draw a Circle in HTML5 Using Canvas Tag?HTMLThe difference between isFinite() method and isFinite() function in JavaScriptJavaScriptHow states work in React?ReactHow to enable GD library support for PHP on windows server?PHP