By Syed Fazle Rahman

Understanding Bootstrap’s Grid System

By Syed Fazle Rahman

Bootstrap is undoubtedly one of the most popular front-end frameworks. With more than 73k stars and 27k forks, Bootstrap is also one of the most popular GitHub repositories. In my last article, Responsive Web Design Tips from Bootstrap’s CSS, I explained how Bootstrap functions as a responsive framework. In this article, we will discuss a related topic: The Grid System, one of the most important concepts in Bootstrap.

What is the Bootstrap Grid System?

Like any grid system, the Bootstrap grid is a library of HTML/CSS components that allow you to structure a website and place a website’s content in desired locations easily.

Think of graph paper, where every page has a set of vertical and horizontal lines. When these lines intersect, we get squares or rectangular spaces.

Graph paper

By Sfoerster (Own work) CC-BY-SA-3.0, via Wikimedia Commons

Well, this is also true for Bootstrap’s Grid System. It allows you to create rows and columns and then place content in the “intersected” areas.

Now the question is, how many rows and columns you can create using Bootstrap’s Grid System? Bootstrap allows you to create up to 12 columns and unlimited rows — hence the name 12-Grid System. So, let’s see how we can utilize this grid system to create various types of layouts.

Getting started with Bootstrap’s Grid System

To get started, naturally, you’ll need to have the necessary assets in your page to get Bootstrap working. If you’re new to Bootstrap, you can refer to our previous article Getting started with Bootstrap or my book Jump Start Bootstrap, to dig deeper.

Bootstrap’s Grid System is made up of 3 things:

  1. A container
  2. Rows
  3. Columns

Let’s explore each of the above in detail.

Creating a Container

Bootstrap’s grid system needs a container to hold rows and columns. A container is a simple <div> element with a class of .container. The container is used to provide a proper width for the layout, acting as a wrapper for the content.

Take a look at the following CodePen demo:

See the Pen Bootstrap grid container demo by SitePoint (@SitePoint) on CodePen.

Here the container element wraps the content and sets left and right margins. It also has different fixed widths in different sized devices. Have a look at the following table:

Device Width Container Width
1200px or higher 1170px
992px to 1199px 970px
768px to 991px 750px
Less than 768px auto

You can choose a fluid container if you are not fond of a fixed layout. To do this, you use the class .container-fluid. A fluid container has no fixed width; its width will always be the width of the device.

Just note that both fixed and fluid containers have padding of 15px on the left and right sides.

Creating a Row

A row acts like a wrapper around the columns. The row nullifies the padding set by the container element by using a negative margin value of -15px on both the left and right sides.

A row spans from the left edge to the right edge of the container element. It is created by adding the class .row to a block level element inside the container.

Have a look at the following CodePen:

See the Pen Bootstrap grid demo with row by SitePoint (@SitePoint) on CodePen.

In this demo, you can see the text touching the left edge of the container element. This is because the container’s padding has been removed by the row due to the negative margins on the row.

Finally, there’s no limit on the number of rows you can create.

Creating Columns

Bootstrap uses different column class prefixes for different sized devices. These prefixes are shown in the table below:

Class Prefix Device Size
.col-xs- <768px
.col-sm- 768px to 991px
.col-md- 992px to 1199px
.col-lg- ≥ 1200px

So, let’s create our first Bootstrap column:

See the Pen Bootstrap Grid demo with row and column by SitePoint (@SitePoint) on CodePen.

In the above demo, I used the class .col-xs-12 to create a single column that spans across 12 virtual Bootstrap columns. Hence, this column’s width will be the width of the row.

In the above demo, you will also see the 15px padding reappear to push the element away from the container. This is because every column in Bootstrap has a padding of 15px.

You must be wondering why I used the class prefix that belonged to extra smaller devices, which is .col-xs-. In Bootstrap, if a column is defined for a particular type of device then it is guaranteed to behave similarly in larger devices as well. Therefore, a column defined for extra smaller devices will work in all types of devices.

Let’s now create a 2-column layout for smaller devices and check out its behaviour in larger devices and extra-small devices. We will use the class prefix .col-sm- here. To create 2 columns of equal widths, we should assign 6 virtual Bootstrap columns to each one of them. This way, we maintain the limit of 12 virtual Bootstrap columns for a single row.

Here’s the demo:

See the Pen Bootstrap grid demo, 2 columns by SitePoint (@SitePoint) on CodePen.

Nesting with the Grid System

Nesting is one of the ways to create complex designs using Bootstrap’s grid system. It is also the one section where many first-timers have trouble.

We understand that to use Bootstrap’s grid system, we need 3 things: A container, rows, and columns. So to nest a grid system within a column we will need the same three things. But the only difference is that the container is already defined. In this case, the columns will behave as the containers for the nested grid system.

Here’s the logic: The containers provide 15px of padding, which is nullified by the row. Then we define columns that again have 15px of padding on the left and right side. So, to nest a grid system within a column, we simply need rows and columns. No .container or .container-fluid elements are necessary for a nested grid system.

Here’s an example of a nested grid system:

See the Pen Bootstrap grid demo with nested columns by SitePoint (@SitePoint) on CodePen.

What About More than 12 Columns?

