Introduction to Bootstrap 3 Grid System

Introduction

 
Bootstrap provides a Mobile-first fluid and responsive Grid System that appropriately scales up to 12 columns as the device or viewport size increases. Using the Bootstrap Grid System, we can create a responsive web layout very quickly.
 
 
Bootstrap provides predefined grid classes for making layouts for various types of devices, like phones (<768px), tablets (≥768px), medium sized desktops (≥992px) and large devices (≥200px).
 
 
Example: Creating two columns (of equal width) for medium Sized desktop screen
 
1) First, add the .container class to the div that sets the maximum width and centering of the layout.
 
2) After adding a container class, add another div with .row class. As we know that a row in the grid is made up of 12 columns and we will create two columns having the same width we need to use a .col-md-6 class in both divs (Sum of Columns 6+6=12). If we use a .col-md-6 class in one div and a .col-md-7 in another div then the second div wrap up and come in the next row (because here the number of columns in a row becomes more than 12, in other words, 6+7).
  1. <!DOCTYPE html>      
  2. <html>      
  3. <head>      
  4. <title>Grid Example</title>      
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">      
  6. <link href="css/bootstrap.min.css" rel="Stylesheet">      
  7. <style>      
  8. </style>      
  9. </head>      
  10. <body>      
  11. <div class="container">      
  12. <div class="row">      
  13. <div class="col-md-6"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor eleifend nibh at tristique. In sit amet nibh non dolor tempor tincidunt. Mauris aliquet, lacus in lobortis pulvinar, ligula augue venenatis mi, vel adipiscing orci libero ut nisi. Suspendisse lobortis faucibus enim, a ullamcorper nibh. Aliquam ut diam posuere, ornare augue eget, dignissim massa. Nullam consectetur fermentum enim sit amet hendrerit. Phasellus commodo ornare aliquam. Vestibulum odio enim, pretium in velit semper, faucibus egestas nunc.</p></div>      
  14. <div class="col-md-6"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor eleifend nibh at tristique. In sit amet nibh non dolor tempor tincidunt. Mauris aliquet, lacus in lobortis pulvinar, ligula augue venenatis mi, vel adipiscing orci libero ut nisi. Suspendisse lobortis faucibus enim, a ullamcorper nibh. Aliquam ut diam posuere, ornare augue eget, dignissim massa. Nullam consectetur fermentum enim sit amet hendrerit. Phasellus commodo ornare aliquam. Vestibulum odio enim, pretium in velit semper, faucibus egestas nunc.</p></div>      
  15. </div>      
  16. </div>      
  17. <script js="js/bootstrap.min.js"></script>      
  18. </body>      
  19. </html>     
Preview
 
 
Example: Multiple Grid System
 
In a multiple grid system, we use two or more predefined grid classes for a single div. For easy understanding, we will create an example in which we have 3 content boxes (3 div) of the same size (33%/33%/33%) for medium-sized devices and when we reduce the size or access it with a tablet (small-sized device) then our layout changes to 2 divs of equal width (in other words 50%) and one with 100% width below the first two divs.
 
In this example, we use 3 divs with a multiple grid system. Here, we set the .col-md-4 class for all the 3 divs that makes it visible side by side and of the same width for the medium-sized desktop (≥992px) and for small screen devices like a tablet we use a .col-sm-6 class for the first 2 divs so that on small devices the first two divs come along side by side and .col-sm-12 for the third div that makes it take the entire width of the container.
  1. <!DOCTYPE html>      
  2. <html>      
  3. <head>      
  4. <title>Multiple Grid Example</title>      
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">      
  6. <link href="css/bootstrap.min.css" rel="Stylesheet">      
  7. <style>      
  8. </style>      
  9. </head>      
  10. <body>      
  11. <div class="container">      
  12. <div class="row">      
  13. <h3>Example of Multiple grid System</h3>      
  14. <div class="col-sm-6 col-md-4">Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.</div>      
  15. <div class="col-sm-6 col-md-4">Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.</div>      
  16. <div class="col-sm-12 col-md-4">Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.</div>      
  17. </div>      
  18. </div>      
  19. <script js="js/bootstrap.min.js"></script>      
  20. </body>      
  21. </html>     
