Utilizing CSS specificity to tailor custom classes according to Bootstrap grid breakpoints

My goal is to create custom width classes that leverage Bootstrap's grid breakpoints. These classes, such as w-10, w-20, would represent widths of 10% and 20% respectively. Alternatively, classes like w-sm-10, w-sm-20 would specify widths of 10% & 20% on screens wider than the 'sm' breakpoint (576px), falling back to their default width on mobile.

Through this code snippet, I have successfully generated classes with this behavior:

@import 'node_modules/bootstrap/scss/bootstrap.scss';

$i : 10;
@while $i <= 90 {
  .w-#{$i} {              
    width: #{"#{$i}%"};   
  }
  @each $bp-name, $bp-pixels in $grid-breakpoints {   
    .w-#{$bp-name}-#{$i} {  
      width: #{"#{$i}%"};
    }
    @media screen and (max-width: $bp-pixels) {       
      .w-#{$bp-name}-#{$i} {
        width: initial;
      }
    }
  }
  $i : $i + 10;
}

While this approach works when applying a single class to elements, it breaks down when multiple classes are combined due to lack of specificity:

For example, the class assignment

<div class='w-lg-50 w-sm-70 w-90'></div>
should result in widths of 50% for lg screens, 70% for sm screens, and 90% otherwise. However, the resulting width remains fixed at 90% regardless of screen size.


I've attempted to understand CSS specificity through resources like W3Schools, but it still eludes me.

Answer №1

Swap out max-width for min-width in your media query. This means that if your element is at least a certain width, then apply the specified attribute.

Additionally, when using 2 loops, the classes like w-40, w-60 are defined before classes like w-sm-40, w-md-40... This leads to the sm and md classes taking precedence over the earlier classes.

Similarly, classes like xs are defined before sm classes, allowing the sm classes to take priority over the xs classes, followed by md, lg, and xl

//Generating width classes such as w-40, w-60 ...
$i : 10;
@while $i <= 90 {
  .w-#{$i} {              //Class names like w-40, w-60
    width: #{"#{$i}%"};   
  }
  $i : $i + 10;
}

@each $bp-name, $bp-pixels in $grid-breakpoints {
  $i : 10;
  @while $i <= 90 {
    @media (min-width: $bp-pixels){ 
      .w-#{$bp-name}-#{$i} {  
          width: #{"#{$i}%"};
      }
    }
    $i : $i + 10;
  }
}

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

What is the method for setting a cell's row-span to 2 using bootstrap?

Is there a way to make the red cell in a Bootstrap grid have a row-span=2 and fill the second row as well? .cell {height: 100px; border: 1px solid; border-collapse:collapse;} .red {background: red;} .yel {background: lightyellow;} <link href="https:/ ...

Bootstrap 4 limitation with Internet Explorer versions 8 and 9

When using bootstrap 4 with an MVC application, I encountered compatibility issues on Internet Explorer 8 or 9. Even after adding html shiv and <meta http-equiv="X-UA-Compatible" content="IE=edge">, all elements appear jumbled. Is there a workaround ...

Packages have gone astray post node_modules scrub

I encountered an issue with npm run watch getting stuck at 10%, so I took the step of deleting the node_modules directory and package-lock.json. However, it seems that I may have installed modules using npm install without the --save-dev option. Even after ...

What is the process for closing the lightbox?

My code is supposed to display a lightbox as soon as the page loads, similar to a popup window. However, there seems to be an issue with closing the lightbox when clicking on the 'x' button at the corner of the box. Unfortunately, the current cod ...

What is the best way to create a hover effect for this text?

When I hover over the image div, the text section will smoothly appear from the bottom. You can see an example of what I want here. Below, you'll find the HTML and CSS code I've implemented for this effect. ============================== ...

What steps should I follow to customize the Bootstrap link template according to my requirements?

In the bootstrap documentation, the links are shown in blue by default. I want them to be white instead. How can I make all the links white if the documentation is ignoring my CSS files? ...

What could be causing my anchored link to redirect incorrectly?

My navigation bar correctly directs me to the 'work' section, but when I click on ABOUT in the navigation bar, it drops down about 300px above the 'about' h2. I suspect that this issue may be related to positioning and display settings. ...

implementing HtmlSpanner with an appended css stylesheet

Using HtmlSpanner with CSS for TextView I recently discovered a library called HtmlSpanner that claims to assist in adding an HTML string with CSS to a TextView. However, I am struggling to locate any detailed documentation on how to achieve this, except ...

Flexbox wrap allows you to determine which child element will move to the next line

Is there a way to control which child element in flexbox drops to the next line, instead of just the last one every time? For example: Full-size, no breaking: https://i.sstatic.net/proUB.png Default behavior, breaking the last element: https://i.sstatic.n ...

How to Align Two Headers Vertically Next to Each Other Using HTML and CSS

Looking for a solution for aligning the orange date: Here is the current setup: Check out the JsFiddle example: http://jsfiddle.net/N4gpA/ (Including code as jsfiddle-only is not permitted) HTML: <div class="Box1"><a href="#"&g ...

I am struggling to run the jQuery animation that adjusts the width more than once

My goal is to allow the user to expand and retract a <div> element upon clicking. The desired behavior is for the <div> to expand when clicked, and then retract back to its original width when clicked again. However, when testing this function ...

Issue with CSS min-height causing divs to not align properly

My CSS Code includes media queries to adjust the layout: .clearfloat { clear: both; font-size: 1px; height: 0; line-height: 0; } .box-container { width:100%; text-align:center; } .icon-box { width:32%; max-width:500px; ...

Troubleshooting challenges arise with Bootstrap's button group spacing issue

I am encountering spacing issues with the markup provided below. <h2>Profitability Report</h2> <form method="post"> <div class="row"> <div class="col-md-12"> <div class="btn-group btn-group-toggle ...

Utilizing a background image in the slick slider

<div id="largeCarousel" style="display:inline-block; float:right;"> <div class="homepage-item" style="padding-bottom:52%; position:relative; box-sizing:border-box;"> <div id="largeCarouselContent" style="position:absolute ...

The drop-down menu keeps flickering instead of remaining open when clicked

I am facing an issue with a dropdown menu in my webpage. The menu is initially hidden, but should become visible when the list element containing it is clicked. I have written JavaScript code to add a class that changes the visibility property to visible u ...

Ways to create adaptive images using Bootstrap 5

On my website, there are some cards that look great on large and medium screens. However, on small screens, the images appear stretched vertically! I am using Bootstrap 5 and here is the code snippet: <div class="col-12 mb- ...

Is the first child component of Material UI Stack not properly aligned?

Is there a CSS issue that needs fixing? I am working on creating a vertical list of checkboxes with labels using the <Stack /> component from Material UI. I have tried implementing this in the sandbox provided (check out demo.tsx): https://codesandb ...

Main content with a floating aside menu

I am currently using the CoreUI admin template. The layout I have set up is the basic one outlined on the following page: My goal is to make the aside menu appear on the right side of the main content, floating over it rather than inline and reducing the ...

Enhancing a JQuery progress bar with customized text and dynamic background color updates

I am considering implementing the Jquery progress bar to display the user's advancement in a course. Within the progress bar, I aim to exhibit the text Course Progress 70% Additionally, I wish to alter the default grey color of the progress bar Thi ...

Lately, I've been working on a practice website I'm developing, and I'm facing some issues with my hyperlinks

Get the omisphere download I created this hyperlink because my previous attempts didn't work, but unfortunately, this one still isn't functioning properly. Is it possible that the issue is with my CSS? ...