86

I'm getting started on Bootstrap 3 and I'm having some trouble understanding how the grid classes are meant to be used.

Here's what I've figured out so far:

It appears that the classes col-sm-# and col-lg-# differ from plain old col-# in that they will only apply when screens are above a certain size (768px and 992px respectively). If you omit the -sm- or -lg- the divs will never collapse into one column.

However, when I create two divs inside a row that are both col-sm-6 it seems they are only side by side when the window is between 768px and 992px wide. In other words, if I shrink the window all the way down and then slowly widen it, the layout is single column, then two columns, then back to single column again.

  1. Is this the intended behavior?
  2. If I want two columns for anything over 768px, should I apply both classes? (<div class="col-sm-6 col-lg-6">)
  3. Should col-6 also be included? <div class="col-6 col-sm-6 col-lg-6">
emersonthis
  • 32,822
  • 59
  • 210
  • 375
  • Read the table: http://getbootstrap.com/css/#grid #1 - It is the intended behavior because uses `@media` for specific sizes. #2 and #3 yes, read 'Example: Combining mobile with desktop' and 'Example: Mobile, tablet, and desktop' – RaphaelDDL Aug 09 '13 at 12:39
  • @RaphaelDDL Thanks. I figured it out shortly after posting. I think I was expecting v3 to behave like the v2's "bootstrap-responsive.css" and that's not the case. – emersonthis Aug 09 '13 at 12:46
  • see also: http://stackoverflow.com/questions/17919225/twitters-bootstrap-3-grid-changing-breakpoint-and-removing-padding/17920693 – Bass Jobsen Aug 09 '13 at 22:31

6 Answers6

59

[UPDATE BELOW]

I took another look at the docs and it appears I overlooked a section which talks specifically about this.

The answers to my questions:

  1. Yes, they are meant to apply only to specific ranges, rather than everything above a certain width.

  2. Yes, the classes are meant to be combined.

  3. It appears that this is appropriate in certain cases but not others because the col-# classes are basically equivalent to col-xsm-# or, widths above 0px (all widths).

Other than reading the docs too quickly, I think I was confused because I came into Bootstrap 3 with a "Bootstrap 2 mentality". Specifically, I was using the (optional) responsive styles (bootstrap-responsive.css) in v2 and v3 is quite different (for the better IMO).

UPDATE for stable release:

This question was originally written when RC1 was out. They made some major changes in RC2 so for anyone reading this now, not everything mentioned above still applies.

As of when I'm currently writing this, the col-*-# classes DO seem to apply upwards. So for example, if you want an element to be 12 columns (full width) for phones, but two 6 columns (half page) for tablets and up, you would do something like this:

<div class="col-xs-12 col-sm-6"> ... //NO NEED FOR col-md-6 or col-lg-6

(They also added an additional xs break point after this question was written.)

emersonthis
  • 32,822
  • 59
  • 210
  • 375
  • The hyperlink appears to point to a nonexistent page currently. I'm not sure if it should be updated to point to the link that @RaphaelDDL provided above -- http://getbootstrap.com/css/#grid -- or to some other link? – Jon Schneider Sep 25 '13 at 13:05
  • Thanks for the question and the answer. I'm not a css guy, and the grid setup was confusing to me at first as well. But if if what you are saying is true, it seems better to just use xs and let everything apply upward. – Mike Purcell Feb 05 '14 at 23:21
39

Here you have a very good tutorial, that explains, how to use the new grid classes in Bootstrap 3.

It also covers mixins etc.

trejder
  • 17,148
  • 27
  • 124
  • 216
Srikanth
  • 586
  • 4
  • 11
  • 2
    The blog link describes Bootstrap 3's Grid in detail including the 4 sizing classes such as col-xs-*. Has good code examples. – Catto Mar 06 '14 at 16:18
  • 2
    It's very good, especially his style of constantly encouraging you to persevere; _"Useful right? No? Don’t know? Let’s keep going!"_ as well explaining things in proper sentences: _"This basically says “keep me at a 6 column width all the way down to the phone size, don’t ever switch me to the stacked mobile layout.”"_. Thanks for posting – slugmandrew Dec 03 '14 at 11:06
5

"If I want two columns for anything over 768px, should I apply both classes?"

This should be as simple as:

<div class="row">
      <div class="col-sm-6"></div>
      <div class="col-sm-6"></div>    
</div>

No need to add the col-lg-6 too.

Demo: http://www.bootply.com/73119

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
  • For reasons I cannot explain yet, it's not working this way for me. `.col-sm-6` only seems to apply to that specific range of widths (not everything above) – emersonthis Aug 12 '13 at 14:32
5

The best way to understand is to simply think from top to bottom ( Large Desktops to Mobile Phones)

Firstly, as B3 is mobile first so if you use xs then the columns will be same from Large desktops to xs ( i recommend using xs or sm as this will keep everything the way you want on every screen size )

Secondly if you want to give different width to columns on different devices or resolutions, than you can add multiple classes e.g

the above will change the width according to the screen resolutions, REMEMBER i am keeping the total columns in each class = 12

I hope my answer would help!

Aslam
  • 9,204
  • 4
  • 35
  • 51
5

To amend SDP's answer above, you do NOT need to declarecol-xs-12 in <div class="col-xs-12 col-sm-6">. Bootstrap 3 is mobile-first, so every div column is assumed to be a 100% width div by default - which means at the "xs" size it is 100% width, it will always default to that behavior regardless of what you set at sm, md, lg. If you want your xs columns to be not 100%, then you normally do a col-xs-(1-11).

helloerik
  • 175
  • 1
  • 2
  • 7
0

This might be late as I think most of us are using BS4. This article explained all the questions you asked in a detailed and simple manner also includes what to do when. The detailed guide to use bs4 or bootstrap

https://uxplanet.org/how-the-bootstrap-4-grid-works-a1b04703a3b7

AAA
  • 35
  • 2
  • 12
  • consider putting the relevant info from your link in the space provided for answer. – sao Apr 02 '20 at 13:44
  • The only reason I included the link was that it was helpful for me to start reading the article from the top. That's when it makes sense. – AAA Apr 02 '20 at 14:10