The many potentials of looping through Sass maps

My sass map contains a variety of color shades, and the full code can be found on Codepen:

$pbcolors: (

pbcyan : (
50:  #E5F5FC,
100: #CCEBF9,
200: #99D7F2,
300: #66C3EC,
400: #33AFE5,
500: #009DBF,
600: #008CAB,
700: #007C98,
800: #006D85,
900: #005D72
),
pbmediumblue: (
50:  #E5F1F8,
100: #CCE3F1,
200: #99C7E3,
300: #66AAD4,
400: #338EC6,
500: #0072B8,
600: #0065A5,
700: #005A93,
800: #004F80,
900: #00436E
),
pbpurple: (
50:  #F5ECF5,
100: #ECD9EB,
200: #D9B2D7,
300: #C68CC3,
400: #B365AF,
500: #A03F9B,
600: #90388B,
700: #80327C,
800: #702C6C,
900: #60255D
);

I'm attempting to generate classes using a loop that combines the color and shade as part of the class name with the corresponding hex value as the background color. An example class would look like this:

.bg-pbmediumblue-100 { background: #CCE3F1; }

However, my current loop syntax seems to be incorrect as I am not progressing to the second level of the map:

@each $item, $color in $pbcolors {
  @each $shade, $value in $item {
    .bg-#{$shade}-#{$shade} {
      background-color: map-get(map-get($pbcolors, $item), 50);
    }
  }
}

As a result, I'm only retrieving the 50 value of each color and the class names are incorrect:

.bg-pbcyan-pbcyan {
   background-color: #E5F5FC;
 }

.bg-pbmediumblue-pbmediumblue {
   background-color: #E5F1F8;
 }

Where did I make a mistake?

Answer №1

It seems like you may be mistakenly referencing the incorrect variable in your code snippet. The $item variable, which comes first, is actually used to refer to the mapping key name, not the value itself (which comes second). To properly iterate over the values, you should adjust your inner loop accordingly.

@each $item, $color in $pbcolors {
  @each $shade, $value in $color {
    .bg-#{$item}-#{$shade} {
      background-color: $value;
    }
  }
}

Answer №2

Instead of using a loop to generate tints, I opted for a function that allows us to call the tints only when needed. This way, we avoid unnecessary printing of all tints.

@function getColor($colorName, $shade: 500) {
   @return map-get(map-get($colors, $colorName), $shade);
}

With this approach, getting the color on demand is quite straightforward:

.example {
  background-color: getColor(blue, 500);
 }

We can also use the function without specifying the shade to get the default shade of 500:

.example {
  background-color: getColor(blue);
 }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS Transition - Troubleshooting my malfunctioning code

I seem to be facing a simple issue with my code. The following snippet does not work in Chrome or Firefox, as the transition property is not working properly. Instead of smoothly transitioning from blue to red when hovered over, the element immediately swi ...

Adjusting containers for optimal display during browser zoom and resize operations

Currently, I have designed a banner using a triangle and rectangle to overlay an image. However, when the browser is zoomed in, there seems to be a gap between these two shapes. I am looking for suggestions on how to better connect or approach this banner ...

Having trouble with CSS styling not applying to the header tag

Take a look at this snippet of HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link href="css/test.css" rel="stylesheet"> </head> <p></p> <h2>Meeting the Old Guard ...

Is it possible to reverse the effects of @media queries for min/max width and height?

I rely on http://www.w3.org/TR/css3-mediaqueries/ to customize my design based on the available viewport size. When I need to adjust the breakpoints, I struggle with negating @media (min-width: 500px), which overlaps with (max-width: 500px). To counter th ...

Animating a div using a changing scope variable in AngularJS

