Bootstrap For Beginners - Part Three (Bootstrap Grids)

Introduction
 
In this article we will learn about Bootstrap's Grid System, Grid Classes, and different Bootstrap Grid Layouts by creating interesting examples. So by using this we can easily create interactive, responsive website layouts.
 
Bootstrap Grid System 
 
Bootstrap grid system provides the easy way to create responsive website layouts. Bootstrap 3 introduces the responsive mobile first fluid grid system which means that the code for Bootstrap now starts with smaller screens like mobile and tablets, and then expands components and grids for larger screens like laptops and desktops. It scales up to 12 columns as the device or viewport size increases. Bootstrap's grid system is responsive, and the columns will re-arrange automatically depending on the screen size.
 
 
 
Grid Classes
 
Bootstrap 3 provides predefined grid classes and by using this we can quickly make grid layouts for different types of devices.
The Bootstrap grid system has four classes. These classes can be combined to create more layouts.
  • xs
  • sm
  • md
  • lg
The following table shows grid classes and some of the features of the new grid system in Bootstrap-3.
 
 
Basic Structure of a Bootstrap Grid
 
Now for creating rows and columns using 12 column responsive grid system.  
  • First we need to create a container that acts as a wrapper for our rows and columns by using the .container class
  • Then we have to create rows inside the container by using (<div class="row">) ,
  • Then we need to add the desired number of columns inside any row by using classes .col-xs-*, .col-sm-*, .col-md-* and .col-lg-* the number of columns should always add up to 12 for each row. In these columns we write our contents.  
The following is a Basic Structure
 
 
  1. <div class="container">  
  2.         <div class="row">  
  3.             <div class="col-*-*">col1</div>  
  4.             <div class="col-*-*">col2</div>  
  5.             <div class="col-*-*">col3</div>  
  6.         </div>  
  7.         <div class="row">  
  8.             ...  
  9.         </div>  
  10.   </div>  
Now we will create some examples for Grid Layouts by which we can easily make responsive website layouts.
 
Example 1: Two Column Layouts
 
In this example we will create two column layouts for different devices. In mobile the column will automatically arrange horizontally
according to screen size. We know that grid system works on 12 columns, so for creating two column layouts we keep the sum of the grid
column numbers  equal to 12 in each row so columns will be in one line. In this example we will add three rows and inside each
row we will add two columns. Let's create the example.
 
Step 1: First we will create a Bootstrap Template, HTML page name as "TwoColLayout.html" by usin the following code.  
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en">  
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part3</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10. <body>  
  11.     <div class="container">  
  12.         <h1>Two Column Layouts</h1>  
  13.     </div>  
  14.     <script src="js/jquery-2.1.4.min.js"></script>  
  15.     <script src="js/bootstrap.min.js"></script>  
  16. </body>  
  17. </html>  
Step 2:

Now we will add rows and columns for creating Two Column Layouts by the following code; in this we will give 
style "background-color" for each column so output shows clearly. 
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en">  
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part3</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10. <body>  
  11.     <div class="container">  
  12.         <h1>Two Column Layouts</h1>  
  13.         <!--First row with column number(4,8)-->  
  14.         <div class="row">  
  15.             <div class="col-sm-4" style="background-color:#8dc1aa">column1: col-sm-4</div>  
  16.             <div class="col-sm-8" style="background-color:#808080">column2: col-sm-8</div>  
  17.         </div>  
  18.         <!--Second row with column number(6,6)-->  
  19.         <div class="row">  
  20.             <div class="col-sm-6" style="background-color:#cfa6e2">column3: col-sm-6</div>  
  21.             <div class="col-sm-6" style="background-color:#faa76c">column4: col-sm-6</div>  
  22.         </div>  
  23.         <!--Third row with column number(3,9)-->  
  24.         <div class="row">  
  25.             <div class="col-sm-3" style="background-color:#c8fcfc">column5: col-sm-3</div>  
  26.             <div class="col-sm-9" style="background-color: #79ad96">column6: col-sm-9</div>  
  27.         </div>  
  28.     </div>  
  29.     <script src="js/jquery-2.1.4.min.js"></script>  
  30.     <script src="js/bootstrap.min.js"></script>  
  31. </body>  
  32. </html>  
