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.
Section overview
5 resourcesExplains 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
- Exercise 1: Create a map named
$social-media-colorswith the following key-value pairs: - "facebook": #3b5998
- "twitter": #00acee
- "instagram": #C13584
- "linkedin": #0077b5
Then, style a div with the class .facebook to have a background color of Facebook's color.
- Exercise 2: Create a map named
$font-sizeswith the following key-value pairs: - "small": 12px
- "medium": 16px
- "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:
- Solution to Exercise 1:
$social-media-colors: (
"facebook": #3b5998,
"twitter": #00acee,
"instagram": #C13584,
"linkedin": #0077b5
);
.facebook {
background-color: map-get($social-media-colors, "facebook");
}
- 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.
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