As a newcomer to Angular, I am looking to add animation to a div element using ng-click within an ng-repeat loop. Here is what I have attempted: app.js var app = angular.module( 'app', [] ); app.controller('appController', function($ ...

maintaining the alignment of one div's right boundary with another div's right boundary

---------------------- ----------------------------->edge X | | | | | logo | | dropdown menu ...

Creating styles for different screen sizes including mobile devices, low-resolution desktops, and high-resolution desktops

One of the techniques I'm using involves analyzing both the horizontal and vertical screen dimensions to differentiate between mobile devices and desktops, as well as distinguish high-resolution from low-resolution desktop displays. In order to achie ...

Guide on making an SVG tooltip using the Menucool JS extension

I am trying to add a simple tooltip to an SVG "map" when the mouse hovers over a rectangle. To create the tooltip, I would like to utilize this specific plugin. Here is how I have connected the SVG to HTML: <object data="map.svg" type="image/svg+xml" ...

Tips for bridging the space between jumbotron and about section using Bootstrap

How can I eliminate the gap between the jumbotron and the about section? I have attempted to reduce the margin between the two sections, but it is not working properly. <section id="atas"> <div class="jumbotron jumbotron-flu ...

Fixing the bug in the circle text animation

I followed the instructions in this tutorial and implemented the following code: Splitting(); * { margin: 0; padding: 0; box-sizing: border-box; font-family: monospace; } body { display: flex; justify-content: center; align-items: center; ...

Use the same CSS style for various attribute selectors

Is there a way to simultaneously apply the following style to a type="submit" without having to repeat the entire block of code? input[type="button"] { width: 120px; margin-left: 35px; display: block; } ...

Emphasize specific letters in a word by making them bold, according to the user

In my app, there is a search feature that filters data based on user input and displays a list of matching results. I am trying to make the text that was searched by the user appear bold in the filtered data. For example, if the user searches 'Jo&apos ...

Issues with Bootstrap's hamburger menu not functioning properly when clicked

Can't seem to get my hamburger button working. I've followed the bootstrap documentation and ensured that Bootstrap and jQuery are in the correct order, but still no luck. Any suggestions on what could be causing this issue? Are my links misplace ...

Internet Explorer does not support the use of a:focus

Unfortunately, the issue with a:focus not working in IE is still unresolved. I have been searching for a solution, but have not been able to find one yet. <div class="city-selection"> <ul> ...

Troubleshooting Alignment Problems in Bootstrap 4 Tables

Having trouble aligning certain items. In thisFiddle, I have two options: Option1 and Option2. I want to align the toggle switches with the ones in the first two rows. Specifically, I want the toggle switches to be in the second column of the table and t ...

The drop-down links vanish into the depths of the webpage's body

Can someone assist with my drop-down menu issue for the link "services"? The link disappears behind the page content when I try to modify it. Any help would be appreciated, thank you! This is the CSS: .bodycontent { margin: 0 25% 0 25%; ...

Achieving Left and Right Alignment of Navigation Elements using Flex Basis in Chakra UI with Next JS

For my project, I am working on creating a straightforward Navigation bar using Chakra UI and Next JS. The goal is to have an svg logo positioned at the top-left, while the menu and UI buttons are aligned to the top-right. However, I'm facing challeng ...

What could be causing my video not to display a thumbnail on mobile devices until it is played?

When I view my website on mobile devices, there is no background image displayed before playing a video. Below is the HTML code I am using: <div class="row " > <div class="col-12 text-center"> <video control ...

Having trouble converting svg to png, thinking it might be due to discrepancies in svg elements

I am facing a puzzling issue where two different SVG elements are causing my JavaScript to work in one scenario but not the other. I have replaced the SVG elements in both examples, and surprisingly, only one works while the other does not. You can view th ...

Is there a way to create rectangles with a designated percentage of blue color using CSS or Bootstrap 3?

There's an image on my laptop that perfectly illustrates what I mean, but unfortunately, I'm not sure how to share it with you. In essence, I am looking to create a rectangle filled with a specific percentage of the color blue. Could you assist m ...