DEV Community

Cover image for Responsive Size Classes in Bootstrap
Dharmen Shah
Dharmen Shah

Posted on • Updated on • Originally published at

Responsive Size Classes in Bootstrap

This article was originally published on


I would recommend you to read the full article, but if you simply want the code, go to final section of this post.


So, in my previous article, we saw how we can create custom checkboxes and radio buttons. Wait, I hope you've read that article, if not check out now:

It was really cool and easy to do so, right!? So, now we are going to learn how we can create responsive size classes, using Bootstrap's scss mixins and variables.

Size Classes

According to Bootstrap's documentation, we have some height and width classes, which gives size relative to the parent. For reference, they're listed in below images:

Bootstrap's default width classes
Bootstrap's default height classes


As you know, bootstrap already has responsive spacing classes, you can check them out here.

What if we need the same responsiveness in our size classes? For example, w-sm-50, h-lg-25, w-md-0, etc.?

Don't worry...

Alt Text

GIF from Giphy


Before we actually go to code, we need to understand some of the functions, mixins, and variables of Bootstrap, which we will use to create our custom classes.

  1. Variable: $grid-breakpoints: If you check Bootstrap's _variables.scss file, they have $grid-breakpoints map, which has all the min-widths for each viewport size. Basically, this map is responsible for how Bootstrap works on different widths of the viewport.
    $grid-breakpoints in Bootstrap's _variables.scss

  2. Variable $sizes: In the same file, $sizes map is responsible for giving relative % values to height and width classes.
    $sizes in _variables.scss

  3. @function breakpoint-infix: In the _breakpoint.scss file, this function returns the viewport name with a dash in front. This will be helpful to create responsive classnames.
    @function breakpoint-infix in _breakpoint.scss

  4. @mixin media-breakpoint-up: Now in the same file, note the mixin media-breakpoint-up and from the image below, it'll be pretty clear what it does:
    @mixin media-breakpoint-up in _breakpoint.scss

Let's come to the point

Create a file called _sizing.scss, I would prefer you to create it inside the utilities folder, and make sure to @import it after ~bootstrap.scss in your main style file.

Want a quick and handy setup for Bootstrap with gulp and sass (and much more, like sample theme file, 7-in-1 architecture, etc.)? Refer to my Github repo bootstrap-theme-kit:

GitHub logo shhdharmen / bootstrap-theme-kit

Quickly generate and showcase your bootstrap theme.

Bootstrap Theme Kit


Quickly Generate and Showcase 🎯 your bootstrap theme 🎨. Get Started or See sample theme.

GitHub license Commitizen friendly

🚀 Getting Started

☑️ Minimum Requirements

node -v
// v10.17.0
git --version
// git version 2.x
Enter fullscreen mode Exit fullscreen mode

⬇️ Steps to Follow

  1. First, fork this repo.
  2. Open terminal and:
git clone <forked-repo-url>
cd bootstrap-theme-kit
npm i
npm run init
npm start
Enter fullscreen mode Exit fullscreen mode
  1. Browser will open at 3000 port.
  2. Start editing your scss/html files and browser will reload.

🏆 Features

So, let's make use of those variable and mixins to create responsive size classes:

Final code

Please don't hate me 🙏🏽 for uploading the image of a code snippet. It's just that I really want you to write this code ✏️ by yourself 😉.

Let's understand what's going on by line numbers:

  1. Line no. 4: We are looping through each member of $grid-breakpoints
  2. Line no. 5: We calling the mixin media-breakpoint-up for each member of $grid-breakpoints
  3. Line no. 6: We reading viewport name (returns a blank string if smallest breakpoint, otherwise returns the name with a dash in front) for each member of $grid-breakpints
  4. Line no. 8: Now, we are looping through a static patterned map of value: abbreviation.
  5. Line no. 9: It goes through each member of $sizes map, this not only helps us to create class names, but it will also give us % values for assignment to properties.
  6. Line no. 10-12: These lines generate the classes, with property and value. For example, it generates classes like below. We are using interpolation for using scss variables in class names and property names.
// for breakpoint value `sm` and size of `25`, it will generate like below:
.h-25 {
  height: 25% !important;
.w-25 {
  height: 25% !important;

// for breakpoint value `md` and size of `75`, it will generate like below:
@media (min-width: 768px) {
  .w-md-75 {
    width: 75% !important;
  .h-md-75 {
    height: 75% !important;
Enter fullscreen mode Exit fullscreen mode

I know it's too much to grasp in the first go, but just try to use it in multiple projects and you will find it easy.

For homework, try to generate colored background and font classes. Bootstrap provides contextual classes like bg-primary and text-primary. Try to generate specific color classes like: bg-blue, text-blue, bg-indigo, text-indigo, etc. Hint: try to utilize @each and $colors map from Bootstrap's _variables.scss file.

That's it for now, we will see you next time. Till then, let me know your thoughts and feedback in the comment section. And if you liked this article, I hope you will like my other articles, too:

And yes, always believe in yourself.

believe in yourself

Photo by Katrina Wright on Unsplash
Cover Photo by Giorgio Trovato on Unsplash

Top comments (0)