If this tutorial is not what you were looking for, you still have any questions, suggestions or concerns - feel free to let us know. Please help us to serve you better!

Your Name

Your Email

Your Message (required)

captcha

HTML/CSS. Bootstrap 3 Grid System

This tutorial provides information regarding Bootstrap 3 Grid System.

Twitter Bootstrap grid system provides a fast and easy way of creating layouts of web pages. Twitter Bootstrap 3 introduces the responsive mobile first fluid grid system that appropriately scales up to 12 columns as the device or viewport size increases. Let’s see how it works.

  1. Rows should be placed within a .container (fixed-width) or .container-fluid (full-width) for proper alignment and padding.

  2. You should use rows to create horizontal groups of columns.

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

  4. 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.

  5. 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.

  6. 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.

  7. Grid classes apply to devices with screen widths greater than or equal to the breakpoint sizes, and override grid classes targeted at smaller devices. Therefore, applying any .col-md- class to an element will not only affect its styling on medium devices but also on large devices if a .col-lg- class is not present.

Layouts

Twitter Bootstrap 3 includes predefined grid classes for quickly making grid layouts for different types of devices like cell phones, tablets, desktops, etc. For example, you can use the .col-xs- class to create grid columns for extra small devices like cell phone similarly the .col-sm- for small screen devices like tablets, the .col-md- class for medium size devices like desktop and the .col-lg- for large desktop screens. Below you can see layouts defined in Bootstrap Grid System.

  1. Extra small devices like Phones (<768px)

  2. Small devices like Tablets (≥768px)

  3. Medium devices like Desktops (≥992px)

  4. Large devices like Desktops (≥1200px)

Grid options

Please check the following table for more detailed information:

Bootstrap 3 Grid System layouts Extra small devices
Phones (<768px)
Small devices
Tablets (≥768px)
Medium devices
Desktops (≥992px)
Large devices
Desktops (≥1200px)
Max container width None (auto) 750px 970px 1170px
Class prefix .col-xs- .col-sm- .col-md- .col-lg-
Max column width Auto ~62px ~81px ~97px
Gutter width 15px on each side of a column (i.e. 30px)

Applying any .col-sm- class to an element will not only affect its styling on small devices like tablets, but also on medium and large devices having screen size greater than or equal to 768px (i.e. ≥768px) if .col-md- and .col-lg- class is not present. Similarly the .col-md- class will not only affect the styling of elements on medium devices, but also on large devices if a .col-lg- class is not present.

