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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Solution to Precision Problem in JavaScript NumbersJavaScriptHow to hide a DIV on clicks outside of it using jQuery?jQuerySome Useful Operators in The SQL WHERE ClauseSQL/MySQLSubquery in PostgresPostgresAll different methods for accessing elements in the DOM using JavaScriptJavaScriptWhy it’s not a good idea to create Number objects in JavaScript?JavaScript