I have an app that customizes Bootstrap 3 stylesheets, by re-using Bootstrap variables and mixins.
My app used the Bootstrap 3 $grid-float-breakpoint
and $grid-float-breakpoint-max
variables in @media queries, to have ‘complex’ layout ‘collapse’ to something compact and small on a small screen.
This variable isn’t available in bootstrap 4 anymore. This post is about Bootstrap 4.3.0, and probably applies to Bootstrap 4.0.0 final too. But googling to try to figure out changes between Bootstrap 3 and 4, I find a lot of things written for one of the Bootstrap 4 alphas, sometimes just calling it “Bootstrap 4” — and in some cases things changed pretty substantially between alphas and final. So it’s confusing, although I’m not sure if this is one of those cases. I don’t think people writing “what’s changed in Bootstrap 4” blogs about an alpha release were expecting as many changes as there were before final.
Quick answer
If in Bootstrap 3 you were doing:
// Bootstrap 3
@media(max-width: $grid-float-breakpoint-max) {
// CSS rules
}
Then in Bootstrap 4, you want to use this mixin instead:
// Bootstrap 4
@include media-breakpoint-down(md) {
// CSS rules
}
In in Bootstrap 3, you were doing:
// Bootstrap 3
@media (min-width: $grid-float-breakpoint) {
// CSS rules
}
Then in Bootstrap 4, you want to do:
@include media-breakpoint-up(lg) { // CSS rules }
If you were doing anything else in Bootstrap 3 with media queries and $grid_float_breakpoint, like doing (min-width: $grid-float-breakpoint-max`) or (max-width: $grid-float-breakpoint), or doing any + 1
or - 1
yourself — you probably didn’t mean to be doing that, were doing the wrong thing, and meant to be doing one of these things.
One of the advantage of the new mix-in style, is that it makes it a little bit more clear what you are doing, how to apply a style to “just when it’s collapsed” vs “just when it’s not collapsed”.
What’s going on
Bootstrap 3
In Bootstrap 3, there is a variable `$grid-float-breakpoint`, documented in comments as “Point at which the navbar becomes uncollapsed.” It is by default set to equal the Bootstrap 3 variable `$screen-sm-min` — so we have an uncollapsed navbar at “sm” screen size and above, and a collapsed navbar at smaller than ‘sm’ screen size. screen-sm-min in Bootstrap 3 defaults to 768px.
For convenience, there was also a $grid-float-breakpoint-max
, documented as “Point at which the navbar begins collapsing” — which is a bit confusing to my programmer brain, it’s more accurate to say it’s the largest size at which the navbar is uncollapsed. (I would say it begins collapsing at $grid-float-breakpoint, one higher than $grid-float-breakpoint-max
).
$grid-float-breakpoint-max
is defined as ($grid-float-breakpoint - 1)
to make that so. So, yeah, $grid-float-breakpoint-max
is confusingly one pixel less than $grid-float-breakpoint
— kind of easy to get confused.
While documented as applying to the navbar, it was also used in default Bootstrap 3 styles in at least one other place, dropdown.scss, where I don’t totally understand what it’s doing, but is somehow changing alignment to something suitable for ‘small screen’ at the same place navbars break — smaller than ‘screen-sm’.
If you wanted to change the point of ‘breakdown’ navbars, dropdowns, and anything else you may have re-used this variable for — you could just reset the $grid-float-breakpoint
variable, it would now be unrelated to $screen-sm
size. Or you could reset $screen-sm
size. In either case, the change is now global to all navbars, dropdowns, etc.
Bootstrap 4
In Bootstrap 4, instead of just one breakpoint for navbar collapsing, hard-coded at the screen-sm
boundary, you can choose to have your navbar break at any of bootstrap’s screen size boundaries, using classes ‘.navbar-expand-sm’, ‘.navbar-expand-lg’, etc. ‘navbar-expand-sm’. You can now choose different breakpoints for different navbars using the same stylesheet, so long as they correspond to one of the bootstrap defined breakpoints.
‘.navbar-expand-sm` means “be expanded at size ‘sm’ and above’, collapsed below that.”
If you don’t put any ‘.navbar-expand-*’ class on your navar — it will always be collapsed, always have the ‘hamburger’ button, no matter how small the screen size.
And instead of all dropdowns breaking at the same point as all navbars at ‘grid-float-break, there are similar differently-sized responsive classes for dropdowns. (I still don’t entirely understand how dropdowns change at their breakpoint, have to experiment).
In support of bootstrap’s own code creating all these breakpoints for navbars and dropdowns, there is a new set of breakpoint utility mixins. These also handily make explicit in their names “do you want this size and smaller” or “do you want this size and larger”, to try to avoid the easy “off by one” errors using Bootstrap 3 variables, where a variable name sometimes left it confusing whether it was the high-end of (eg) md
or the low-end of md
.
You can also use these utility mixins yourself of course! breakpoint-min(md)
will be the lowest value in pixels that is still “md” size. breakpoint-min(xs)
will return sass null value (which often converts to an empty string), because “xs” goes all the way to 0.
breakpoint-max(md)
will return a value with px
units, that is the largest pixel value that’s within “md” size. breakpoint-max(xl)
will return null/””, because “xl” has no max value, it goes all the way up to infinity.
Or you can use the mixins that generate the actual media queries you want, like media-breakpoint-up(sm)
(size “sm” and up), or media-breakpoint-down(md)
(size ‘md’ and down). Or even the handy media-breakpoint-between(sm, lg)
(small to large, inclusive; does not include xs
or xl
.)
Some Bootstrap 4 components still have breakpoints hard-coded to a certain responsive size, rather than the flexible array of responsive breakpoint classes. For instance a card has a collapse breakpoint at the bottom of ‘sm’ size, and there’s no built-in way to choose a different collapse breakpoint. Note how the Bootstrap source uses the media-breakpoint-up
utility to style the ‘card’ collapse breakpoint.
Bootstrap 4 responsive sizes shift by one from Bootstrap 3!
To make things more confusing, ‘sm’ in bootstrap 3 is actually ‘md’ in bootstrap 4.
- Added a new sm grid tier below 768px for more granular control. We now have xs, sm, md, lg, and xl. This also means every tier has been bumped up one level (so .col-md-6 in v3 is now .col-lg-6 in v4)
—https://getbootstrap.com/docs/4.0/migration/
In Bootstrap 3, ‘sm’ began at 768px. In Bootstrap 4, it’s md that by default begins at 768px. And there’s a new ‘sm’ inserted below 768 — in Bootstrap 4 sm
by default begins at 576px.
So that’s why to get the equivalent of Bootstrap 3(max-width: $grid-float-breakpoint-max)
, where $grid-float-breakpoint
was defined based on “screen-sm-min” in Bootstrap 3 (smaller than ‘sm’) — in bootstrap 4 we need to use md
instead — media-breakpoint-down(md)
.
Customizing breakpoints in Bootstrap 4
The responsive size breakpoints in bootstrap 4 are defined in a SASS ‘map’ variable called grid-breakpoints. You can change these breakpoints, taking some care to mutate the ‘map;’ without removing default values, if you that is your goal.
If you change them there, you will change all the relevant breakpoints, including the grid utility classes like col-lg-2
, as well as the collapse points for responsive classes for navbars and dropdowns. If you change the sm
breakpoint, you’ll change the collapse breakpoint for card
for instance too.
There’s no way to only change the navbar/dropdown collapse breakpoint, as you could in Bootstrap 3 with $grid-float-breakpoint
. On the other hand, you can at least hypothetically (I haven’t tried it or seen it documented) add additional breakpoints if you want, maybe you want something in between md and large, called, uh, I don’t know what you’d call it that wouldn’t be confusing. But in theory all the responsive utilities should work with it, the various built-in *-md-*
etc classes should now be joined by classes for your new one (since the built-in ones are generated dynamically), etc. I don’t know if this is really a good idea.