Table of Contents
Bootstrap default grid Example
Offset column: Default grid
Nested columns: Default grid
Home Web Front-end HTML Tutorial Bootstrap grid system_html/css_WEB-ITnose

Bootstrap grid system_html/css_WEB-ITnose

Jun 24, 2016 pm 12:05 PM
bootstrap system grid

As of version 2.3.2, Bootstrap provides two types of grids, the default grid system and the fluid grid system. The default grid system is 940px wide and 12 columns. This article mainly explains the first type.

  1. First let’s see how to apply the default grid system

<!DOCTYPE html><html>  <head>    <title>Fixed layout example with Bootstrap</title>    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <!-- Bootstrap -->    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">  </head>  <body>    <script src="http://code.jquery.com/jquery.js"></script>    <script src="bootstrap/js/bootstrap.min.js"></script>  </body></html>
Copy after login

Bootstrap uses CSS classes "row" to create horizontal rows, and the CSS class "spanx" (x values ​​from 1 to 12) to create vertical columns. Using these two, you can create a three-column grid (each column contains some text content), and the HTML looks like this

<!DOCTYPE html><html>  <head>    <title>Fixed layout example with Bootstrap</title>    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <!-- Bootstrap -->    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">  </head>  <body><div class="container"><div class="row">  <div class="span4"><p>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.</p></div>  <div class="span4"><p>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.</p></div>  <div class="span4"><p>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.</p></div></div>  </div>  <script src="http://code.jquery.com/jquery.js"></script>    <script src="bootstrap/js/bootstrap.min.js"></script>  </body></html>
Copy after login

The syntax for creating a grid is as follows:

<div class="row"><div class="spanx">inline elements like span, block level elements like p, div.</div> <div> 
Copy after login

CSS 代码     解释.span12 {  width: 940px;}    如果该行有一个单一的列,列宽为 940px。.span11 {  width: 860px;}    如果该行有一个由 11 列合并的列,列宽为 860px。.span10 {  width: 780px;}    如果该行有一个由 10 列合并的列,列宽为 780px。.span9 {  width: 700px;}    如果该行有一个由 9 列合并的列,列宽为 700px。.span8 {  width: 620px;}    如果该行有一个由 8 列合并的列,列宽为 620px。.span7 {  width: 540px;}    如果该行有一个由 7 列合并的列,列宽为 540px。.span6 {  width: 460px;}    如果该行有一个由 6 列合并的列,列宽为 460px。.span5 {  width: 380px;}    如果该行有一个由 5 列合并的列,列宽为 380px。.span4 {  width: 300px;}    如果该行有一个由 4 列合并的列,列宽为 300px。.span3 {  width: 220px;}    如果该行有一个由 3 列合并的列,列宽为 220px。.span2 {  width: 140px;}    如果该行有一个由 2 列合并的列,列宽为 140px。.span1 {  width: 60px;}    单个列宽为 60px。
Copy after login

