The Grid system is the "heart and soul" of a responsive design. Bootstrap uses a 12 column grid that are typically created in any combination of multiple of twelve (e.g., 12 columns, six two columns, four three columns, eight column and four columns, etc.). Columns will AUTOMATICALLY re-arrange themselves depending on the screen size. If more than 12 columns are used, they will stack no matter what the viewport size is.
Bootstrap's uses four classes for its grid system:
IMPORTANT NOTE: These values represent the SIZE where the columns will start STACKING VERTICALLY. Columns always stack on small device by design. Also, it is important to note that class scales up, so if you want to set the same widths for xs and sm, you only need to set xs class.
Bootstrap uses two classes when creating columns:
Typically, multiple columns with a specific class (e.g., col-md-3) are nested inside of a single row with a class of row. The numbers in the .col-sm-* classes indicates how many columns the div should span. For example, .col-sm-1 spans 1 column, .col-sm-4 spans 4 columns, etc. The column numbers in all of the rows should typically add up to 12:
<div class="row">
<div class="col-md-4">Column 1</div>
<div class="col-md-4">Column 2</div>
<div class="col-md-4">Column 3</div>
</div>
Bootstrap grid system specifications:
Phones (<768px) | Tablets (>=768px) | Desktops (>=992px) | Large 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 |
Container width | None (auto) | 750px | 970px | 1170px |
Class prefix | .col-xs- | .col-sm- | .col-md- | .col-lg- |
Column width | Auto | ~62px | ~81px | ~97px |
They all have the following characteristics in common:
Examples:
For small devices (e.g., tablet and below), you may want to use a ratio of 25% (3 columns span) and 75% (9 columns span):
<div class="col-sm-3">....</div>For medium devices with 992 pixels to 1199 pixels (e.g., desktop and above), you may want to use a ratio of 50% (6 columns span) and 50% (6 columns span):
<div class="col-sm-3 col-md-6">....</div>
<div class="col-sm-9 col-md-6">....</div>
The example above will result in a 25%/75% ratio on small devices and a 50%/50% ratio on medium devices.
On very large devices, you may want to use a ratio of 33% (4 columns span) and 66% (8 columns span)
<div class="col-sm-3 col-md-6 col-lg-4">....</div>
<div class="col-sm-9 col-md-6 col-lg-8">....</div>
Notice that the first example has sm, the second has sm and md and the third has sm, md and lg.
SUMMARY: Using any .col-sm-* class on an element will affect its styling on small devices (e.g., screen size greater than or equal to 768px) as well as medium and large devices if .col-md-* and .col-lg-* class is not used. Likewise, the .col-md-* class will affect the styling of elements on medium devices as well as on large devices if a .col-lg-* class is not used. If more than 12 grid columns are placed within a single row, then each group of extra columns, as a whole, will wrap onto a new line.
The first row in the grid will have four columns of equal length. Each taking up about 25% of the width of the row each.
</div> <!-- Grid with 4 columns --> <div class="container-fluid">
<div class="row">
<div class="col-md-3">
<h2>HTML Training</h2>
</div>
<div class="col-md-3">
<h2>CSS Training</h2>
</div>
<div class="col-md-3">
<h2> JS Training</h2>
</div>
<div class="col-md-3">
<h2>Side Menu</h2>
</div>
</div>
</div>
<div class="col-md-3">
<img src="images/HTML5_logo.png" width="200" height="200" alt=""/>
<h2>HTML Training</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div> <div class="col-md-3">
<img src="images/CSS3_logo.jpg" width="200" height="200" alt=""/>
<h2>CSS Training</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div>
<div class="col-md-3">
<img src="images/JAVASCRIPT_logo.svg" alt="" width="200" height="200"/>
<h2>JS Training</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div>
To make the columns more appealing, we will center all of the headings and images.
<style> .col-md-3 h2{text-align: center;} }
</style>
<div class="col-md-3">
<img class="img-fluid img-circle mx-auto" style="display:block"
src="images/HTML5_logo.png" width="200" height="200" alt=""/>
<h2>HTML Training</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div>
<div class="col-md-3">
<img class="img-fluid img-circle mx-auto" style="display:block"
src="images/CSS3_logo.jpg" width="200" height="200" alt=""/>
<h2>CSS Training</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div>
<div class="col-md-3">
<img class="img-fluid img-circle mx-auto" style="display:block"
src="images/JAVASCRIPT_logo.svg" width="200" height="200" alt=""/>
<h2>JS Training</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div>
</p>
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
Resources
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="http://www.w3schools.com" target="new">w3schools.com</a>
<a class="dropdown-item" href="http://www.caniuse.com" target="new">caniuse.com</a>
<a class="dropdown-item" href="http://www.getBootstrap.com" target="new">getBootStrap.com</a>
</div>
The second row in the grid will have two columns of equal length. Each taking up about 50% of the width of the row each.
</div> <!-- Grid with 2 columns --> <div class="container-fluid">
<div class="row">
<div class="col-md-6">
<h2>Feature Story</h2>
</div>
<div class="col-md-6">
<h2>Tips and Tricks</h2>
</div>
</div>
</div>
<style>
.col-md-3 h2, .col-md-6 h2{text-align: center;}
body{margin-bottom:80px;}
</style>
<!-- Grid with 2 columns --> <div class="container-fluid"> <div class="dropdown-divider"></div>
<div class="row">
<div class="col-md-6">
<h2>Feature Story</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit....</p>
</div>
<div class="col-md-6">
<h2>Tips and Tricks</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit....</p>
</div>
</div>
</div>