SASS/SCSS / Mixins and Functions
Using Built-in SASS/SCSS Functions
In this tutorial, you'll discover the power of built-in SASS/SCSS functions. These functions offer a variety of utilities that can speed up your style writing process.
Section overview
5 resourcesTeaches how to create reusable code using mixins and functions in SASS/SCSS.
Using Built-in SASS/SCSS Functions
1. Introduction
Goal
This tutorial aims to help you understand and utilize the built-in functions of SASS/SCSS, a powerful CSS preprocessor.
Learning Outcomes
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
Prerequisites
A basic understanding of CSS is required. Familiarity with SASS/SCSS is useful, but not necessary.
2. Step-by-Step Guide
SASS/SCSS comes with several built-in functions, primarily for color manipulation, list/map manipulation, and mathematical operations.
Color Functions
lighten($color, $percentage): This function makes a color lighter by the given percentagedarken($color, $percentage): This function makes a color darker by the given percentage
List/Map Functions
nth($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 map
Math Functions
percentage($number): This function converts a number to a percentageround($number): This function rounds a number to the nearest whole number
3. Code Examples
Example 1: Color Functions
$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
}
Example 2: List/Map Functions
$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
}
Example 3: Math Functions
.number-class {
width: percentage(0.5); // Outputs 50%
padding: round(15.7px); // Outputs 16px
}
4. Summary
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.
5. Practice Exercises
Exercise 1:
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.
Solution:
$red: #ff0000;
.lighten-red {
color: lighten($red, 10%);
}
.darken-red {
color: darken($red, 10%);
}
Exercise 2:
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.
Solution:
$colors: 'red', 'green', 'blue', 'yellow', 'black';
.nth-color {
background-color: nth($colors, 3); // Outputs 'blue'
}
Exercise 3:
Create a map of font properties and values. Use the map-get function to set the properties of a class .map-get-font.
Solution:
$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!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article