SASS Tutorial (build your own CSS library) #8 - Maps

Share

Summary

This video explains what Sass maps are, how to create them, and how to manipulate them using built-in Sass functions like `map-get`, `map-has-key`, `map-remove`, and `map-merge`. It also demonstrates how to use map values within CSS selectors.

Highlights

Introduction to SASS Maps
00:00:02

Sass maps are similar to associative arrays or JavaScript objects, allowing you to collect variables or values in key-value pairs. They are particularly useful for generating utility classes by looping through the map and creating a class for each value.

Creating a Color Palette Map
00:00:43

The video demonstrates creating a "colors" map to store a palette of colors. It shows how to use existing variables (like primary, secondary, error, info) as values and also how to add new key-value pairs with specific hex codes or color keywords (e.g., blue, red, green, black, white). Using variables ensures that color changes only need to be made in one place.

Retrieving Values from a Map
00:02:56

The `map-get` function is introduced to retrieve values from a map. The first argument is the map name (e.g., `colors`), and the second is the key (e.g., `purple`). The example shows how to debug and see the retrieved color value in the terminal.

Checking for Keys in a Map
00:03:46

The `map-has-key` function is explained, which checks if a map contains a specific key. It takes the map name and the key as arguments and returns `true` or `false`. This is useful for evaluating maps to see if a variable exists, which can be combined with `if` statements later.

Removing and Adding to a Map
00:04:30

The `map-remove` function is shown to remove a key-value pair from a map. The `map-merge` function is used to add new key-value pairs to an existing map. For `map-merge`, you pass the original map and then a new map containing the key-value pairs you want to add (e.g., adding a 'pink' color).

Using Map Values in CSS Selectors
00:05:54

The video concludes by demonstrating how to use the `map-get` function directly within CSS selectors. An example shows setting the `background-color` of a `test-btn` class using a color retrieved from the `colors` map, illustrating how map functionality integrates with CSS styling.

Recently Summarized Articles

Loading...