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.

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Teaches 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 percentage
  • darken($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 list
  • map-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 percentage
  • round($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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Scientific Calculator

Perform advanced math operations.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help