Let’s check some examples:

  1. Stacked-to-horizontal. Using a single set of .col-md-* grid classes, you can create a basic grid system that starts out stacked on mobile devices and tablet devices (the extra small to small range) before becoming horizontal on desktop (medium) devices. Place grid columns in any .row.

    HTMLCSS. Bootstrap 3 Grid System-1

    You can check the code below:

    <div class="row">
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    	<div class="col-md-1">.col-md-1</div>
    </div>
    <div class="row">
    	<div class="col-md-8">.col-md-8</div>
    	<div class="col-md-4">.col-md-4</div>
    </div>
    <div class="row">
    	<div class="col-md-4">.col-md-4</div>
    	<div class="col-md-4">.col-md-4</div>
    	<div class="col-md-4">.col-md-4</div>
    </div>
    <div class="row">
    	<div class="col-md-6">.col-md-6</div>
    	<div class="col-md-6">.col-md-6</div>
    </div>   
    
  2. Fluid container. Turn any fixed-width grid layout into a full-width layout by changing your outermost .container to .container-fluid.

    <div class="container-fluid">
    	<div class="row">
    	...
    	</div>
    </div>
    
  3. Mobile and desktop. You can use the extra small and medium device grid classes by adding .col-xs-* .col-md-* to your columns.

    HTMLCSS. Bootstrap 3 Grid System-2

    You can check the code below:

    <!-- Stack the columns on mobile by making one full-width and the other half-width -->
    <div class="row">
    	<div class="col-xs-12 col-md-8">.col-xs-12 .col-md-8</div>
    	<div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
    </div>
        
    <!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop -->
    <div class="row">
      <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
      <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
      <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
    </div>
      
    <!-- Columns are always 50% wide, on mobile and desktop -->
    <div class="row">
      <div class="col-xs-6">.col-xs-6</div>
      <div class="col-xs-6">.col-xs-6</div>
    </div>
      
  4. Mobile, tablet, desktops. We are going to make more dynamic layouts with tablet .col-sm-* classes based on our previous example.

    HTMLCSS. Bootstrap 3 Grid System-3

    You can check the code below:

    <div class="row">
    	<div class="col-xs-12 col-sm-6 col-md-8">.col-xs-12 .col-sm-6 .col-md-8</div>
    	<div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
    </div>
    <div class="row">
    	<div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
    	<div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
    	<!-- Optional: clear the XS cols if their content doesn't match in height -->
    	<div class="clearfix visible-xs-block"></div>
    	<div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
    </div>
    
  5. Column wrapping. If more than 12 columns are placed within a single row, each group of extra columns will, as one unit, wrap onto a new line.

    HTMLCSS. Bootstrap 3 Grid System-4

    You can check the code below:

    <div class="row">
        <div class="col-xs-9">.col-xs-9</div>
        <div class="col-xs-4">.col-xs-4<br>Since 9 + 4 = 13 &gt; 12, this 4-column-wide div gets wrapped onto a new line as one contiguous unit.</div>
        <div class="col-xs-6">.col-xs-6<br>Subsequent columns continue along the new line.</div>
    </div>
    
  6. Responsive column resets. With the four tiers of grids available you’re bound to run into issues where, at certain breakpoints, your columns don’t clear quite right as one is taller than the other. To fix that, use a combination of a .clearfix and our responsive utility classes.

    HTMLCSS. Bootstrap 3 Grid System-5

    You can check the code below:

    <div class="row">
    	<div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
    	<div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
    
    <!-- Add the extra clearfix for only the required viewport -->
    	<div class="clearfix visible-xs-block"></div>
    	<div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
    	<div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
    </div>
    
  7. Offsetting columns. Move columns to the right using .col-md-offset-* classes. These classes increase the left margin of a column by * columns. For example, .col-md-offset-4 moves .col-md-4 over four columns.

    HTMLCSS. Bootstrap 3 Grid System-6

    You can check the code below:

    
    <div class="row">
    	<div class="col-md-4">.col-md-4</div>
    	<div class="col-md-4 col-md-offset-4">.col-md-4 .col-md-offset-4</div>
    </div>
    <div class="row">
    	<div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
    	<div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
    </div>
    <div class="row">
    	<div class="col-md-6 col-md-offset-3">.col-md-6 .col-md-offset-3</div>
    </div>
    
    
  8. Nesting columns. To nest your content with the default grid, add a new .row and set of .col-sm-* columns within an existing .col-sm-* column. Nested rows should include a set of columns that add up to 12 or less (it is not required that you use all 12 available columns).

    HTMLCSS. Bootstrap 3 Grid System-7

    You can check the code below:

    <div class="row">
    	<div class="col-sm-9">
    	Level 1: .col-sm-9
    		<div class="row">
    		<div class="col-xs-8 col-sm-6">
    		Level 2: .col-xs-8 .col-sm-6
    		</div>
    			<div class="col-xs-4 col-sm-6">
    			Level 2: .col-xs-4 .col-sm-6
    		</div>
    		</div>
    	</div>
    </div>
    
  9. Column ordering. You can change the order of our built-in grid columns with .col-md-push-* and .col-md-pull-* modifier classes.

    HTMLCSS. Bootstrap 3 Grid System-8

    You can check the code below:

    <div class="row">
    	<div class="col-md-9 col-md-push-3">.col-md-9 .col-md-push-3</div>
    	<div class="col-md-3 col-md-pull-9">.col-md-3 .col-md-pull-9</div>
    </div>
    

Bootstrap Responsive Utilities Classes

  1. You can use the following responsive classes to enable the visibility of elements on certain devices that screen sizes falls with the specific range. As of v3.2.0, the .visible-*-* classes for each breakpoint come in three variations, one for each CSS display property value: inline, block and inline-block.

    Class Description
    .visible-xs-*
    Makes the element visible only on extra small devices having screen width less than 768px. Hidden on others.
    .visible-sm-*
    Makes the element visible only on small devices having screen width greater than or equal to 768px (i.e. ≥768px) but less than 992px. Hidden on others.
    .visible-md-*
    Makes the element visible only on medium devices having screen width greater than or equal to 992px (i.e. ≥992px) but less than 1200px. Hidden on others.
    .visible-lg-*
    Makes the element visible only on large devices having screen width greater than or equal to 1200px (i.e. ≥1200px). Hidden on others.
  2. Similarly you can use these hidden utility classes to hide the elements on certain devices:

    Class Description
    .hidden-xs
    Hide the elements only on extra small devices having screen width less than 768px. Visible on others.
    .hidden-sm
    Hide the elements only on small devices having screen width greater than or equal to 768px (i.e. ≥768px) but less than 992px. Visible on others.
    .hidden-md
    Hide the elements only on medium devices having screen width greater than or equal to 992px (i.e. ≥992px) but less than 1200px. Visible on others.
    .hidden-lg
    Hide the elements only on large devices having screen width greater than or equal to 1200px (i.e. ≥1200px). Visible on others.
  3. You can use the following utility classes to show or hide certain elements for printing purpose or devices.

    .visible-print-block
    Hide block elements for Browser rendering while visible for print.
    .visible-print-inline
    Hide inline elements for Browser rendering while visible for print.
    .visible-print-inline-block
    Hide inline-block elements for Browser rendering while visible for print.
    .hidden-print
    Hide elements for printing while visible on Browser.