This is one of the root causes for disordered Bootstrap layouts. A wrong calculation in deciding the number of virtual Bootstrap columns can lead to an improper layout.

In such a case, a virtual row will be created and unfitted columns will shift to the next row. For example, if you have defined 2 columns with the classes .col-md-8 and .col-md-5, the second column will shift to a new row because it requires 5 virtual Bootstrap columns whereas only 4 are left.

Helper Classes

Bootstrap provides various helper classes that can be useful in certain situations in dealing with grids. These classes are:

  • .clearfix: Normally used to clear floats, adding this class to any column will make it shift to a new row automatically, to help you correct problems that occur with uneven column heights.
  • Offsetting columns: You don’t have to occupy all 12 of the virtual columns. You can use offset classes like .col-xs-offset-* or .col-md-offset-* to leave a particular number of virtual Bootstrap columns to the left of any column (kind of like invisible place holders).
  • Reordering: Use classes like .col-md-push-* and .col-md-pull-* to shift a column to the right or left, respectively.

Complimentary Code Snippet

To conclude, here is a complimentary code snippet for a 3-column layout with a responsive Bootstrap navigation bar. Have fun and let me know in the comments if there’s anything in Bootstrap’s grid system that I haven’t discussed here that you find interesting, problematic, etc.

See the Pen Bootstrap 3-column grid example by SitePoint (@SitePoint) on CodePen.

(Just note that the 3-column layout won’t appear in the CodePen embed above, but you can view it at full screen).

  • dongguangming

    great useful article,thank you.

  • Andy K

    Is there a trick to printing out a SitePoint article that uses CodePen (i.e. all of them)? Firefox, Chrome, IE – all show a big CodePen header but never all of the code.

  • adam__roberts

    I just had a quick look around the CodePen documentation and can’t see anything on this. Unfortunately you might have to rely a screenshot to capture that stuff on a printed page, sorry about that.

    • chriscoyier

      We can fix that. hold tight.

      • adam__roberts

        Thanks Chris!

  • midnight

    Used nested grid wrong. Forgot inner row class. Leads to extra padding inside.

  • Mike Ross

    Great article, thank you for writing it.
    What I don’t get is in your 3 column example, why did you create a second container for the navbar?

    • Hi Mike,

      The class .container-fluid provides a padding of 15px on both left and right sides. If this was not used, the elements inside the navigation bar would stick to the corners.

      Check out Responsive Web Design Tips from Bootstrap’s CSS where I have explained this.

      Thanks. :)

      • Mike Ross

        Nice, that makes sense. Thank you

  • LouisLazaris

    Sorry about that. We had the class in there, but the HTML had a typo. It’s fixed now. Thanks.

  • Pritam Mendon

    Hey Syed, Thank you so much for this wonderful article it helped me understand it very well… i would like to get more in detail, hoping that may be in future you can make like a part 2 of this..

  • rekha

    Hi syed,
    Nice article. I am new to bootstrap. But still this article helps understanding grid layout.
    Thanks keep posting for other elements also.

  • Moneer Naser

    Thanks Syed for the valuable article.
    In the last example, you have a row with the columns with the classes “col-md-4 col-sm-6”.
    I’m ok with the first class (they add up to 12), but what about the col-sm-6? they add up to 18. Is this allowed?

  • Rachel

    Now it all makes sense! Great job

  • Hi Moneer,

    Yes, Definitely that’s allowed. Once all the 12 virtual Bootstrap columns are filled, the remaining will wrap onto a new line. That’s one of the advantages of using Bootstrap’s grid system. Thanks for asking this question.

    Regards :)

  • dksuresh

    Very nice article :) presentation is good.. can you write about Bootstrap Sass?

  • sindhus

    Thank you so much for writing this, Trying my hand at using Bootstrap for a assignment. Helped a lot!

  • Silviu

    In the last example shouldn’t you have included also bootstrap.js for the collapsed menu to work ?

  • dam

    Hi syed how can i perform bootstrap grid push and pull in bootstrap 2.0 ( grid using span6,span6 )

  • Say Wow Media

    yeah man good stuff. really nice and clear… no more in the dark here…

  • Наме Мурад Агаин Мурад

    thank you much for writing about grid system. but what do you mean when say bootstrap needs a container to hold rows and columns and acting as a wrapper for the content? what is difference between to use .container div or not to use, very interesting point))

    • Kotya Karapetyan

      .row has a negative horisontal padding. You need a container which has a positive margin to come to zero. Then you use columns which also habe positive margin to obtain a nice layout.

  • Nazar Jumangy

    Custom Flexbox Grid using Bootstrap mixins (SASS)

    Custom grid new layout module flexbox in css3. Flexbox grid uses the
    same classes as bootstrap grid, so you won’t have to get used to new
    classes names. 12,24,48 columns grid can be configured in
    variables.scss file.

  • NonZero


  • Daniel Johns

    Hey great article! I have one question though – in the ‘nested columns’ codepen, why do the nested col-sm-6 take up the whole width? shouldn’t they take up half while there parent col-sm-6 take up whole?

  • Huzaifa

    Can I Do Something like .. As in Both in One Class

  • FortuneCookiie

    thank you !

  • Sneha Desai

    nice article!!

Get the latest in Front-end, once a week, for free.