Output: See output for different devices according to screen size.
 
 
 
Example 2: Three Column Layouts

In this example we will create Three column layouts for different devices like laptops and desktops screens. In this example we will add rows and inside each row we will add three columns. Let's create the example. First we will create a Bootstrap Template, HTML page name as "ThreeColLayout.html". Then we will add rows and columns for creating Three Column Layouts by the following code. 
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en">  
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part3</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10. <body>  
  11.     <div class="container">  
  12.         <h1>Three Column Layouts</h1>  
  13.         <!--First row with column number(4,4,4)-->  
  14.         <div class="row">  
  15.             <div class="col-md-4" style="background-color:#8dc1aa">column1: col-md-4</div>  
  16.             <div class="col-md-4" style="background-color:#808080">column2: col-md-4</div>  
  17.             <div class="col-md-4" style="background-color:#8dc1aa">column3: col-md-4</div>  
  18.         </div>  
  19.         <!--Second row with column number(2,8,2)-->  
  20.         <div class="row">  
  21.             <div class="col-md-2" style="background-color:#cfa6e2">column4: col-md-2</div>  
  22.             <div class="col-md-8" style="background-color:#faa76c">column5: col-md-8</div>  
  23.             <div class="col-md-2" style="background-color:#cfa6e2">column6: col-md-2</div>  
  24.         </div>  
  25.         <!--Third row with column number(3,6,3)-->  
  26.         <div class="row">  
  27.             <div class="col-md-3" style="background-color:#d2ee8b">column7: col-md-3</div>  
  28.             <div class="col-md-6" style="background-color:#8dc1aa">column8: col-md-6</div>  
  29.             <div class="col-md-3" style="background-color:#d2ee8b">column9: col-md-3</div>  
  30.         </div>  
  31.         <!--Forth row with column number(3,7,2)-->  
  32.         <div class="row">  
  33.             <div class="col-md-3" style="background-color:#808080">column10: col-md-3</div>  
  34.             <div class="col-md-7" style="background-color:#faa76c">column11: col-md-7</div>  
  35.             <div class="col-md-2" style="background-color:#808080">column12: col-md-2</div>  
  36.         </div>  
  37.     </div>  
  38.     <script src="js/jquery-2.1.4.min.js"></script>  
  39.     <script src="js/bootstrap.min.js"></script>  
  40. </body>  
  41. </html>  
Output: See output for laptops and desktops screens and in mobile it will become horizontal as usual according to the screen size.
 
 
 
Example 3: Creating Multi-Column Layouts
 
