Using The Bootstrap Mobile Grid System In A Real World Setting
The Bootstrap mobile grid system uses some clever CSS to make a simple to use, but extremely powerful, mobile grid system. The ability to rearrange columns as well as define different ways for them to display for different screen sizes makes it just as flexible as it is powerful.
Between Bootstrap 2 & 3 there were a lot of changes to how the grid system, and the core, worked. The push pull classes were added for ordering along with column sizing options that made it easy to handle different layouts at different screen sizes.
They were aremed with the knowledge they got from litterlay billions of pageviews and thousands of code submissions and ammedments on the github repo when they rebuilt mobile first for Bootstrap 3. During the rebuild they included everything developers wanted & everything users needed to make pages look and work great on any device with at any screensize.
The Mobile First Grid System
With the new mobile first focus I guess it seemed time to take another look at how things work on mobile and one of the most common complaints with Bootstrap 2.3.2 was that the grid system totally collapsed on mobile. While that does work it not optimal for all situations so in comes the new mobile grid.
The 2 most common edits by developers to how the old grid system worked was preventing certain columns from collapsing – by overriding the width on the small media query – and changing the order of the columns from right to left – by changing spans to float:right
and inverting the margins. Both of those issue have been completely fixed with Bootstrap 3 because you can decide which point the grid collapses (if you even want it to collapse at all) and you can now decide the order by pushing and pulling them.
New Bootstrap 3 Grid Classes
The whole grid system has been overhauled and the first thing you need to know is that the class names have been changed. Gone are the old .span*
classes we are used to, the new grid system uses .col-*
instead. There’s also .col-sm-*
and .col-lg-*
for tablet and desktop respectively.
.col-lg-*
collapses below desktop sizes, so it collapses on everything below large tablets. .col-sm-*
collapses on small tablets and mobile devices. The standard .col-*
class does not collapse at all.
Offsets are still there for anything above mobile view using .col-offset-*
.
<div class="row"> <div class="col-2 col-offset-2></div> <div class="col-8></div> </div>
Responsive Grid Ordering
Bootstrap 3 has an easy way of ordering the columns in your grid. No matter the order you have your columns in your markup you can have them display in whatever order you want. There’s 2 reasons why you would want to do this: an SEO reason and a UX reason.
In terms of usability if you have the sidebar first in the markup then it’s going to display before the main content when the grid finally collapses. That means your forcing the user to have to scroll away down before they reach what they are looking for. Not the best experience you could be giving.
In terms of SEO this is good for a bunch of reasons that can get quite complex but if your interested here’s the short version with the 2 main points of benefit:
- The earlier something is in the markup the more relevant that content is to the page. As a generic rule the post/page content is going to be more important that what’s in the sidebar.
- Search engines seem to give most (if not all) of the value of a link to the first occurrence of a link that it finds on a page.
- If you link to something in both the sidebar and the main content then the first occurrence will be the most powerful in terms of SEO. You want the most powerful link to be surrounded by high quality related content that helps Search Engines work out where the most relevant place to rank it is. The content around it in the post is likely to be a better fit for that scenario that the content in the sidebar.
Moving your columns around is as easy as pushing or pulling the them in the direction you want. Either use the .col-push-*
to move columns in the grid left or .col-pull-*
to pull them right.
To show you an example of the left sidebar scenario.
<div class="row "> <div class="col-sm-8 col-sm-push-4"> <!-- this is the column for the main content --> <!-- notice its being pushed by 4 column widths, that moves it 4 spaces right --> </div> <div class="col-sm-4 col-sm-pull-8"> <!-- this is the sidebar content column --> <!-- it's pulled 8 column widths, that moves it 8 to the left --> </div> </div>
The above example shows how you can use the push/pull classes to rearrange the layout in a simple 2 column design.
Some site have simple 2 column layouts but most sites are a bit more complex than that. Maybe you have 3 columns or more and things are split with multiple rows and grids on the one page.
Bootstrap has you covered in those complicated situations as well.
Say you had 3 primary rows each with different column set-ups. Each behaves differently on different device sizes. Let’s also say that some of the columns contain their own grid systems that operate independently from their parent. And some may have multiple independent grids inside the same column.
It might sound overly complicated but it’s modelled on an existing design flow that’s fairly common on a lot of websites. Header, Content, Footer – 3 rows.
To achieve the layout described above you would use a combination of different sizing classes markup that matches this.
Make your own Mobile Grid System
Inside the Bootstrap 3 LESS file there are a combination of variables
and mixins
that allow you to easily create your very own grid system. You can either do this yourself right now if you are comfortable with LESS or you could use the Bootstrap 3 customizer to do it for you.
I’ve made a lot of use of the Mobile Grid System and some of the mobile show/hide CSS from Bootstrap in my Best ReLoaded Bootstrap Theme.
Loving bootstrap 3. It’s just so efficient and clean, and I definitely prefer it to Foundation 4. Can’t wait to continue using it in the future.
I’d definitely have to disagree with you on the clean and efficient front. It’s a heavyweight set of css classes and jquery plugins that requires lots and LOTS of markup to make it work… but it is still awesome so… 😀
Great article! Thanks.
Anyone can tell me how to decrease the collapsed width of the menu in BS3?
I am having a menu and a logo on the left and it collapsed right on mobile but not on Ipad.
probably because it stacks under 768 pixels and not below..
anyway of changing the menu so it also collapses on Ipad (Portrait at least..)
Thanks
Hey Gil, thanks for the kind words about the article.
I’m not 100% sure I understand your question but what it sounds like to me is that you want the site to collapse to the mobile view earlier.
There are 2 easy options for you to do that. because this post is about Bootstrap 3 I’m going to assume your using Bootstrap 3 RC1 but if your using 2.3.2 then let me know because it would be a slightly different set of methods.
If you go with option 1 there are a bunch of different media queries that you would need to change. I’ve got a list of the line numbers you’d need to change in the bootstrap.css file.
Is the website your talking about the one that you linked in the website field? Because on that site I don’t see if behaving correctly when it comes to responsiveness, it’s because you haven’t set your viewport properly (I see a bunch of them in the head section of the homepage but all commented out).
If you need any more help with things then feel free to jump back here and ask. I’m here to help 😀
Hi William
You understand me correctly 🙂
I am trying to get the site collapsed earlier, not really on mobile view but on Ipad because in mobile it works great.
I understand the concept of your option 1, I thought about it already but dont know what class I need to reference to make the menu collapse. do you know what class makes the navbar collapse?
The website I am talking about is not the one that is linked to my website field. the one linked to my profile is my own website which will soon be built on bootstrap too 🙂
what I am talking about is here:
http://www.clients.tipoos.com/fireblade/
all works great except for Ipad. if you check on Ipad you will see the problem with the top menu
Thanks a lot!
Gil
Yeah I see the problem your trying to fix. I’m on mobile just now so I don’t have access to Firebug or the Inspector but I know you need to target the
.navbar-toggle
class and make itdisplay:inline
above 728px with a media query. In that same media query you need to target.nav-collapse.collapse
and change it todisplay:none
. You also need to tackle the list items in the menu and prevent them from going inline in the tablet view. You can do it all within the one media query. I suggest you specify a max-width in the media query but don’t specify a min-width.Sorry I can’t be more helpful just now but I’ll take a look next time I’m on my laptop and have a spare half hour 🙂
Tried that:
@media (min-width: 767px) {.navbar-toggle{display: inline;}.nav-collapse, .nav-collapse{display: none;}}
unfortunately it didn’t go well.. probably more adjustments are needed
anyway you can help? I am willing to pay of course.
BTW – in this site it works perfect! just like I need it
please let me know
Thanks
I also tried max-width..
Hey Gil,
Not that I want it to sound like I’m money grubbing but money does get things done quickly 😉 I’m assuming that as you have this on the “clients” subdomain that the quicker you can get this fixed the better.
I’m on my laptop now so I will take another quick look at the site and see what I can figure out.
Hi William.
I would not offer it if I didn’t think you deserve it 🙂
what email I could contact you in private?
You can contact me using will@pattonwebz.com