This article was originally published on blog.shhdharmen.me
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 now:
Create custom checkboxes and radios in Bootstrap
Dharmen Shah ใป Feb 20 '20
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:
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.
๐ Getting Started
โ๏ธ Minimum Requirements
node -v
// v10.17.0
git --version
// git version 2.x
โฌ๏ธ Steps to Follow
- First, fork this repo.
- Open terminal and:
git clone <forked-repo-url>
cd bootstrap-theme-kit
npm i
npm run init
npm start
- Browser will open at 3000 port.
- Start editing your scss/html files and browser will reload.
๐ Features
- Of course, Bootstrap
- ๐ Default and ๐ Dark themes
- Gulp
- SCSS
-
SCSS-7-in-1
- Pro tip: Quickly generate SCSS 7-in-1 architecture anywhere using npx scss-7-in-1
- Live reload with Browsersync
- Linting and Formatting
-
Commitzen Friendly
-
Pro tip: After staging your files, use
npm run commit
to make commit messages commitzen friendly.
-
Pro tip: After staging your files, use
- Changelog and Version Management with Semantic Release
- โฆ
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.
// 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. 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:
Create custom checkboxes and radios in Bootstrap
Dharmen Shah ใป Feb 20 '20
Dribbble Challenge 02: Birthday list card
Dharmen Shah ใป Mar 6 '20
And yes, always believe in yourself.
Top comments (0)