English 中文(简体)
Bootstrap Tutorial

Bootstrap with CSS

Bootstrap Layout Components

Bootstrap Plugins

Bootstrap Demos

Bootstrap Useful Resources

Selected Reading

Bootstrap - Grid System
  • 时间:2025-02-21

Bootstrap - Grid System


Previous Page Next Page  

In this chapter we shall discuss the Bootstrap Grid System.

What is a Grid?

As put by wikepedia −

In graphic design, a grid is a structure (usually two-dimensional) made up of a series of intersecting straight (vertical, horizontal) pnes used to structure the 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.

To put in simple words, grids in web design organise and structure content, makes the websites easy to scan and reduces the cognitive load on users.

What is Bootstrap Grid System?

As put 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 understand the above statement. Bootstrap 3 is mobile first in the sense that the code for Bootstrap now starts by targeting smaller screens pke mobile devices, tablets, and then “expands” components and grids for larger screens such as laptops, desktops.

Mobile First Strategy

    Content

      Determine what is most important.

    Layout

      Design to smaller widths first.

      Base CSS address mobile device first; media queries address for tablet, desktops.

    Progressive Enhancement

      Add elements as screen size increases.

Working of Bootstrap Grid System

Grid systems 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 apgnment and padding.

    Use rows to create horizontal groups of columns.

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

    Predefined grid classes pke .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 the 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 a really fancy term for "conditional CSS rule". It simply apppes some CSS, based on certain conditions set forth. If those conditions are met, the style is appped.

Media Queries in Bootstrap allow you to move, show and hide content based on the viewport size. Following media queries are used in LESS files to create the key breakpoints in the Bootstrap grid system.

/* Extra small devices (phones, less than 768px) */
/* No media query since this is the default in Bootstrap */

/* Small devices (tablets, 768px and up) */
@media (min-width: @screen-sm-min) { ... }

/* Medium devices (desktops, 992px and up) */
@media (min-width: @screen-md-min) { ... }

/* Large devices (large desktops, 1200px and up) */
@media (min-width: @screen-lg-min) { ... }

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

@media (max-width: @screen-xs-max) { ... }
@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... }
@media (min-width: @screen-md-min) and (max-width: @screen-md-max) { ... }
@media (min-width: @screen-lg-min) { ... }

Media queries have two parts, a device specification and then a size rule. In the above case, the following rule is set −

Let us consider this pne −

@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... }

For all devices no matter what kind 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 summarizes aspects of how Bootstrap grid system works across 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)

30px

(15px on each side of a column)

30px

(15px on each side of a column)

30px

(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 −

<span class = "container">

   <span class = "row">
      <span class = "col-*-*"></span>
      <span class = "col-*-*"></span>
   </span>
   
   <span class = "row">...</span>
	
</span>

<span class = "container">
   ....
</span>

Let us see some simple grid examples −

Responsive column resets

With the four tiers of grids available, you are bound to run into issues where at certain breakpoints, the columns don t clear quite right as one is taller than the other. To fix that, use a combination of a class .clearfix and the responsive utipty classes as shown in the following example −

<span class = "container">
   <span class = "row" >
   
      <span class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
         box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
         
         <p>Lorem ipsum dolor sit amet, consectetur adipisicing ept.</p>
      </span>
      
      <span class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
         box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
         
         <p>Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do 
            eiusmod tempor incididunt ut labore et dolore magna apqua. Ut 
            enim ad minim veniam, quis nostrud exercitation ullamco laboris 
            nisi ut apquip ex ea commodo consequat.</p>
         
         <p>Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do 
            eiusmod tempor incididunt ut.</p>
      </span>

      <span class = "clearfix visible-xs"></span>
      
      <span class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
         box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
         
         <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco 
            laboris nisi ut apquip ex ea commodo consequat.</p>
      </span>
      
      <span class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
         box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
         
         <p>Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do 
            eiusmod tempor incididunt ut labore et dolore magna apqua. Ut 
            enim ad minim</p>
      </span>
      
   </span>
</span>

This will produce the following result −