The following is a preview of a medium device desktop:
 
 
The following is a preview of viewing on a small devices tablet:
 
 
Example: Nested Column
 
For adding a nested column, add another .row class to the div that you want to do the partition and then divide the column using .col-md-* as needed.
  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4. <title>Grid Example</title>    
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">    
  6. <link href="css/bootstrap.min.css" rel="Stylesheet">    
  7. <style>    
  8. </style>    
  9. </head>    
  10. <body>    
  11. <div class="container">    
  12. <div class="row">    
  13. <div class="col-md-8">    
  14. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor eleifend nibh at tristique. In sit amet nibh non dolor tempor tincidunt. Mauris aliquet, lacus in lobortis pulvinar, ligula augue venenatis mi, vel adipiscing orci libero ut nisi. Suspendisse lobortis faucibus enim, a ullamcorper nibh. Aliquam ut diam posuere, ornare augue eget, dignissim massa. Nullam consectetur fermentum enim sit amet hendrerit. Phasellus commodo ornare aliquam. Vestibulum odio enim, pretium in velit semper, faucibus egestas nunc.</p>    
  15. <div class="row">    
  16. <div class="col-md-6">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor eleifend nibh at tristique. In sit amet nibh non dolor tempor tincidunt. Mauris aliquet, lacus in lobortis pulvinar, ligula augue venenatis mi, vel adipiscing orci libero ut nisi. Suspendisse lobortis faucibus enim, a ullamcorper nibh.</div>    
  17. <div class="col-md-6">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor eleifend nibh at tristique. In sit amet nibh non dolor tempor tincidunt. Mauris aliquet, lacus in lobortis pulvinar, ligula augue venenatis mi, vel adipiscing orci libero ut nisi. Suspendisse lobortis faucibus enim, a ullamcorper nibh.</div>    
  18. </div>    
  19. </div>    
  20. <div class="col-md-4">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor eleifend nibh at tristique. In sit amet nibh non dolor tempor tincidunt. Mauris aliquet, lacus in lobortis pulvinar, ligula augue venenatis mi, vel adipiscing orci libero ut nisi. Suspendisse lobortis faucibus enim, a ullamcorper nibh.</div>    
  21. </div>    
  22. </div>    
  23. <script js="js/bootstrap.min.js"></script>    
  24. </body>    
  25. </html>   
Preview
 
 
Offset
 
Offset is required for moving columns towards the right side using .col-md-offset-* classes; here the value of * is between 1 and 11.
  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4. <title>Grid Example</title>    
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">    
  6. <link href="css/bootstrap.min.css" rel="Stylesheet">    
  7. <style>    
  8. </style>    
  9. </head>    
  10. <body>    
  11. <div class="container">    
  12. <div class="row">    
  13. <div class="col-md-3"><p style="background-color:silver;padding:20px;">.col-md-3</p></div>    
  14. <div class="col-md-3"><p style="background-color:silver;padding:20px;">.col-md-3</p></div>    
  15. <div class="col-md-3"><p style="background-color:silver;padding:20px;">.col-md-3</p></div>    
  16. <div class="col-md-3"><p style="background-color:silver;padding:20px;">.col-md-3</p></div>    
  17. </div>    
  18. <div class="row">    
  19. <div class="col-md-3 col-md-offset-3"><p style="background-color:silver;padding:20px;">.col-md-3 .col-md-offset-3</p></div>    
  20. </div>    
  21. <div class="row">    
  22. <div class="col-md-5"><p style="background-color:silver;padding:20px;">.col-md-5</p></div>    
  23. <div class="col-md-3 col-md-offset-2"><p style="background-color:silver;padding:20px;">.col-md-3 .col-md-offset-2</p></div>    
  24. </div>    
  25. </div>    
  26. <script js="js/bootstrap.min.js"></script>    
  27. </body>    
  28. </html>   
Preview