This tutorial aims to help you understand and utilize the built-in functions of SASS/SCSS, a powerful CSS preprocessor.
By the end of this tutorial, you will be able to:
- Understand the basic concepts of SASS/SCSS
- Use different built-in SASS/SCSS functions
- Apply the functions to practical coding situations
A basic understanding of CSS is required. Familiarity with SASS/SCSS is useful, but not necessary.
SASS/SCSS comes with several built-in functions, primarily for color manipulation, list/map manipulation, and mathematical operations.
lighten($color, $percentage)
: This function makes a color lighter by the given percentagedarken($color, $percentage)
: This function makes a color darker by the given percentagenth($list, $n)
: This function gets the nth item in a listmap-get($map, $key)
: This function retrieves the value associated with a specific key from a mappercentage($number)
: This function converts a number to a percentageround($number)
: This function rounds a number to the nearest whole number$primary-color: #3CABFA;
.lighten-class {
background-color: lighten($primary-color, 20%); // Makes the primary color 20% lighter
}
.darken-class {
background-color: darken($primary-color, 20%); // Makes the primary color 20% darker
}
$list: 'apple', 'banana', 'cherry';
.map-class {
content: nth($list, 2); // Outputs 'banana'
}
$map: (font-size: 14px, padding: 10px);
.map-get-class {
font-size: map-get($map, font-size); // Outputs 14px
}
.number-class {
width: percentage(0.5); // Outputs 50%
padding: round(15.7px); // Outputs 16px
}
In this tutorial, we've covered the basics of built-in SASS/SCSS functions, including color functions, list/map functions, and mathematical functions.
As next steps, try experimenting with these functions in your CSS files, and look up more SASS/SCSS functions to broaden your toolkit. You can find additional resources at the official SASS documentation.
Use the lighten
and darken
functions to create two classes, .lighten-red
and .darken-red
, that lighten and darken the color red by 10%, respectively.
$red: #ff0000;
.lighten-red {
color: lighten($red, 10%);
}
.darken-red {
color: darken($red, 10%);
}
Create a list of five colors. Use the nth
function to set the background color of a class .nth-color
to the third color in the list.
$colors: 'red', 'green', 'blue', 'yellow', 'black';
.nth-color {
background-color: nth($colors, 3); // Outputs 'blue'
}
Create a map of font properties and values. Use the map-get
function to set the properties of a class .map-get-font
.
$font-map: (font-family: Arial, font-size: 16px, line-height: 1.5);
.map-get-font {
font-family: map-get($font-map, font-family); // Outputs Arial
font-size: map-get($font-map, font-size); // Outputs 16px
line-height: map-get($font-map, line-height); // Outputs 1.5
}
Remember, practice makes perfect. Keep exploring more functions and applying them to your code. Happy coding!