Generating dynamic colors from a map in SASS within a media query function

Utilizing @include-media for SASS breakpoints (). Seeking to create a dynamic breakpoint helper text that changes background color on various media sizes.

Important

Breakpoint mixin - https://raw.githubusercontent.com/eduardoboucas/include-media/master/dist/_include-media.scss. Looking for a straightforward solution. The end goal is to dynamically generate colors from maps or lists.

SCSS

//  BREAKPOINT (Mobile first)
    $breakpoints: (
        'sm':   540px,
        'md':   768px,
        'lg':   1025px,
        'xl':   1360px
    )!default;

    $breakpoint-colors: ( 
        red, green, yellow, blue
    )

//  Container (Mobile first)
    $container: (
        'sm':   520px,
        'md':   740px,
        'lg':   1024px,
        'xl':   1320px,
    )!default;

//  generate
    @each $name, $value in $breakpoints{
        @include media('>=#{$name}') {
            &:after {
                content: "#{$name} >= #{$value}";
            }
        }
    }
}

Expected Output

@media (min-width: 540px) {
    body:after {
        content: "Media: sm >= 540px | Container: 520px;";
        background-color: red;
    }
}

@media (min-width: 1024px) {
    body:after {
        content: "Media: sm >= 768px | Container: 740px;";
        background-color: green;
    }
}

@media (min-width: 1200px) {
    body:after {
        content: "Media: sm >= 1025px | Container: 1024px;";
        background-color: yellow;
    }
}

@media (min-width: 1600px) {
    body:after {
        content: "Media: sm >= 1360px | Container: 1320px;";
        background-color: blue;
    }
}

Answer №1

The issue you are encountering is related to scope. By defining the breakpoints variable within the body tag, you are unable to properly override the default breakpoints definition from the sass mixins being utilized.

I have also converted your $breakpoint-colors into a map for more streamlined mapping of corresponding breakpoint values.

You can try the following code for compilation that aligns with your requirements:

$breakpoints: (        
  'sm': 540px,
  'md': 768px,
  'lg': 1025px,
  'xl': 1360px
);

$breakpoint-colors: ( 
  'sm': red, 
  'md': green,
  'lg': yellow,
  'xl': blue
);

$containers: (        
  'sm': 520px,
  'md': 740px,
  'lg': 1024px,
  'xl': 1320px,
);

body {
  @each $name, $value in $breakpoints {
    $container: map_get($containers, $name);
    $background: map_get($breakpoint-colors, $name);
    @include media('>=#{$name}') {
      &:after {
        content: "Media: #{$name} >= #{$value} | Container: #{$container}";
        background: $background;
      }
    }
  }
}

Upon compilation, the output will be as follows:

@media (min-width: 540px) {
  body:after {
    content: "Media: sm >= 540px | Container: 520px";
    background: red;
  }
}
@media (min-width: 768px) {
  body:after {
    content: "Media: md >= 768px | Container: 740px";
    background: green;
  }
}
@media (min-width: 1025px) {
  body:after {
    content: "Media: lg >= 1025px | Container: 1024px";
    background: yellow;
  }
}
@media (min-width: 1360px) {
  body:after {
    content: "Media: xl >= 1360px | Container: 1320px";
    background: 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

Leverage the power of Bootstrap 5 to showcase multiple form input-groups side by

Is there a way to showcase multiple form input groups side by side using Bootstrap 5? I attempted the following, but the input group components ended up stacking on top of each other: <div class="container"> Your username is <div class= ...

Tips for adding an animated box shadow in CSS without adjusting the shadow's direction

A unique box with a rotating animation was created, however, applying a box-shadow caused the shadow to also rotate along with the box. <!DOCTYPE html> <html> <head> <title>Page Title</title> <style> body{ displa ...

Modify the color of a sub division's background using CSS when hovering over

As I work on this Fiddle, my goal is to change the background of each div section individually when moused over. However, currently, hovering over one section triggers the background change for all sections. Is there a way to ensure that only the specific ...

File uploading feature in Bootstrap (without the need for Javascript)

While I've seen this question posted in a similar style before, one critical point (in my opinion) hasn't been addressed - is there a way to achieve this without using JavaScript? On mobile devices, overscripted sites can sometimes have non-worki ...

The jquery UI button is displaying too wide even though the padding is set to zero

My goal is to decrease the width of certain jQuery buttons. Though I have attempted to eliminate padding using the code below, there remains a minimum of 5 pixels on either side of the text within my button. .button().css({ 'padding-left':' ...

The impact of CSS positioning relative on the height of an element

I need help with positioning elements in my HTML layout. I have one element stacked below another and am using relative positioning to nudge the bottom element up slightly so that it overlaps the top element. The paperOverlay element is positioned at the ...

Tips for aligning divs in Zurb Foundation 3 framework

My design conundrum involves a 12 column row and trying to make a series of four div blocks stay in a row without stacking as the browser size decreases. Currently, the divs start to stack when the browser gets smaller. I have 4 divs that should be displa ...

Add a new row to the table when a dropdown option is selected, and remove the row when deleted. Ensure that the row is only added

Here is my specific requirement: I need a table with a default row containing a dropdown menu in the first column. When an option is selected from the dropdown, a new table row should be added with the same content as the main row and a delete button for ...

What is the reason for min-content not being compatible with auto-fill or auto-fit?

My confusion lies in the fact that this code snippet works: .grid { display: grid; grid-template-columns: repeat(4, min-content); } Whereas this one does not: .grid { display: grid; grid-template-columns: repeat(auto-fill, min-content); } I am ...

Changing Background Colors of Grid Items in React.js Grid Layout

I'm currently learning React and have created a basic app that shows data from a database in a grid format. I'm looking to customize the background colors of each grid item based on its priority level, which is pulled from the database. The prior ...

Designing a structure with three fieldset components

I'm currently trying to design a page layout that includes three fieldsets. My goal is to have the first one span across 100% of the width, with the other two positioned side by side below it at 50% width each. However, I am struggling to achieve thi ...

When a mouse hovers over, introduce a smooth opacity change

I'm currently working on a project to create a React app and I'd like to incorporate an opacity fade-in transition for an image overlay when the user hovers over it. While I understand how to achieve this using the traditional ":hover" method, I& ...

Ways to prevent a specific class from using CSS styling

My HTML <body> <div id="finalparent"> <!--many parent divs here--> <div id="1stparent"> <div id="txt_blog_editor" class="box" style="width: 1097px;"> <div class="abc anotherclass"> </div> & ...

Adjusting font size in a responsive div using Bootstrap 4

I'm attempting to dynamically resize text within a div, similar to the functionality shown in this example. As I resize the div, I want the font size to adjust accordingly. I've tried using various relative length units in pure CSS for the text ...

Using jQuery and HTML to create numerous links that direct to a solitary iframe

I created a basic overlay setup using css, html, and jQuery. Here's how it looks: <a class="activator" id="activator" style="" href="#">hello</a> <div class="overlay" id="overlay" style="display:none;"></div> <div style="d ...

What is the best way to utilize MUI breakpoints for displaying images based on different screen sizes?

I need help displaying an image based on the screen size using MUI breakpoints. I'm struggling to understand how to implement this with MUI. Can someone assist me with the breakpoints? interface EmptyStateProps { title: string; description: string ...

Is there a way to insert a colored square into a <button> tag rather than text?

Looking for help with the following HTML: <button name="darkBlue" onclick="setThemeColor(this.name)">Blue</button> <button name="black" onclick="setThemeColor(this.name)">Black</button> I'm interested in replacing the text on ...

Chrome causing a background-size cover issue

I encountered an issue with this code. It runs smoothly on Firefox 50, but encounters failures on Chrome Version 54.0.2840.99 m (64-bit) on Windows. .test { background: url(datas/images/3.jpg) no-repeat center center fixed; background-size: cover ...

How can I set the minimum size of a ParamQuery Grid integrated within a Bootstrap tab?

My grid is hidden "behind" a bootstrap tab, only visible when the tab is clicked. However, each time I click the tab, the grid collapses to zero size, forcing me to expand it manually every time. I attempted to prevent this by setting the "object" to obj ...

Arranging arrows strategically along a line and ensuring they are positioned correctly above all div elements

I'm really struggling with this issue. I have a situation where I need to center a downward pointing arrow within some divs. It should be positioned in the middle, on a brown line, and on top of everything else. The last div should also include the a ...