SASS causing conflicts with CSS breakpoints selector overrides

I am working with a partial file named _display.scss.

It includes various @mixin and styling classes related to the display CSS property.

_display.scss

@mixin d-block{
    display: block;
}

@mixin d-none{
    display: none;
}

.d-block{
    @include d-block();
}

.d-none{
    @include d-none();
}

I have also created a @mixin called generate-responsive-content, which takes the @content of a class and generates different @media queries for each breakpoint.

For example:

.d-block{
    @include generate-responsive-content() {
        @include d-block();
    }
}

.d-none{
    @include generate-responsive-content() {
        @include d-none();
     }
}

// Generate all breakpoints from content
@mixin generate-responsive-content() {

  // Responsive styles
  // Loop over each size
  @each $breakName, $width in $breakpoints {

    // Check breakpoint
    @if ($breakName != "") {
      $breakName: '-' + $breakName;

      @media (min-width: $width) {
        &#{$breakName} {
          @content
        }
      }

    } @else {
      @content;
    }

  }
}

For instance, classes like: .d-block, .d-block-xs, .d-block-sm...

However, I encountered an issue where I cannot override the classes of .d-none with the classes of .d-block for each breakpoint because they are generated earlier and get overwritten by those of .d-none.

In addition, there are classes with the same name but without breakpoint variants, such as d-none-lg, d-block-lg, which take precedence over others.

You can view a demonstration on this CodePen. Here, d-none variants overwrite every class of d-block.

How can I resolve this conflict?

Answer №1

Check out this demonstration I made for you: https://codepen.io/rhythm19/pen/OJVMyLa. It's functioning as anticipated. My suggestion is to rearrange the process. Start by creating the d-none classes first, followed by the d-block classes.

.d-none{
    @include generate-responsive-content() {
        @include d-none();
     }
}

.d-block{
    @include generate-responsive-content() {
        @include d-block();
    }
}

Answer №2

Revised answer now includes the max-width breakpoint.

.see{outline:1px solid black;padding:1em;}
// BREAKPOINT
$breakpoints: (
  "xs": 575px,
  "sm": 576px,
  "md": 768px,
  "lg": 992px,
  "xl": 1200px
);

@mixin d-block() {
  display: block;
}

@mixin d-none() {
  display: none;
}

.d-block{
    @include d-block();
}

.d-none{
    @include d-none();
}

// Generate all breakpoints from content
@mixin generate-responsive-content() {

  // Responsive styles
  // Loop over each size
  @each $breakName, $width in $breakpoints {

    // Check breakpoint
    @if ($breakName == 'xs' ) {
      $breakName: '-' + $breakName;

      @media (max-width: $width) {
        &#{$breakName} {
          @content
        }
      }
    }
    @else if ($breakName != 'xs' ) {
      $breakName: '-' + $breakName;

      @media (min-width: $width) {
        &#{$breakName} {
          @content
        }
      }

    } @else {
      @content;
    }

  }
}

.d-block{
    @include generate-responsive-content() {
        @include d-block();
    }
}

.d-none{
    @include generate-responsive-content() {
        @include d-none();
     }
}

This is what is output:

.see {
  outline: 1px solid black;
  padding: 1em;
}

.d-block {
   display: block;
}


.d-none {
   display: none;
 }

@media (max-width: 575px) {
  .d-block-xs {
    display: block;
  }
}
@media (min-width: 576px) {
  .d-block-sm {
    display: block;
  }
}
@media (min-width: 768px) {
  .d-block-md {
    display: block;
  }
}
@media (min-width: 992px) {
  .d-block-lg {
    display: block;
  }
}
@media (min-width: 1200px) {
  .d-block-xl {
    display: block;
  }
}

@media (max-width: 575px) {
  .d-none-xs {
    display: none;
  }
}
@media (min-width: 576px) {
  .d-none-sm {
    display: none;
  }
}
@media (min-width: 768px) {
  .d-none-md {
    display: none;
  }
}
@media (min-width: 992px) {
  .d-none-lg {
    display: none;
  }
}
@media (min-width: 1200px) {
  .d-none-xl {
    display: none;
  }
}

UPDATED CODEPEN: View the updated code on Codepen: https://codepen.io/chrislafrombois/pen/JjdGKGJ

Here is the code snippet from the pen for reference:

.see {
  outline: 1px solid black;
  padding: 1em;
}

.d-block {
  display: block;
}

.d-none {
  display: none;
}

@media (max-width: 575px) {
  .d-block-xs {
    display: block;
  }
}

@media (min-width: 576px) {
  .d-block-sm {
    display: block;
  }
}

@media (min-width: 768px) {
  .d-block-md {
    display: block;
  }
}

@media (min-width: 992px) {
  .d-block-lg {
    display: block;
  }
}

@media (min-width: 1200px) {
  .d-block-xl {
    display: block;
  }
}

@media (max-width: 575px) {
  .d-none-xs {
    display: none;
  }
}

@media (min-width: 576px) {
  .d-none-sm {
    display: none;
  }
}

@media (min-width: 768px) {
  .d-none-md {
    display: none;
  }
}

@media (min-width: 992px) {
  .d-none-lg {
    display: none;
  }
}

@media (min-width: 1200px) {
  .d-none-xl {
    display: none;
  }
}
<div class="see">
  <span>I CANNOT SEE ANYTHING BECAUSE d-none OVERWRITE EVERYTHING</span>
  <div class="d-none d-none-xs d-block-sm d-block-md d-block-lg">
    CHECK CSS STYLE D-BLOCK-LG OVERWRITE EVERITHING
  </div>
