Bootstrap Grid System

Bootstrap Grid System is an amazing tutorial to manage content in grid shape. In this tutorial we shall discuss the Bootstrap Grid System.

What is a Grid?

As written on wikepedia:

In graphic design, a grid is a structure (usually two-dimensional) made up of a series of intersecting straight (vertical, horizontal) lines used to structure content. It is widely used to design layout and content structure in print design. In web design, it is a very effective method to create a consistent layout rapidly and effectively using HTML and CSS.

In the other simple words grids organize and structure content in web design, makes websites easy to scan and trim down cognitive load on users.

What is Bootstrap Grid System?

As written by the official documentation of Bootstrap for grid system:

Bootstrap includes a responsive, mobile first fluid grid system that appropriately scales up to 12 columns as the device or viewport size increases. It includes predefined classes for easy layout options, as well as powerful mixins for generating more semantic layouts.

Let us realize the above declaration. Bootstrap 3 is mobile first in the intellect that the code for Bootstrap now starts by targeting smaller screens like mobile devices, tablets, and then “expands” components and grids for larger screens such as laptops, desktops.

Mobile First Strategy

  • Content
    • Find out what is most important in your content.
  • Layout
    • Start your design to smaller widths first.
    • First base CSS address for mobile device and then media queries address for desktops and tablets.
  • Progressive Enhancement
    • Add/Remove elements as screen size increases and decreases.

Working of Bootstrap Grid System

Grid systems in Bootstrap are used for creating page layouts through a series of rows and columns that house your content. Here’s how the Bootstrap grid system works:

  • Rows must be placed within a .container class for proper alignment and padding.

  • Use rows to create horizontal groups of columns.

  • Content should be placed within columns, and only columns may be immediate children of rows.

  • Predefined grid classes like .row and .col-xs-4 are available for quickly making grid layouts. LESS mixins can also be used for more semantic layouts.

  • Columns create gutters (gaps between column content) via padding. That padding is offset in rows for the first and last column via negative margin on .rows.

  • Grid columns are created by specifying the number of twelve available columns you wish to span. For example, three equal columns would use three .col-xs-4.

Media Queries

Media query is actually fancy term for "conditional CSS rule". It basically applies several CSS based on certain conditions set forth. If those entire situations are met, the style is applied.

Bootstrap Media Queries allow you to hide, show and move content based on required browser’s dimensions. Subsequent media queries are used in LESS files to make the key breakpoints in the Bootstrap grid system.

Occasionally these are expanded to include a max-width to limit CSS to a narrower set of devices.

Media queries are based on two major parts:

  1. A device specification
  2. Then a size rule.

In the above case, the following rule is set:

Let’s think about this line:

For all devices no matter what type with min-width: @screen-sm-min if the width of the screen gets smaller than @screen-sm-max, then do something.

Grid options

The following table sums up features of how Bootstrap grid system works across the multiple devices:

Extra small devices Phones (<768px) Small devices Tablets (≥768px) Medium devices Desktops (≥992px) Large devices Desktops (≥1200px)
Grid behavior Horizontal at all times Collapsed to start, horizontal above breakpoints Collapsed to start, horizontal above breakpoints Collapsed to start, horizontal above breakpoints
Max container width None (auto) 750px 970px 1170px
Class prefix .col-xs- .col-sm- .col-md- .col-lg-
# of columns 12 12 12 12
Max column width Auto 60px 78px 95px
Gutter width 30px
(15px on each side of a column)
(15px on each side of a column)
(15px on each side of a column)
(15px on each side of a column)
Nestable Yes Yes Yes Yes
Offsets Yes Yes Yes Yes
Column ordering Yes Yes Yes Yes

Basic Grid Structure

Following is basic structure of Bootstrap grid:

Let us see some simple grid examples:

Responsive column resets

You are bound to run into issues with the four tiers of grids available at the positive breakpoints, the columns do not clear quite right side as one is taller than the other. To fix this issue, use this combination of class “.clearfix” and the responsive effectiveness classes as publicized in the following example.

Resize your viewport or check it out on your phone for a desired result of this example.

Offset columns

Offsets are a helpful feature for additional specialized layouts. These can be used to push columns over for extra spacing, for example. The .col-xs=* classes do not support offsets, but they are simply simulated by using an empty cell.

To use these offsets on large displays, use the .col-md-offset-* classes. These classes increase the left margin of a column by * columns where * range from 1 to 11.

In this example we have <div class=”col-md-6″>..</div>, we will center this using class .col-md-offset-3.

Nesting columns

If you want to nest your content/data with the default grid, add a new .row and set of .col-md-* columns within an current .col-md-* column. Nested rows must include a set of columns that can add up to 12.

In the following model, the layout has two columns, with the second one being divided into four boxes over two rows.

Column ordering

An additional good feature of Bootstrap grid system is that you can write without difficulty the columns in an order, and explain them in another one. Anyone can easily modigy the order of built-in grid columns with .col-md-push-* and .col-md-pull-* modifier classes where * range from 1 to 11.

In this following example we are using two-column layout and left column is the narrowest and doing act as sidebar. We will exchange the order of these columns by .col-md-push-* and .col-md-pull-* classes.

Leave a Reply

Your email address will not be published. Required fields are marked *