Bootstrap Grid System
Introduction
In this tutorial, you will learn how to use Bootstrap to create a grid system.
As you may know, in graphic design, a grid system is a two-dimensional structure composed of areas formed by the intersection of horizontal and vertical axes, used to organize content. It is widely used in the layout and content structure of graphic design. In web design, it is a very effective way to quickly and efficiently create consistent layouts using HTML and CSS. Therefore, the grid system has become an important component/module of the framework or workflow in web design.
Simply put, in web design, we use HTML and CSS to create rows and columns to implement a grid. The columns contain the actual content.
Since version 2.3.2, Bootstrap has provided two types of grids. The default grid system is 940px wide with 12 columns. You can add responsive stylesheets to let it adjust its width to 724px and 1170px depending on the viewport.
There is also a fluid grid system, which is based on percentages rather than pixels. It can be extended to be responsive just like the default fixed grid. In this tutorial we will discuss the default grid through some examples, while the fluid grid system will be explained in a separate tutorial.
Please download the latest version of the Bootstrap files from "http://twitter.github.io/bootstrap/assets/bootstrap.zip". You cansee our getting started tutorialto learn about the relevant file structure.
Getting Started with the Default Grid
Let's start with a basic HTML and see how to apply the default grid to it.
使用Bootstrap固定布局的实例
Bootstrap uses the CSS class "row" to create horizontal rows and the CSS class "spanx" (where x ranges from 1 to 12) to create vertical columns. Using these two, you can create a three-column grid (each column contains some text content), with the HTML as follows
使用Bootstrap固定布局的实例
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam cursus. Morbi ut mi. Nullam enim leo, egestas id, condimentum at, laoreet mattis, massa. Sed eleifend nonummy diam. Praesent mauris ante, elementum et, bibendum at, posuere sit amet, nibh. Duis tincidunt lectus quis dui viverra vestibulum. Suspendisse vulputate aliquam dui. Nulla elementum dui ut augue. Aliquam vehicula mi at mauris. Maecenas placerat, nisl at consequat rhoncus, sem nunc gravida justo, quis eleifend arcu velit quis lacus. Morbi magna magna, tincidunt a, mattis non, imperdiet vitae, tellus. Sed odio est, auctor ac, sollicitudin in, consequat vitae, orci. Fusce id felis. Vivamus sollicitudin metus eget eros.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam cursus. Morbi ut mi. Nullam enim leo, egestas id, condimentum at, laoreet mattis, massa. Sed eleifend nonummy diam. Praesent mauris ante, elementum et, bibendum at, posuere sit amet, nibh. Duis tincidunt lectus quis dui viverra vestibulum. Suspendisse vulputate aliquam dui. Nulla elementum dui ut augue. Aliquam vehicula mi at mauris. Maecenas placerat, nisl at consequat rhoncus, sem nunc gravida justo, quis eleifend arcu velit quis lacus. Morbi magna magna, tincidunt a, mattis non, imperdiet vitae, tellus. Sed odio est, auctor ac, sollicitudin in, consequat vitae, orci. Fusce id felis. Vivamus sollicitudin metus eget eros.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam cursus. Morbi ut mi. Nullam enim leo, egestas id, condimentum at, laoreet mattis, massa. Sed eleifend nonummy diam. Praesent mauris ante, elementum et, bibendum at, posuere sit amet, nibh. Duis tincidunt lectus quis dui viverra vestibulum. Suspendisse vulputate aliquam dui. Nulla elementum dui ut augue. Aliquam vehicula mi at mauris. Maecenas placerat, nisl at consequat rhoncus, sem nunc gravida justo, quis eleifend arcu velit quis lacus. Morbi magna magna, tincidunt a, mattis non, imperdiet vitae, tellus. Sed odio est, auctor ac, sollicitudin in, consequat vitae, orci. Fusce id felis. Vivamus sollicitudin metus eget eros.
The following is a graphical representation of the grid system
In this way, by using the "span4" class on each column, we have created a three-column grid. The "container" class is used to hold the entire structure.You can view the online example hereFrom this, we can derive the general syntax of the CSS classes used to create a given number of columns.
General syntax for creating a grid:
inline elements like span, block level elements like p, div.
repeat y times.
where y is the number of columns you want to create and x is the sum equal to 12 (the maximum number of columns you can create). x must be a positive integer, with values from 1 to 12.
For example, if you have three equal-width columns, each uses class="span4"; but if you want the first column to be larger than the other two, you can use class="span6" for the first column and class="span3" for the other two.
How to Create Rows in the Fixed Grid
Next, before we move on to other examples, let's look at the CSS rules used to create rows and columns in the fixed grid.
The row class is as follows
.row {
margin-left: -20px;
*zoom: 1;
}
Set a negative left margin of 20px, and set "zoom: 1;". Here, the "" indicates that the zoom property of all elements is set to 1, to fix IE6/7 bugs. Setting the zoom property to 1 sets an internal property called hasLayout, which is used to fix many zoom/rendering issues in IE6/7.
.row:before,
.row:after {
display: table;
line-height: 0;
content: "";
}
Bootstrap uses the preceding CSS code to create rows. It uses the CSS properties ":before" and ":after". These two are pseudo-elements. ":before" is used to insert some content before the target element, and ":after" is used to insert some content after the target element. "display:table;" makes the element render as a table. By setting "line-height: 0;", it ensures each row has no row height of its own, and by using 'content: ""', it ensures no content is inserted before or after the element.
Then use the following rule to ensure there are no floating elements on the left or right sides of the given element.
.row:after {
clear: both;
}
How to Create Columns in the Fixed Grid
[class*="span"] {
float: left;
min-height: 1px;
margin-left: 20px;
}
This is the CSS rule to use. '[class*="span"]' selects all elements whose class attribute value starts with 'span'. Now "float: left;" is used to position each column next to the others. "min-height: 1px" is used to give all columns a minimum height of 1px, and "margin-left: 20px;" sets the left margin to 20px.
Separate CSS rules are used to set the column widths. As shown in the table below
CSS code
Explanation
.span12 { width: 940px; } | If the row has a single column, the column width is 940px.| onSelect | event |
.span11 {
width: 860px;
}
| If the row has a column merged from 11 columns, the column width is 860px.| onSelect | event |
.span10 {
width: 780px;
}
| If the row has a column merged from 10 columns, the column width is 780px.| onSelect | event |
.span9 {
width: 700px;
}
| If the row has a column merged from 9 columns, the column width is 700px.| onSelect | event |
.span8 {
width: 620px;
}
| If the row has a column merged from 8 columns, the column width is 620px.| onSelect | event |
.span7 {
width: 540px;
}
| If the row has a column merged from 7 columns, the column width is 540px.| onSelect | event |
.span6 {
width: 460px;
}
| If the row has a column merged from 6 columns, the column width is 460px.| onSelect | event |
.span5 {
width: 380px;
}
| If the row has a column merged from 5 columns, the column width is 380px.| onSelect | event |
.span4 {
width: 300px;
}
| If the row has a column merged from 4 columns, the column width is 300px.| onSelect | event |
.span3 {
width: 220px;
}
| If the row has a column merged from 3 columns, the column width is 220px.| onSelect | event |
.span2 {
width: 140px;
}
| If the row has a column merged from 2 columns, the column width is 140px.| onSelect | event |
.span1 {
width: 60px;
}
| A single column has a width of 60px. |
Bootstrap Default Grid Example
This example demonstrates how to create 1 column, 2 columns, 6 columns, 12 columns, and 4 columns (in that order).
Also note that all created columns are wrapped by the "container" class, which is used to create a fixed layout with Bootstrap.
Example
Bootstrap Grid System Example - w3cschool Bootstrap Tutorial
w3cschool.cc is a web design and development tutorial.
w3cschool offers web development tutorials. We believe in Open Source. Love standards. And prioritize simplicity and readability while serving content. With 3000+ unique content pages and thousands of examples, we are comprehensive. We have online practice editors to play around with the example codes.
Some of the topics and more... :






