首页 > 网站 > WEB开发 > 正文

Bootstrap 网格系统

2024-04-27 14:35:22
字体:
来源:转载
供稿:网友
Bootstrap 网格系统

自版本 2.3.2 起,Bootstrap 提供了两种类型的网格,默认网格系统和流动网格系统。默认的网格系统是 940px 宽和 12 列。本文主要讲解第一种。

  1. 首先让我们看,怎么来应用默认网格系统
<!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>
View Code

Bootstrap 使用 CSS 的 class "row" 来创建水平行,使用 CSS 的 class "spanx"(x 的值从 1 到 12)来创建垂直列。通过这两个就可以创建一个三列的网格(每一列包含一些文本内容),HTML 如下所示

<!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>
View Code

创建网格的语法如下:

<div class="row"><div class="spanx">inline elements like span, block level elements like p, div.</div> <div> 
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。

Bootstrap 默认网格实例

<!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 clas
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表