Sass - Functions


Advertisements

Description

SASS supports the use of functions by providing some keyword arguments, which are specified using normal CSS function syntax.

Syntax

p {
   color: hsl($hue: 0, $saturation: 50%, $lightness: 50%);
}

HSL stands for hue, saturation, and lightness, which are more intuitive for creating a set of matching colors by using saturation and lightness.

  • hue − It represents the degree of color such as 120 for red, 240 for green, 290 for pastel violet etc.

  • saturation − It is a percentage value that increases the saturation of color.

  • lightness − It is a percentage value which decreases the lightness of color.

Example

The following example demonstrates the use of functions in the SCSS file −

<html>
   <head>
      <title>Functions Example</title>
      <link rel = "stylesheet" type = "text/css" href = "style.css" />
      <link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
      <script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
   </head>

   <body>
      <div class = "container">
         <h2>Example using Functions</h2>
         <p>SASS stands for Syntactically Awesome Stylesheet..</p>
      </div>
   </body>
</html>

Next, create file style.scss.

style.scss

Use the following SCSS code which defines the HSL function on the SASS code.

p {
   color: hsl(290,60%,70%);
}

You can tell SASS to watch the file and update the CSS whenever SASS file changes, by using the following command −

sass --watch C:\ruby\lib\sass\style.scss:style.css

Next, execute the above command; it will create the style.css file automatically with the following code −

style.css

p {
   color: #d185e0;
}

Output

Let us carry out the following steps to see how the above given code works −

  • Save the above given html code in functions.html file.

  • Open this HTML file in a browser, an output is displayed as shown below.

Sass Functions
sass_script.htm
Advertisements