In Bootstrap v3 I often use the hidden-** classes combined with clearfix to control multi column layouts at different screen widths. For example,
I could combine multiple hidden-** in one DIV to make my multi columns appear correctly at different screen widths.
As an example if I wanted to display rows of product photos, 4 per row on larger screen sizes, 3 on smaller screens, then 2 on very small screens. The product photos might be different heights so I need the clearfix to ensure the row breaks properly.
Here's an example in v3...
http://jsbin.com/tosebayode/edit?html,css,output
Now that v4 has done away with these classes, and replaced them with the visible/hidden-**-up/down classes I seem to have to do the same thing with multiple DIVs instead.
Here's a similar example in v4...
http://jsbin.com/sagowihowa/edit?html,css,output
So I've gone from single DIVs to having to add multiple DIVs with lots of up/down classes to achieve the same thing.
From...
<div class="clearfix visible-xs-block visible-sm-block"></div>
to...
<div class="clearfix hidden-sm-up"></div>
<div class="clearfix hidden-md-up hidden-xs-down"></div>
<div class="clearfix hidden-md-down"></div>
Is there a better way of doing this in v4 that I have overlooked?
Unfortunatly all classes
hidden-*-up
andhidden-*-down
were removed from Bootstrap (as of Bootstrap Version 4 Beta, in Version 4 Alpha and Version 3 these classes still existed).Instead, new classes
d-*
should be used, as mentioned here: https://getbootstrap.com/docs/4.0/migration/#utilitiesI found out that the new approach is less useful under some circumstances. The old approach was to HIDE elements while the new approach is to SHOW elements. Showing elements is not that easy with CSS since you need to know if the element is displayed as block, inline, inline-block, table etc.
You might want to restore the former "hidden-*" styles known from Bootsrap 3 with this CSS:
The classes
hidden-unless-*
were not included in Bootstrap 3, but they are useful as well and should be self-explanatory.Bootstrap v4.1 uses new classnames for hiding columns on their grid system.
For hiding columns depending on the screen width, use
d-none
class or any of thed-{sm,md,lg,xl}-none
classes. To show columns on certain screen sizes, combine the above mentioned classes withd-block
ord-{sm,md,lg,xl}-block
classes.Examples are:
More of these here.
Unfortunately these new bootstrap 4 classes do not work like the old ones on a div using
collapse
as they set the visible div toblock
which starts out visible rather than hidden and if you add an extra div around thecollapse
functionality no longer works.I do no expect that multiple div's is a good solution.
I also think you can replace
.visible-sm-block
with.hidden-xs-down
and.hidden-md-up
(or.hidden-sm-down
and.hidden-lg-up
to act on the same media queries).hidden-sm-up
compiles into:.hidden-sm-down
and.hidden-lg-up
compiles into:Both situation should act the same.
You situation become different when you try to replace
.visible-sm-block
and.visible-lg-block
. Bootstrap v4 docs tell you:http://v4-alpha.getbootstrap.com/layout/responsive-utilities/
You now have to define the size of what is being hidden as so
Will hide anythinging from xs and smaller, only xs
Will hide everything
Update for Bootstrap 4 (2018)
The
hidden-*
andvisible-*
classes no longer exist in Bootstrap 4. If you want to hide an element on specific tiers or breakpoints in Bootstrap 4, use thed-*
display classes accordingly.Remember that extra-small/mobile (formerly
xs
) is the default (implied) breakpoint, unless overridden by a larger breakpoint. Therefore, the-xs
infix no longer exists in Bootstrap 4.Show/hide for breakpoint and down:
hidden-xs-down (hidden-xs)
=d-none d-sm-block
hidden-sm-down (hidden-sm hidden-xs)
=d-none d-md-block
hidden-md-down (hidden-md hidden-sm hidden-xs)
=d-none d-lg-block
hidden-lg-down
=d-none d-xl-block
hidden-xl-down
(n/a 3.x) =d-none
(same ashidden
)Show/hide for breakpoint and up:
hidden-xs-up
=d-none
(same ashidden
)hidden-sm-up
=d-sm-none
hidden-md-up
=d-md-none
hidden-lg-up
=d-lg-none
hidden-xl-up
(n/a 3.x) =d-xl-none
Show/hide only for a single breakpoint:
hidden-xs
(only) =d-none d-sm-block
(same ashidden-xs-down
)hidden-sm
(only) =d-block d-sm-none d-md-block
hidden-md
(only) =d-block d-md-none d-lg-block
hidden-lg
(only) =d-block d-lg-none d-xl-block
hidden-xl
(n/a 3.x) =d-block d-xl-none
visible-xs
(only) =d-block d-sm-none
visible-sm
(only) =d-none d-sm-block d-md-none
visible-md
(only) =d-none d-md-block d-lg-none
visible-lg
(only) =d-none d-lg-block d-xl-none
visible-xl
(n/a 3.x) =d-none d-xl-block
Demo of the responsive display classes in Bootstrap 4
Also, note that
d-*-block
can be replaced withd-*-inline
,d-*-flex
,d-*-table-cell
,d-*-table
etc.. depending on the display type of the element. Read more on the display classes