SASS/SCSS / Maps and Lists in SASS/SCSS

Using Lists in SASS/SCSS for Reusable Data

This tutorial will cover how to create and use lists in SASS/SCSS for storing reusable data. It will also demonstrate how to access and manipulate list data.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explains the use of maps and lists for storing and managing data in SASS/SCSS.

Using Lists in SASS/SCSS for Reusable Data

1. Introduction

Goal of the Tutorial

The goal of this tutorial is to guide you on how to create and use lists in SASS/SCSS to store reusable data.

Learning Objectives

By the end of this tutorial, you will learn:
- How to create lists in SASS/SCSS
- How to access elements in a list
- How to manipulate list data

Prerequisites

Before starting this tutorial, you should have a basic understanding of HTML, CSS, and SCSS syntax.

2. Step-by-Step Guide

List in SASS/SCSS

A list in SASS/SCSS is a single variable that stores multiple values. They can be comma or space-separated. To create a list in SASS/SCSS, simply assign multiple space or comma-separated values to a variable.

$font-stack: Arial, sans-serif;

Accessing List Elements

You can access elements in a list using the nth($list, $n) function, where $list is the list variable and $n is the position of the element in the list.

$font-stack: Arial, sans-serif;
$primary-font: nth($font-stack, 1); // Arial

Manipulating List Data

SASS/SCSS provides several built-in functions to manipulate list data, such as length($list), append($list, $value), join($list1, $list2), etc.

$font-stack: Arial, sans-serif;
$font-stack: append($font-stack, 'Helvetica'); // Arial, sans-serif, Helvetica

3. Code Examples

Example 1: Creating and Accessing Lists

// Define a list of fonts
$font-stack: Arial, sans-serif, Verdana, 'Courier New';

// Access the first font in the list
.primary-font {
  font-family: nth($font-stack, 1); // Arial
}

Example 2: Manipulating Lists

// Define a list of fonts
$font-stack: Arial, sans-serif;

// Add a new font to the list
$font-stack: append($font-stack, 'Helvetica');

// Check the length of the list
font-length: length($font-stack); // 3

// Join two lists
$more-fonts: Times, 'Courier New';
$all-fonts: join($font-stack, $more-fonts);

.all-fonts {
  content: '#{$all-fonts}'; // Arial, sans-serif, Helvetica, Times, 'Courier New'
}

4. Summary

In this tutorial, you learned how to create, access, and manipulate lists in SASS/SCSS. You also learned about the nth, append, length, and join functions.

To learn more about SASS/SCSS, consider exploring topics such as mixins, functions, and control directives.

5. Practice Exercises

Exercise 1:

Create a list of five colors and use it to set the background colors of five different classes.

Solution:

$colors: red, blue, green, yellow, purple;

.red {
  background-color: nth($colors, 1);
}

.blue {
  background-color: nth($colors, 2);
}

.green {
  background-color: nth($colors, 3);
}

.yellow {
  background-color: nth($colors, 4);
}

.purple {
  background-color: nth($colors, 5);
}

Exercise 2:

Append a new color to the list and use it to set the background color of a new class.

Solution:

$colors: append($colors, pink);

.pink {
  background-color: nth($colors, 6);
}

Exercise 3:

Join the $colors list with a new list of your choice and output the resulting list.

Solution:

$more-colors: black, white;
$all-colors: join($colors, $more-colors);

.all-colors {
  content: '#{$all-colors}'; // red, blue, green, yellow, purple, pink, black, white
}

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

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Backlink Checker

Analyze and validate backlinks.

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