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 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

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
How to remove special characters (dash, asterisk etc) from any string in PHP?PHPCan We Use For Loop to Loop Through Associative Arrays in PHP?PHPHow to change CSS with JavaScript?CSSDefault Values in SCSS (Sass)SCSSEXISTS and NOT EXISTS in PostgresPostgresHow to disable right click and drag and drop of images using jQuery?jQuery