Social networking sites to share:












Fontend Development:
HTML4.0, XHTML1.0, CSS2.1, HTML5, CSS3, JavaScript
Backend Developemt:
PHP, Ruby, Python, Java, ASP.NET, SCALA
Database Management:
SQL, MySQL POstgreSQL, NoSQL, MongoDB
APIs, Tools and Tips:
Google Plus API, Twitter Bootstrap, JSON, Firebug, WebPNG
The following is what you will create

View online
View the example above in different browser windows.
Please click here to download all the HTML, CSS, JS, and image files of the example above.
Adding Responsiveness to the Default Grid
If you want to add responsiveness to Bootstrap's default grid, you only need to add after the native CSS in the HTML file
To view the default grid with responsiveness,click here to view the online demo。click here to download the relevant code。
There are two conditions for the default grid system to be responsive. When the viewport (the available space for rendering the grid) is larger than 1200px [obtained via @media (min-width: 1200px)], and when the viewport is larger than 768px but smaller than 979px [set via @media (min-width: 768px) and (max-width: 979px)].
Offsetting Columns: Default Grid
By using offsets, you can move a column to the right of its original position. This is achieved by adding a left margin to the column. With Bootstrap, you can use the "offsetx" class (where x is a positive integer) and the "spany" class (where y is a positive integer). Depending on the value of 'x' in 'offsetx', the relevant column moves 'x' column widths to the right.
The width of offsets is defined in the Bootstrap CSS. The largest left margin, offset12, is 980px, and the smallest, offset1, is 100px.
Since the default grid system is pixel-based, when applying offsets you must know the pixels you will use for the offset and the pixels you will use for the columns. These two together must not exceed the number of pixels of your horizontal viewport.
In the following example, we will create a two-column grid. Here, we move the left column 4 columns to the right. The HTML code is as follows:
Example
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam cursus. Morbi ut mi. Nullam enim leo, egestas id, condimentum at, laoreet mattis, massa. Sed eleifend nonummy diam. Praesent mauris ante, elementum et, bibendum at, posuere sit amet, nibh. Duis tincidunt lectus quis dui viverra vestibulum. Suspendisse vulputate aliquam dui. Nulla elementum dui ut augue. Aliquam vehicula mi at mauris. Maecenas placerat, nisl at consequat rhoncus, sem nunc gravida justo, quis eleifend arcu velit quis lacus. Morbi magna magna, tincidunt a, mattis non, imperdiet vitae, tellus. Sed odio est, auctor ac, sollicitudin in, consequat vitae, orci. Fusce id felis. Vivamus sollicitudin metus eget eros.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam cursus. Morbi ut mi. Nullam enim leo, egestas id, condimentum at, laoreet mattis, massa. Sed eleifend nonummy diam. Praesent mauris ante, elementum et, bibendum at, posuere sit amet, nibh. Duis tincidunt lectus quis dui viverra vestibulum. Suspendisse vulputate aliquam dui. Nulla elementum dui ut augue. Aliquam vehicula mi at mauris. Maecenas placerat, nisl at consequat rhoncus, sem nunc gravida justo, quis eleifend arcu velit quis lacus. Morbi magna magna, tincidunt a, mattis non, imperdiet vitae, tellus. Sed odio est, auctor ac, sollicitudin in, consequat vitae, orci. Fusce id felis. Vivamus sollicitudin metus eget eros.
click here to view the online demo
click here to download the relevant code
。
You can add responsiveness to the default grid by adding responsive CSS after the native CSS, or by using offset columns.
Nesting Columns: Default Grid
With the Bootstrap default grid, columns can be nested. You simply need to create a row inside a column and create the number of columns you want to nest in that row. At the same time, you must keep in mind the total number of columns used to nest other columns, ensuring it does not exceed the number of columns mentioned when you created the parent column.
The following example demonstrates how to nest columns in the Bootstrap default grid.
Example
Maecenas aliquet velit vel turpis. Mauris neque metus, malesuada nec, ultricies sit amet, porttitor mattis, enim. In massa libero, interdum nec, interdum vel, blandit sed, nulla. In ullamcorper, est eget tempor cursus, neque mi consectetuer mi, a ultricies massa est sed nisl. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Proin nulla arcu, nonummy luctus, dictum eget, fermentum et, lorem. Nunc porta convallis pede.
Maecenas aliquet velit vel turpis. Mauris neque metus, malesuada nec, ultricies sit amet, porttitor mattis, enim. In massa libero, interdum nec, interdum vel, blandit sed, nulla. In ullamcorper, est eget tempor cursus, neque mi consectetuer mi, a ultricies massa est sed nisl. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Proin nulla arcu, nonummy luctus, dictum eget, fermentum et, lorem. Nunc porta convallis pede.
Maecenas aliquet velit vel turpis. Mauris neque metus, malesuada nec, ultricies sit amet, porttitor mattis, enim. In massa libero, interdum nec, interdum vel, blandit sed, nulla. In ullamcorper, est eget tempor cursus, neque mi consectetuer mi, a ultricies massa est sed nisl. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Proin nulla arcu, nonummy luctus, dictum eget, fermentum et, lorem. Nunc porta convallis pede.
click here to view the online demo
click here to download the relevant code
。
In the example above, the row inside the container has two columns, defined as 'class="span7"' and 'class="span5"', making the total number of columns 12. Now, we add a row inside the left column and create two columns, defined as 'class="span4"' and 'class="span3"'. Thus, it respects the total number of columns specified by its parent column (7=4+3).
You can also add responsiveness here.
Conclusion
In this tutorial, we have discussed Bootstrap's default grid system. This tutorial is based on Bootstrap V2.3.2. The following is a summary of the key points in this tutorial:
- Bootstrap's default grid system is 940px wide with 12 columns.
- In the grid, rows are created with 'class="row"' and columns with 'class="spanx"', where x is a positive integer. The sum of all x values for the columns must not exceed 12.
- By adding Bootstrap's responsive CSS, you can add responsiveness to the default grid.
- Use offsets to create extra space for columns, by using 'class="offsetx"' where x is a positive integer. If you use offsets, the total number of columns, including the number used for offsets, must not exceed 11.
- Columns can be nested. If nested columns are used, they must also be counted when calculating the total number of columns in the grid (a row in the container).
- Offsets can also be used in nested columns.