SASS/SCSS / Maps and Lists in SASS/SCSS
Building Flexible Styles with Lists and Maps
This tutorial will demonstrate how to build flexible and reusable styles using lists and maps in SASS/SCSS. We will focus on how to use these tools to improve your styling workflo…
Section overview
5 resourcesExplains the use of maps and lists for storing and managing data in SASS/SCSS.
Introduction
In this tutorial, we will explore how to build flexible and reusable styles using lists and maps in SASS/SCSS. Our goal is to improve your understanding of these tools and demonstrate their practical use in enhancing your styling workflow.
By the end of this tutorial, you'll be able to:
- Understand the use and benefits of lists and maps in SASS/SCSS
- Create and manipulate lists and maps in SASS/SCSS
- Use lists and maps to manage complex styling in a more efficient way
Before starting, ensure you have basic knowledge of HTML, CSS, and a general understanding of SASS/SCSS.
Step-by-Step Guide
SASS/SCSS Lists
A list in SASS/SCSS is a series of values, separated by either spaces or commas. They can hold any type of value (numbers, strings, colors, etc.) and they don't require brackets or quotes to define.
$list: 10px 20px 30px; // space-separated list
$list: 10px, 20px, 30px; // comma-separated list
You can access list elements using the nth() function.
$list: 10px, 20px, 30px;
.element {
padding: nth($list, 1); // 10px
}
SASS/SCSS Maps
A map is a collection of key-value pairs. Maps are handy when you want to group related variables together.
$map: (key1: value1, key2: value2, key3: value3);
You can retrieve a value from a map using map-get() function.
$colors: (primary: #333, secondary: #666, accent: #999);
.element {
color: map-get($colors, primary); // #333
}
Code Examples
Example 1: Using lists for margins
Here, we're using a list to manage the margins of an element.
$margins: 10px, 20px, 30px, 40px;
.element {
margin: nth($margins, 1) nth($margins, 2) nth($margins, 3) nth($margins, 4);
}
This will compile to:
.element {
margin: 10px 20px 30px 40px;
}
Example 2: Using maps for theme colors
Here, we're using a map to manage the theme colors of a website.
$theme-colors: (primary: #333, secondary: #666, accent: #999);
.element {
background-color: map-get($theme-colors, primary);
border-color: map-get($theme-colors, secondary);
}
This will compile to:
.element {
background-color: #333;
border-color: #666;
}
Summary
In this tutorial, we've learned how to use lists and maps in SASS/SCSS to create flexible and reusable styles. We've seen how they can simplify and enhance our styling workflow by providing a way to group related values together.
The next step would be to explore how to use lists and maps in loops and functions to further enhance their utility. You can find more information and advanced usage in the SASS/SCSS documentation.
Practice Exercises
- Create a list of font sizes (14px, 16px, 18px) and use it to style three elements.
- Create a map of font families (serif, sans-serif, monospace) and use it to style three elements.
- Create a map of breakpoints (small: 480px, medium: 768px, large: 1024px) and use it to create a responsive layout.
Here are the solutions and explanations:
-
Solution:
scss $font-sizes: 14px, 16px, 18px; .element1 { font-size: nth($font-sizes, 1); } .element2 { font-size: nth($font-sizes, 2); } .element3 { font-size: nth($font-sizes, 3); }
Explanation: We've created a list of font sizes and used nth() function to apply different sizes to different elements. -
Solution:
scss $font-families: (serif: 'Times New Roman', sans-serif: 'Arial', monospace: 'Courier New'); .element1 { font-family: map-get($font-families, serif); } .element2 { font-family: map-get($font-families, sans-serif); } .element3 { font-family: map-get($font-families, monospace); }
Explanation: We've created a map of font families and used map-get() function to apply different families to different elements. -
Solution:
scss $breakpoints: (small: 480px, medium: 768px, large: 1024px); @media (max-width: map-get($breakpoints, small)) { /* styles for small screens */ } @media (min-width: map-get($breakpoints, medium)) and (max-width: map-get($breakpoints, large)) { /* styles for medium screens */ } @media (min-width: map-get($breakpoints, large)) { /* styles for large screens */ }
Explanation: We've created a map of breakpoints and used map-get() function in media queries to create a responsive layout.
Keep practicing and experimenting with different values and use-cases to get more comfortable with lists and maps in SASS/SCSS. Enjoy 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