首页 > 代码库 > <转载>Bootstrap 入门教程 http://www.cnblogs.com/ventlam/archive/2012/05/28/2520703.html 系列

<转载>Bootstrap 入门教程 http://www.cnblogs.com/ventlam/archive/2012/05/28/2520703.html 系列

  Bootstrap建立了一个响应式的12列格网布局系统,它引入了fixed和fluid-with两种布局方式。我们从全局样式(Global Style),格网系统(Grid System),流式格网(Fluid grid System),自定义(Customing),布局(Layouts),响应式设计(Responsive  Design)五个方面深入讲解Boostrap的scaffolding.

  1.  全局样式(Global Style).Bootstrap要求html5的文件类型,所以必须在每个使用bootstrap页面的开头都引用:
    <!DOCTYPE html><html lang="en">  ...</html>
    同时,它通过Bootstrap.less文件来设置全局的排版和连接显示风格.其中去掉了Body的margin,使用@baseFontFamily,@baseFontSize,@linkColor等变量来控制基本排版。
  2.  格网系统(Grid System).默认的Bootstrap格网系统提供一个宽达940像素的,12列的格网。这意味着你页面默认宽度是940px,最小的单元要素宽度是940/12px.Bootstrap能够使得你的网页可以更好地适应多种终端设备(平板电脑,智能手机等)。默认格网系统直观概念如图1-1所示:<iframe id="iframe_0.32631810001940686" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://www.ventlam.org/wp-content/uploads/2012/05/12columns.jpg?_=2520703%22%20style=%22border:none;max-width:966px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById(‘img‘);%20window.parent.postMessage(%7BiframeId:‘iframe_0.32631810001940686‘,width:img.width,height:img.height%7D,%20‘http://www.cnblogs.com‘);%7D%3C/script%3E" frameborder="0" scrolling="no" width="320" height="240"></iframe>                                                                                                                                                            图1-1 默认格网系统(Default Grid System)                                                                                                                                                                 以下简单的代码则是实现图1-1中,第三列的宽度为4和宽度为8的两个div.
    <div class="row">  <div class="span4">...</div>  <div class="span8">...</div></div>

    2.2  偏移列. 有时候,页面要素前面需要一些空白,bootstrap提供了偏移列来实现,如图1-2所示:

    <iframe id="iframe_0.3712764728430027" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://www.ventlam.org/wp-content/uploads/2012/05/offsetcolumns.jpg?_=2520703%22%20style=%22border:none;max-width:966px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById(‘img‘);%20window.parent.postMessage(%7BiframeId:‘iframe_0.3712764728430027‘,width:img.width,height:img.height%7D,%20‘http://www.cnblogs.com‘);%7D%3C/script%3E" frameborder="0" scrolling="no" width="320" height="240"></iframe>

                                                                              图1-2 偏移列(Offset columns)

    以下代码实现了是实现图1-2中,第一列的宽度为4和偏移度为4宽度为4的两个div.

    <div class="row">  <div class="span4">...</div>  <div class="span4 offset4">...</div></div>

    2.3 嵌套列. 嵌套列是容许用户实现更复杂的页面要素布局。在bootstrap中实现嵌套列非常简单,只需要在原有的div中加入.row 和相应的长度的span* div即可。       如图1-3所示:

     <iframe id="iframe_0.6544789853605961" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://www.ventlam.org/wp-content/uploads/2012/05/nestingcolumn.jpg?_=2520703%22%20style=%22border:none;max-width:966px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById(‘img‘);%20window.parent.postMessage(%7BiframeId:‘iframe_0.6544789853605961‘,width:img.width,height:img.height%7D,%20‘http://www.cnblogs.com‘);%7D%3C/script%3E" frameborder="0" scrolling="no" width="320" height="240"></iframe>                                                                                                                                               图1-3     嵌套列(Nesting columns) 

    以下代码实现了是实现图1-3中,第一层的宽度为12和第二层两个宽度为6的两个div.

    <div class="row">  <div class="span12">    Level 1 of column    <div class="row">      <div class="span6">Level 2</div>      <div class="span6">Level 2</div>    </div>  </div></div>

    嵌套的div长度之和不能大于它的父div,否则会溢出叠加。各位可以试试将第一层的div长度改为其他值,看看效果。

  3. 流式格网系统(Fluid grid system).它使用%,而不是固定的px,来确定页面要素的宽度.只需要简单的将.row 改成.row-fluid ,就可以将fixed行改为fluid.如图1-4所示:                                                    <iframe id="iframe_0.3188393151936262" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://www.ventlam.org/wp-content/uploads/2012/05/fluidlayout-1024x386.jpg?_=2520703%22%20style=%22border:none;max-width:966px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById(‘img‘);%20window.parent.postMessage(%7BiframeId:‘iframe_0.3188393151936262‘,width:img.width,height:img.height%7D,%20‘http://www.cnblogs.com‘);%7D%3C/script%3E" frameborder="0" scrolling="no" width="320" height="240"></iframe>                                                                                                                     图1-4 流式格网系统(Fluid grid system)                                                                                                                                                                                  以下代码实现了是实现图1-4中,两个不同长度的流式页面要素.
    <div class="row-fluid">  <div class="span4">...</div>  <div class="span8">...</div></div>

    嵌套的流式格网和嵌套的固定格网,稍微有些不同。嵌套流式格网(Fluid nesting)的子要素不用匹配父要素的宽度,子要素用100%来表示占满父要素的页面宽度。

  4. 自定义格网(Grid customization).Bootstrap允许通过修改variables.less的参数值来自定义格网系统。主要包括如表1-1所示的变量:
    变量默认值说明
    @gridColumns12列数
    @gridColumnWidth60px每列的宽度
    @gridGutterWidth20px列间距

                                                                                                    表1-1 格网变量                                                                                                                                                                                                                                               我们通过修改以上值,并重新编译Bootstrap来实现自定义格网系统。如果添加新的列,需要同时修改grid.less.同样的,需要修改responsive.less来获得多设备兼容.

  5. 布局(Layout).本文最后我们讨论创建页面基础模板的布局。如前面所言,Bootstrap提供两种布局方式,包括固定(Fixed)和流式(Fliud)布局。如图1-5所示,左边为Fixed布局,右边为Fluid布局:                          <iframe id="iframe_0.3483543029466427" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://www.ventlam.org/wp-content/uploads/2012/05/Layout-1024x236.jpg?_=2520703%22%20style=%22border:none;max-width:966px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById(‘img‘);%20window.parent.postMessage(%7BiframeId:‘iframe_0.3483543029466427‘,width:img.width,height:img.height%7D,%20‘http://www.cnblogs.com‘);%7D%3C/script%3E" frameborder="0" scrolling="no" width="320" height="240"></iframe>                                                                                                                    图1-5 布局(Layout)                                                                                                                                                                                           固定布局代码如下:
    <body>  <div class="container">    ...  </div></body>

    流式布局代码如下:

    <div class="container-fluid">  <div class="row-fluid">    <div class="span2">      <!--Sidebar content-->    </div>    <div class="span10">      <!--Body content-->    </div>  </div></div>

    如果对Bootstrap提供的布局不够满意,可以参见Less Frame Work 提供的模板。

    最后,再次强调,官方文档极其优秀,强烈推荐各位直接参考和学习之。http://twitter.github.com/bootstrap/index.html

    参考文献与延伸阅读:

    1.Bootstrap的来由和发展。http://www.alistapart.com/articles/building-twitter-bootstrap/

    2.Less与Sass的介绍与对比.http://coding.smashingmagazine.com/2011/09/09/an-introduction-to-less-and-comparison-to-sass/

    3.Html5模板 http://html5boilerplate.com/

    4.Html5与Bootstrap混合项目(H5BP)https://gist.github.com/1422879

    5.20个有用的Bootstrap资源 http://www.webresourcesdepot.com/20-beautiful-resources-that-complement-twitter-bootstrap/

    6.Bootstrap按钮生成器 http://charliepark.org/bootstrap_buttons/

    7.前后端结合讨论  http://stackoverflow.com/questions/9525170/backend-technology-for-front-end-technologies-like-twitter-bootstrap

    8. Bootstrap英文教程  http://webdesign.tutsplus.com/tutorials/htmlcss-tutorials/stepping-out-with-bootstrap-from-twitter/

     

<iframe id="iframe_0.7228233427756252" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://i.creativecommons.org/l/by-nc-sa/2.5/cn/88x31.png?_=2520703%22%20style=%22border:none;max-width:966px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById(‘img‘);%20window.parent.postMessage(%7BiframeId:‘iframe_0.7228233427756252‘,width:img.width,height:img.height%7D,%20‘http://www.cnblogs.com‘);%7D%3C/script%3E" frameborder="0" scrolling="no" width="320" height="240"></iframe>
本作品由VentLam创作,采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。

<转载>Bootstrap 入门教程 http://www.cnblogs.com/ventlam/archive/2012/05/28/2520703.html 系列