SASS/SCSS / Maps and Lists in SASS/SCSS

Creating and Accessing Maps in SASS/SCSS

This tutorial will explore how to create maps in SASS/SCSS and how to access their data. We will also look at some practical examples of maps usage in styling.

Tutorial 3 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.

1. Introduction

Welcome to this comprehensive guide on how to create and access Maps in SASS/SCSS. Our primary aim in this tutorial is to help you understand the concept of Maps in SASS/SCSS, how to create them, and how to access their data.

By the end of this tutorial, you will be able to:
- Understand what Maps are and their use cases in SASS/SCSS
- Create Maps in SASS/SCSS
- Access and manipulate data in Maps

Prerequisites

  • Basic knowledge of SASS/SCSS
  • Basic knowledge of CSS
  • A text editor (e.g., Visual Studio Code, Atom, Sublime Text)
  • A modern web browser for testing

2. Step-by-Step Guide

SASS/SCSS Maps are like arrays, but they use key-value pairs instead of indexes. They are similar to JavaScript Objects or Python Dictionaries.

Creating a Map

To create a map in SASS/SCSS, we use parentheses () and separate key-value pairs with a comma ,. Each key-value pair is separated by a colon :.

$color-map: (
    primary: #007bff,
    secondary: #6c757d,
    success: #28a745,
    danger: #dc3545,
);

Accessing Data in a Map

To access data in a map, we use the map-get() function. It takes two arguments - the map and the key.

body {
    background-color: map-get($color-map, primary);
}

3. Code Examples

Let's take a look at a complete example:

// Defining a map
$theme-colors: (
    "primary":    #007bff,
    "secondary":  #6c757d,
    "success":    #28a745,
    "danger":     #dc3545
);

// Accessing data
body {
    background-color: map-get($theme-colors, "primary"); // This will set the body's background color to #007bff
    color: map-get($theme-colors, "secondary"); // This will set the body's text color to #6c757d
}

In this example, we defined a map named $theme-colors with four key-value pairs. We then used the map-get() function to access the values using their keys.

4. Summary

In this tutorial, we learned about SASS/SCSS maps - how to create them, and how to access their data. Maps are powerful tools that can help us write more efficient and manageable SCSS code.

To continue learning about SASS/SCSS, you can explore functions like map-has-key(), map-keys(), map-values(), and map-remove(), which provides more ways to manipulate maps.

5. Practice Exercises

  1. Exercise 1: Create a map named $social-media-colors with the following key-value pairs:
  2. "facebook": #3b5998
  3. "twitter": #00acee
  4. "instagram": #C13584
  5. "linkedin": #0077b5

Then, style a div with the class .facebook to have a background color of Facebook's color.

  1. Exercise 2: Create a map named $font-sizes with the following key-value pairs:
  2. "small": 12px
  3. "medium": 16px
  4. "large": 24px

Then, style three p elements with classes .small, .medium, and .large to have font sizes corresponding to the values in the map.

Solutions:

  1. Solution to Exercise 1:
$social-media-colors: (
    "facebook": #3b5998,
    "twitter": #00acee,
    "instagram": #C13584,
    "linkedin": #0077b5
);

.facebook {
    background-color: map-get($social-media-colors, "facebook");
}
  1. Solution to Exercise 2:
$font-sizes: (
    "small": 12px,
    "medium": 16px,
    "large": 24px
);

.small {
    font-size: map-get($font-sizes, "small");
}

.medium {
    font-size: map-get($font-sizes, "medium");
}

.large {
    font-size: map-get($font-sizes, "large");
}

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

Time Zone Converter

Convert time between different time zones.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

Color Palette Generator

Generate color palettes from images.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Unit Converter

Convert between different measurement units.

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