Responsive Size Classes in Bootstrap

Responsive Size Classes in Bootstrap

ยท

4 min read

In this article, I will show you how to easily create responsive size classes using Bootstrap's scss mixins.

TL;DR

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

Background

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 below link:

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

Responsiveness

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...

We got this...

Basics

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:

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;
  }
}

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.

And yes, always believe in yourself.

katrina-wright-yMg_SMqfoRU-unsplash (1).jpg

Photo by Katrina Wright on Unsplash

Did you find this article valuable?

Support Dharmen Shah by becoming a sponsor. Any amount is appreciated!