YogeshChauhan . com

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

What’s a repository on Github?MiscThe actual difference between indexOf() and search() in JavaScriptJavaScriptAn Example of Cross-site Scripting (XSS) Attack in PHP and How to Avoid It?PHPHow to check if the page is the home page in WordPress?WordPressWhat is iFrame in HTML? Why do we need it?HTMLHow to select multiple values in React dropdown?React