Understanding the grid classes ( col-sm-# and col-lg-# ) in Bootstrap 3 - html

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.
Is this the intended behavior?
If I want two columns for anything over 768px, should I apply both classes? (<div class="col-sm-6 col-lg-6">)
Should col-6 also be included? <div class="col-6 col-sm-6 col-lg-6">

[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:
Yes, they are meant to apply only to specific ranges, rather than everything above a certain width.
Yes, the classes are meant to be combined.
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.)

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

"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

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!

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).

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

Related

Unable to understand the combination of Bootstrap Grids for various screen sizes

I found one code snippet on codeply.com and modified a bit to understand the combination of col-xl, col-lg, col-sm and col-xs. Here is the code snippet.
http://www.codeply.com/go/bipCPFM4mU
I am a newbie to Bootstrap. So, please let me know if the code snippet has been implemented in a wrong way and its output might be unpredictable. That being said here are my questions:
For the last 3 divs (div for 1.1 .. 4.1, div for 1.2 .. 4.2 and div for 1.3 .. 5.3), there are two xl specification i.e. col-xl-3 and col-xl-6 then why does it take only col-xl-6 into consideration not col-xl-3 (for xl screens)? I tried to reverse the order but there was no difference in the output. All *.1 sections and *.2 sections were on the first row and then *.3 were on the second row.
When you open the output in another window and resize the width to make the screen size to lg, then 1.3, 2.3, 3.3 and 4.3 disappear. And, how do the rest of the elements figure out the column size. I am assuming that they take on the full width because I haven't specified anything for col-lg.
I hope my questions make sense. I have tried looking at a number of examples but it does not answer my questions and help me understand why is it behaving the way it is behaving.
Can someone please help?
Thank you,
Jayant
You can't include 2 declarations in the same size category - xl, so declaring 3 columns and 6 columns at the xl width will always result in just one as they are just width declarations really. I think it's probably a typo and should be col-xl-3 col-lg-6 col-sm-12.
They haven't disappeared, they are just hidden by 5.3 which is full width at that screen size and floating in front of them. If you contain 5.3 in a class="row" or give it a clear:both style you will see 1.3 - 4.3 again.
Bootstrap layout is hard to get your head around initially until it clicks.
Hope that helps :)

Difference between declaring multiple classes or single class for columns in bootstrap framework

What is the difference in between below two methods?
Method #1
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
</div>
Method #2
<div class="col-md-12">
</div>
I'm using the second method and it works for all small and large devices perfectly then when do i need the first method?
This is a good question. I see a few others have answered it already, but I'd like to go a little bit more in depth in case you're curious.
In method 2, you're specifically saying "I want this container to take up the full 12 at 970px and above. Since it's a full 12, though, it will retain the full-width 12 all the way down to the smallest resolution. If, however, you had two col-md-6's, the 6's would become 12's when the screen goes below 970px due to the use of md.
In your first method, you're explicitly stating that you want the column to be the full 100% at all the breakpoints.
To answer your question "Why do I need the first method?", you simply don't. The first method is overkill since bootstrap will make them all 100% by default anyways. The only reason you would want to use something like the first method is if you want to show the content in different layouts at different resolutions.
For example:
<div class="col-sm-6 col-md-3">
</div>
This would make the column a 3 at all resolutions 992px and above (because of the md). By default, bootstrap would make the column a full 12 below 992px using the col-md-3 class but with the declaration of col-sm-6, you're overwriting it and making it a 6 on all resolutions between 768px and 992px. Everthing below 768px (col-xs-*) would still make the column a full 12. You could overwrite this by declaring a 'col-xs-*' class just as you did with the 'col-sm-6'
If a smaller resolution is not explicitly stated, it's going to be a 12. Anything you declare will apply to that resolution and above with the exception of the xs. xs will declare 768px and below.
Here is the official bootstrap documentation to show the grid options and their associated resolutions:
https://getbootstrap.com/css/#grid-options
Here's a bootply to help visualize this:
http://www.bootply.com/c0KgRryXcl
I hope that helps!
You won't notice a difference with col-md-12 as it's the full width of the container, but if you had 2 col-md-6's, they would look squished together on a small device, hence why you'd use col-xs-12, then the breakpoint would kick in and it would change from 50% to 100%.
The first method is useful if you want to change your proportions of divs when on a smaller device.
For example, you may have the following
<div class="row">
<div class="col-xs-12 col-md-6">
</div>
<div class="col-xs-12 col-md-6">
</div>
</div>
So on a small device, instead of each occupy half of the width (which will be small), the two columns will be stacking up
These classes are used for different devices resolution. your first method includes all devices like mobile , laptops, desktops and larger and second method includes only desktop.
This are grid classes in bootstrap,
xs (for phones)
sm (for tablets)
md (for desktops)
lg (for larger desktops)
The classes above can be combined to create more dynamic and flexible layouts.
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
</div>
Bootstrap's grid system allows up to 12 columns across the page.
but 12 columns each row. Your code have added 60 columns in one row its wrong.
Basic Structure of bootstrap grid:
<div class="row">
<div class="col-*-*"></div>
<div class="col-*-*"></div>
<div class="col-*-*"></div>
</div>

zurb foundation make column width persist across screen sizes without individual declarations

Using zurb foundation, I'm looking to have a single class declaration that will persist across media query breakpoints. For instance, if I wanted to create a div that was 10 columns no matter if it were being viewed on a desktop or a phone.
I realize this can be accomplished by adding all classes like this:
<div class"xxlarge-12 xlarge-12 large-12...">
The content
</div>
but
I would really rather not add 5+ classes to every div that I want spanning the entire screen regardless of size.
Thanks in advance
Foundation uses min-width media queries, which means as long as you're using the "small" class, and the it doesn't change as the screen gets larger that's all you need.
For example if you want it to be full screen on Mobile and half screen on desktop you would use the following markup:
<div class="small-12 large-6 columns"></div>
Here's a link to the Zurb Foundation documentation on their grid.
If you use small-4 it should scale all larger screen sizes to only take up 4 of the grid.
In your instance you want to use small-12 columns.

Can you build multiple layouts with twitter bootstrap based on window size?

Maybe this is a very simple question, but I am wondering if you can setup multiple layouts based on window size, and have them apply based off the users current window size?
Right now I use bootstrap with C# and I set one standard size like col-md-#, and then let everything below that just fall apart into pieces as elements start overlapping. This makes the site at least readable on small screens but it is still a mess since everything is all broken apart.
Example md/lg layout
moving to a smaller screen (sm/xs) then breaks those elements into pieces, which cascade vertically.
What I am wondering is if I can maybe setup a col-xs/sm-12, and then re-arrange the elements (or re-size items like images/tables/divs) to better suite a smaller screen. Basically have/display different layouts when moving from different screen sizes.
Is it possible that instead of just breaking the elements apart (like above) for smaller screens, to in fact run a different style for smaller screen sizes (below)?
TLDR: Does bootstrap currently support multiple layouts that are determined by window size, rather than having just one layout, and breaking everything apart for anything smaller?
If so, can a simple example be given showing two layouts being enacted by different window sizes.
Yes.
If your using bootstrap 3, you can simply define the layout for xs, which will then scale up until something bigger takes over.
for example
col-xs-6 col-md-12
col-xs-6 col-md-12
col-xs-12
col-xs-12
So when its on xs, it will display as 6 columns, then medium up will display on 12 columns.
The above is better described here http://getbootstrap.com/css/#grid
you can also use push/pull to adjust the ordering http://getbootstrap.com/css/#grid-column-ordering

My thumbnails are not going into multiple columns, what am I doing wrong?

So at the moment I am working on a website (I'm using Bootstrap 3 to style my site). On the homepage, I am wanting to have a series of thumbnails which give title and brief description on different parts of the site. I have been able to program this, but I am also wanting to have the thumbnails be in four columns in a row however it pops up as four rows in a column. I am using the classes, row and col-md-4 from bootstrap and from what I see, that should automatically put the thumbnails into rows of four.
Can anyone think of what I may be doing wrong? Please ask questions on what I have written if you need to know more details.
Thanks!
Twitter bootstrap 3 is designed to be used with multiple screen resolutions out of the box. As such, depending on the size of the screen the content is restructured to either take the full available width or the preset width.
In your case you need the a single row with four columns, I suspect this is due to the fact that screen you are viewing the page in, or the width of the container is less than the md size width (Greater than or Equal to 992px - see http://getbootstrap.com/css/#grid-options).
As such, you can either use one of the lower resolution prefixes (sm or xs) or increase the width of the container.
See the example fiddle at - http://jsfiddle.net/ay3oL496/ - for clarification on how the screen resolution affects the various col- classes. Increase the width of the preview tab, and the various elements should resize.
// Update following code sample
Thanks for the code sample - the problem is clear now. See the solution at - http://jsfiddle.net/x6vqrck9/1/.
The issue you are experiencing is due to the <div class="row"> syntax that is added after each column - effectively you have one column per row. In addition, you are using col-md-4 - this will result in 3 columns per row (There are twelve grid columns, and you are using 4 to display each element).
The structure should be
<div class="container">
<div class="row">
<div class="col-md-3">Content here</div>
<div class="col-md-3">Content here</div>
<div class="col-md-3">Content here</div>
<div class="col-md-3">Content here</div>
</div>
<div class="row">Other site content here</div>
</div>
The code in the fiddle corrects these issues aswell as adding the jQuery, and bootstrap libraries.