Bootstrap default grid Example

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Bootstrap Grid System Example - w3cschool Bootstrap Tutorial</title> <meta name="description" content="Creating a 16 columns Grid with Bootstrap. Learn with examples to create a Grid System in Bootstrap."> <link href="booststrap/css/bootstrap.min.css" rel="stylesheet"> <style>.span12 h1{color:#FE6E4C; font-weight: bold; padding: 5px;}h3 {margin: 10px 0 10px 0;}</style></head><body><div class="container"><div class="row"><div class="span12"><h1>w3cschool.cc is a web design and development tutorial.</h1> </div></div><div class="row"><div class="span12"><p>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.</p> </div></div><div class="row"><div class="span12"><h3>Some of the topics and more... :</h3> </div><div class="span2"><p><img src="images/html5_logo.png" width="140" height="86" alt="html5 logo" /></p> </div><div class="span2"><p><img src="images/javascript-logo.png" width="140" height="86" alt="javascript logo" /></p> </div><div class="span2"><p><img src="images/json.gif" width="140" height="86" alt="JSON logo" /></p> </div><div class="span2"><p><img src="images/php.png" width="140" height="86" alt="PHP logo" /></p> </div><div class="span2"><p><img src="images/mysql-logo.png" width="140" height="86" alt="MySQL logo" /></p> </div><div class="span2"><p><img src="images/browser-statistics.png" width="140" height="86" alt="Browser Statistics logo" /></p> </div></div><div class="row"><div class="span12"><h3>Social networking sites to share:</h3> </div></div><div class="row"><div class="span1"><p><img src="images/gplus.png" width="50" height="49" alt="GPlus logo" /></p> </div><div class="span1"><p><img src="images/twitter.png" width="50" height="38" alt="Twitter logo" /></p> </div><div class="span1"><p><img src="images/orkut.png" width="50" height="55" alt="Orkut logo" /></p> </div><div class="span1"><p><img src="images/ipad.png" width="50" height="53" alt="iPad logo" /></p> </div><div class="span1"><p><img src="images/digo.png" width="50" height="54" alt="Digo logo" /></p> </div><div class="span1"><p><img src="images/zapface.png" width="51" height="53" alt="Zapface logo" /></p> </div><div class="span1"><p><img src="images/facebook.png" width="48" height="53" alt="facebook logo" /></p> </div><div class="span1"><p><img src="images/netvibes.png" width="51" height="53" alt="Netvibes logo" /></p> </div><div class="span1"><p><img src="images/linkedin.png" width="49" height="54" alt="LinkedIn logo" /></p> </div><div class="span1"><p><img src="images/newsvine.png" width="48" height="53" alt="Newsvine logo" /></p> </div><div class="span1"><p><img src="images/blogger.png" width="51" height="53" alt="Blogger logo" /></p> </div><div class="span1"><p><img src="images/reditt.png" width="48" height="57" alt="Reddit logo" /></p> </div></div><div class="row"><div class="span3"><h3>Fontend Development:</h3> <p>HTML4.0, XHTML1.0, CSS2.1, HTML5, CSS3, JavaScript</p></div><div class="span3"><h3>Backend Developemt:</h3> <p>PHP, Ruby, Python, Java, ASP.NET, SCALA</p></div><div class="span3"><h3>Database Management:</h3> <p>SQL, MySQL POstgreSQL, NoSQL, MongoDB</p></div><div class="span3"><h3>APIs, Tools and Tips:</h3> <p>Google Plus API, Twitter Bootstrap, JSON, Firebug, WebPNG</p></div></div></div></body></html>
Copy after login

The effect is as follows:

Offset column: Default grid

By using offsets, you can move a column to the right of its original position. This is accomplished by adding a left margin to the column. With Bootstrap, you can use the "offsetx" (where the value of x is a positive integer) class and the class "spany" (where the value of y is a positive integer). Depending on the value of 'x' in 'offestx', the relevant column is shifted to the right by 'x' column widths.

The offset width is defined in Bootstrap CSS. The highest left margin of offset12 is 980px, and the lowest left margin of offset1 is 100px.

Since the default grid system is pixel based, when applying an offset you must know which pixels you want to use for the offset and which pixels you want to use for the columns. These two combined must not exceed the number of pixels in your horizontal viewport.

In the example below, we will create a two-column grid. In it, we move the left column 4 columns to the right. The HTML code is as follows:

<!DOCTYPE html><html>  <head>    <title>Bootstrap offsetting column example</title>    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <!-- Bootstrap -->    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">  </head>  <body>    <div class="container">    <div class="row"><div class="span4 offset4"><p>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.</p></div><div class="span3"><p>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.</p></div></div></div>    <script src="http://code.jquery.com/jquery.js"></script>    <script src="bootstrap/js/bootstrap.min.js"></script>  </body></html>
Copy after login

The rendering is as follows:

Nested columns: Default grid

Using Bootstrap's default grid, columns can be nested. You simply create a row within the columns and create as many columns as you want nested within the row. At the same time, you must remember that the total number of columns used to nest other columns must not exceed the number of columns mentioned when you created the parent column.

The following example shows how to nest columns in Bootstrap's default grid.

<!DOCTYPE html><html>  <head>    <title>Bootstrap nested columns example</title>    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <!-- Bootstrap -->    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">  </head>  <body>    <div class="container">    <div class="row">    <div class="span7">    <div class="row">    <div class="span4">   <p>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.</p>    </div>   <div class="span3"><p>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.</p>    </div>    </div>   </div>  <div class="span5">  <p>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.</p>    </div>   </div>   </div>    <script src="http://code.jquery.com/jquery.js"></script>    <script src="bootstrap/js/bootstrap.min.js"></script>  </body></html>
Copy after login

The effect is as follows:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the bootstrap search bar How to get the bootstrap search bar Apr 07, 2025 pm 03:33 PM

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

See all articles