In this example we will create Multi-Column Layouts. In first row we will add 12 columns with column number 1(12x1=12), then in second row we will add 6 columns with column number 2(6x2=12),then in third row we will add 4 columns with column number 3(4x3=12),then in forth row we will add 3 columns with column number 4(3x4=12),then in fifth row we will add 2 columns with column number 6(2x6=12).Lets create example and see output by writing following code. 
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en">  
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part3</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10. <body>  
  11.     <div class="container">  
  12.         <h1>Multi Column Layouts</h1>  
  13.         <!--First row with column number(1)-->  
  14.         <div class="row">  
  15.             <div class="col-md-1" style="background-color:#8dc1aa">col-md-1</div>  
  16.             <div class="col-md-1" style="background-color:#808080">col-md-1</div>  
  17.             <div class="col-md-1" style="background-color:#8dc1aa">col-md-1</div>  
  18.             <div class="col-md-1" style="background-color:#808080">col-md-1</div>  
  19.             <div class="col-md-1" style="background-color:#8dc1aa">col-md-1</div>  
  20.             <div class="col-md-1" style="background-color:#808080">col-md-1</div>  
  21.             <div class="col-md-1" style="background-color:#8dc1aa">col-md-1</div>  
  22.             <div class="col-md-1" style="background-color:#808080">col-md-1</div>  
  23.             <div class="col-md-1" style="background-color:#8dc1aa">col-md-1</div>  
  24.             <div class="col-md-1" style="background-color:#808080">col-md-1</div>  
  25.             <div class="col-md-1" style="background-color:#8dc1aa">col-md-1</div>  
  26.             <div class="col-md-1" style="background-color:#808080">col-md-1</div>  
  27.         </div>  
  28.         <br />  
  29.         <!--Second row with column number(2)-->  
  30.         <div class="row">  
  31.             <div class="col-md-2" style="background-color:#cfa6e2">col-md-2</div>  
  32.             <div class="col-md-2" style="background-color:#faa76c">col-md-2</div>  
  33.             <div class="col-md-2" style="background-color:#cfa6e2">col-md-2</div>  
  34.             <div class="col-md-2" style="background-color:#faa76c">col-md-2</div>  
  35.             <div class="col-md-2" style="background-color:#cfa6e2">col-md-2</div>  
  36.             <div class="col-md-2" style="background-color:#faa76c">col-md-2</div>  
  37.         </div>  
  38.         <br />  
  39.         <!--Third row with column number(3)-->  
  40.         <div class="row">  
  41.             <div class="col-md-3" style="background-color:#d2ee8b">col-md-3</div>  
  42.             <div class="col-md-3" style="background-color:#8dc1aa">col-md-3</div>  
  43.             <div class="col-md-3" style="background-color:#d2ee8b">col-md-3</div>  
  44.             <div class="col-md-3" style="background-color:#8dc1aa">col-md-3</div>  
  45.         </div>  
  46.         <br />  
  47.         <!--Forth row with column number(4)-->  
  48.         <div class="row">  
  49.             <div class="col-md-4" style="background-color:#faa76c">col-md-4</div>  
  50.             <div class="col-md-4" style="background-color:#cfa6e2">col-md-4</div>  
  51.             <div class="col-md-4" style="background-color:#faa76c">col-md-4</div>  
  52.         </div>  
  53.         <br />  
  54.         <!--Fifth row with column number(6)-->  
  55.         <div class="row">  
  56.             <div class="col-md-6" style="background-color:#808080">col-md-6</div>  
  57.             <div class="col-md-6" style="background-color:#8dc1aa">col-md-6</div>  
  58.         </div>  
  59.     </div>  
  60.     <script src="js/jquery-2.1.4.min.js"></script>  
  61.     <script src="js/bootstrap.min.js"></script>  
  62. </body>  
  63. </html>  
 
Output
 
 
Example 4: Offsetting the Grid Columns
 
In this example we will learn about Offsetting the Grid Columns. We can move grid columns to the right by using the column Offset classes like .col-sm-offset-*,.col-md-offset-* we can set alignment. These classes are used for offsetting the column and it will increase its left margin by a specified number of columns. In this example the class .col-sm-offset-3 on the column .col-sm-9 moves it to the right as specified offset three columns from original position by writing following code. 
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en">  
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>Bootstrap Part3</title>  
  7.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  8.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">  
  9. </head>  
  10. <body>  
  11.     <div class="container">  
  12.         <h1>Offsetting the Grid Columns</h1>  
  13.         <!--First row with column number(3,9)-->  
  14.         <div class="row">  
  15.             <div class="col-sm-3" style="background-color:#8dc1aa">column1: col-sm-3</div>  
  16.             <div class="col-sm-9" style="background-color:#808080">column2: col-sm-9</div>  
  17.         </div>  
  18.         <!--Second row with column number(9),with 3 column offset-->  
  19.         <div class="row">  
  20.             <div class="col-sm-9 col-sm-offset-3" style="background-color:#cfa6e2">column3: col-sm-9 with 3 column Offset</div>  
  21.         </div>  
  22.     </div>  
  23.     <script src="js/jquery-2.1.4.min.js"></script>  
  24.     <script src="js/bootstrap.min.js"></script>  
  25. </body>  
  26. </html>  
Output
 
 
In this article we focused on Bootstrap Grid system. Then in the next articles we will understand all the components of Bootstrap step by step.
 
Read more articles on Bootstrap:

Up Next
    Ebook Download
    View all
    Learn
    View all