Creating and Accessing Maps in SASS/SCSS

Tutorial 3 of 5

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!