5

diagram

Is there a way to accomplish the above using Bootstrap, where 3 columns are shown if the screen is wide enough, but only the first and last columns are shown on a smaller screen (like mobile)?

5 Answers 5

7

you dont need col-xs-12 at all - by default all content that does not have an -xs- designation will be displayed full width on a mobile (ie:xs size). All you need to do is designate the columns on larger viewports than xs. Note that there is a -sm- class as well so if you want to go 2 across on small viewports (still hiding div 2 on small screens):

<div class="col-md-4 col-sm-6">Column 1</div>
<div class="col-md-4 hidden-xs hidden-sm">Column 2</div>
<div class="col-md-4 col-sm-6 ">Column 3</div> 

both of these will display full width on the xs size, but will display div 1 and 3 beside each other on small viewports and all three horizontally on medium and larger screens. you can also add a col-lg- in if you want a different display on large screens.

EDIT - as per @Siavas comments - this solution requires Bootstrap 3 (link - https://getbootstrap.com/docs/3.3/css/#grid-example-basic)

3
  • *-xs-* column classes are required for proper padding. By default, a gutter width of 15 px is added on each side – omitting it will make the column take the entire space and will therefore not adhere to the guidelines.
    – Siavas
    Commented Dec 12, 2018 at 17:42
  • Sorry @Siavas - that is not true - bootstrap applies padding-left and right in a separate style rule to the min-width. This means the above code retains the 15px to either side - even at xs size. The width calculations (8.333% of the viewport size are in media queries and are separate to the padding style rules. This means that the approach taken in the solution will still have hte left and right padding, but there will not be a width constraint - meaning the width is 100%. - eg: .col-md-4 { position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; }
    – gavgrif
    Commented Dec 12, 2018 at 22:08
  • Indeed @gavgrif, have just checked and I agree! I remember when this was not applicable, but it is in 3.3.7 so sorry for that. Would you mind editing your post for adding this Bootstrap 3 docs link to it? Would like to make a rectification as well :)
    – Siavas
    Commented Dec 12, 2018 at 22:17
4

Use class as this to your column divs : col-md-4 col-xs-12. So in case of laptop or desktop screen, it will display as three columns. However, in case of mobile view they will collapse to one.

<div class="col-md-4 col-xs-12">Column 1</div>
<div class="col-md-4 col-xs-12">Column 2</div>
<div class="col-md-4 col-xs-12">Column 3</div>
3

You should read about responsive utilities.

For faster mobile-friendly development, you can use these utility classes for showing and hiding content by a device via media query. Also included are utility classes for toggling content when printed.

Demo https://jsfiddle.net/nanilab/zx22szz7/

<div class="row">
  <div class="col-md-4 col-xs-12"> Col 1 </div>
  <div class="col-md-4 hidden-xs"> Col 2 </div>
  <div class="col-md-4 col-xs-12"> Col 3 </div>
</div>
2

In Bootstrap 4 the hidden/visible properties got stripped for the display properties. The link might help in combination with "gavgrifs" answer. Missing visible-** and hidden-** in Bootstrap v4

This is an example for 3 equally sized colums. The middle column not displayed for xs and sm displays.

<div class="row">
    <div class="col"> Col 1 </div>
    <div class="col d-none d-md-block"> Col 2 </div>
   <div class="col"> Col 3 </div>
</div>
1

add additional classes in the <div> where you have classes like col-md- or col-lg- and just add col-xs-12 over there and you will have full screen on smaller screens

Not the answer you're looking for? Browse other questions tagged or ask your own question.