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.
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
}
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
}
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;
}
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;
}
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.
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!