</div>

Based on our discussion, it's advisable not to add the default d-none and d-block into this mixin. This approach ensures better separation of concerns, with defaults placed before the media query blocks.

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

Dropdown menu featuring a customizable input field

Is it possible to have a drop-down list with an input textbox field for creating new items in the same dropdown menu? ...

Is there a challenge in setting up a tag search bar similar to Stack Overflow's?

First and foremost, I apologize for the awkwardly phrased question. The issue at hand is that when the tags exceed the container size, the search option becomes hidden. My goal is to have the search bar adjust dynamically, moving everything back inside the ...

Attempting to align the text perfectly while also adding a subtle drop shadow effect

Trying to center text with a drop shadow without using the CSS property. I'm utilizing span and :before for cross-browser compatibility. Here is what it currently looks like: The HTML: <h3 title="Say hello to Stack Overflow"><span>Say ...

What is the best way to find out the size of a Google font file?

Google fonts are being utilized in my project, specifically the Roboto font for light and regular font styles. Unfortunately, I am unsure of the file size of this particular font. ...

The show more/show less link for a long jQuery paragraph is malfunctioning

I encountered an issue while coding where the "read more" link works correctly, but the "show less" link does not. Despite my efforts, I cannot seem to identify the error. Within this code snippet, there is an anchor tag with class="show-less" that I am u ...

In Vue, when you want to display text after reaching a height of 50px, any additional text will automatically be replaced by five full

https://i.stack.imgur.com/mp0YJ.png >>>>>>>>>Explore Sandbox Example <div style="height:50px"> ...

Guide on organizing items into rows with 3 columns through iteration

Click on the provided JSFiddle link to view a dropdown menu that filters items into categories. Each item is stored as an object in an array for its respective category. Currently, all items are displayed in one column and I want to divide them into three ...

Suitable HTML container for ASP form over HTML table

Can anyone advise on the best HTML container to use for an ASP form that can be styled with CSS instead of an HTML table? For example, consider the following form on an aspx page: <form id="WPF" runat="server"> <asp:Label ID="lblCCode" Text="Cou ...

Clicking on text within a DIV using jQuery

How can I use jQuery to show an "alert" when text inside a DIV is clicked, excluding images and other tags? The texts are contained within various DIVs along with images and tags. Is there a way to achieve this using jQuery? Check out the jsFiddle example ...

What could be the reason for the presence of a white border in the footer section?

I am trying to create a div in the footer that sits side by side, but the current code is causing it to have an unwanted white border. <!DOCTYPE html> <html lang="en"> <head> <style type="text/css"> #botto ...

The CSS specificity of a nested HTML element with no explicitly defined styles

Seeking clarification on this specific CSS cascading/inheritance rule. In the given example, I would have expected the text inside the em tag to be colored #000000, but I was informed that it would actually be colored #ff0000. I am familiar with CSS speci ...

Ways to arrange buttons vertically so they are perfectly aligned beneath one another

I'm trying to align a set of buttons, each containing an information icon, vertically under each other. The current alignment can be seen in image_1 below. Can someone please provide me with guidance on how to achieve this using CSS and HTML? I was t ...

Combining input elements and captchas in one line within Bootstrap

I am currently working on a form group that includes a label, a captcha image, and an input field for the captcha code. My goal is to have the label positioned at the top of the form group, with the captcha image and input field aligned in one line. I have ...

Can you please elaborate on how the CSS properties: -webkit-border-radius and border-radius are utilized?

https://i.sstatic.net/ILDTb.png What is the reason for having different values of 25px for -webkit-border-radius and border-radius in various positions? table.cal{ color: #064525c; border-spacing: 0; border: 3px solid black; -webkit-bor ...

What is the best way to adjust a fixed-width table to completely fill the width of a smartphone browser?

In an ancient web application, the design was primarily constructed using rigid tables: <div class="main"> <table width="520" border="0" cellspacing="0" cellpadding="0"> <tr> <td>...</td> </ ...

The contents table remains fixed in the top right corner as you scroll

I have developed an Angular app with a table-of-contents component that only displays two items. The code for the script is as follows: ts import { Component, OnInit } from '@angular/core'; import { pdfDefaultOptions } from 'ngx-extended-p ...

Tips for receiving notifications when the Collapsible collapses

I'm having trouble figuring out how to receive notifications when the Collapsible is expanded and collapsed. Currently, I am not receiving any type of notification. Any suggestions on how to make this work? Below is my code: --Imported jQuery < ...

The operation malfunctions if the variable input is below 50, causing it to produce inaccurate results

The function "calcFinalTotal()" was created to calculate the post-tax discount on a purchase of items that were previously totaled and stored in an input tag with the ID of "totaltaxamount". This function applies a 10% discount to orders between $50 to $ ...

Loading images with CSS media queries is an essential technique for optimizing

Currently, I am exploring options to dynamically load a background image based on the width of the browser window. This way, I can optimize bandwidth usage by only loading the image that will be displayed. I am aware that using the HTML picture tag allows ...

CSS: Achieving Layered Effect with Child Image Over Parent Background Image

I have set a background image on the .section class in my CSS stylesheet. The background also has an opacity effect applied to it. However, I am facing an issue where the child img (which is a transparent png) is displaying behind the background image of t ...