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:
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...
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.
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.
Variable $sizes: In the same file, $sizes map is responsible for giving relative % values to height and width classes.
@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.
@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:
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:
Let's understand what's going on by line numbers:
Line no. 4: We are looping through each member of $grid-breakpoints
Line no. 5: We calling the mixin media-breakpoint-up for each member of $grid-breakpoints
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
Line no. 8: Now, we are looping through a static patterned map of value: abbreviation.
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.
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.
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: