资源描述
本作品由VentLam创作,采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。
BootStrap入门教程 (一)
2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用、优雅、灵活、可扩展的前端工具集--BootStrap。Bootstrap由MARK OTTO和Jacob Thornton所设计和建立,在github上开源之后,迅速成为该站上最多人watch&fork的项目。大量工程师踊跃为该项目贡献代码,社区惊人地活跃,代码版本进化非常快速,官方文档质量极其高(可以说是优雅),同时涌现了许多基于Bootstrap建设的网站:界面清新、简洁;要素排版利落大方。如下图所示:
GitHub上这样介绍 bootstrap:简单灵活可用于架构流行的用户界面和交互接口的html,css,javascript工具集。基于html5、css3的bootstrap,具有大量的诱人特性:友好的学习曲线,卓越的兼容性,响应式设计,12列格网,样式向导文档,自定义JQuery插件,完整的类库,基于Less等。本系列教程遵循官方文档结构来介绍bootstrap,包括手脚架(Scaffolding),基础CSS,组件,javascript插件,使用LESS与自定义.本文主要介绍Bootstrap的基础布局--Scaffolding.
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>
<htmllang="en"> ...
</html>
2 同时,它通过Bootstrap.less文件来设置全局的排版和连接显示风格.其中去掉了Body的margin,使用@baseFontFamily,@baseFontSize,@linkColor等变量来控制基本排版。
3 格网系统(Grid System).默认的Bootstrap格网系统提供一个宽达940像素的,12列的格网。这意味着你页面默认宽度是940px,最小的单元要素宽度是940/12px.Bootstrap能够使得你的网页可以更好地适应多种终端设备(平板电脑,智能手机等)。默认格网系统直观概念如图1-1所示: 图1-1 默认格网系统(Default Grid System) 以下简单的代码则是实现图1-1中,第三列的宽度为4和宽度为8的两个div.
<divclass="row">
<divclass="span4">...</div>
<divclass="span8">...</div>
</div>
2.2 偏移列. 有时候,页面要素前面需要一些空白,bootstrap提供了偏移列来实现,如图1-2所示:
图1-2 偏移列(Offset columns)
以下代码实现了是实现图1-2中,第一列的宽度为4和偏移度为4宽度为4的两个div.
<divclass="row">
<divclass="span4">...</div>
<divclass="span4 offset4">...</div>
</div>
2.3 嵌套列. 嵌套列是容许用户实现更复杂的页面要素布局。在bootstrap中实现嵌套列非常简单,只需要在原有的div中加入.row 和相应的长度的span* div即可。 如图1-3所示:
图1-3 嵌套列(Nesting columns)
以下代码实现了是实现图1-3中,第一层的宽度为12和第二层两个宽度为6的两个div.
<divclass="row">
<divclass="span12"> Level 1 of column
<divclass="row">
<divclass="span6">Level 2</div>
<divclass="span6">Level 2</div>
</div>
</div>
</div>
嵌套的div长度之和不能大于它的父div,否则会溢出叠加。各位可以试试将第一层的div长度改为其他值,看看效果。
4 流式格网系统(Fluid grid system).它使用%,而不是固定的px,来确定页面要素的宽度.只需要简单的将.row 改成.row-fluid ,就可以将fixed行改为fluid.如图1-4所示: 图1-4 流式格网系统(Fluid grid system) 以下代码实现了是实现图1-4中,两个不同长度的流式页面要素.
<divclass="row-fluid">
<divclass="span4">...</div>
<divclass="span8">...</div>
</div>
嵌套的流式格网和嵌套的固定格网,稍微有些不同。嵌套流式格网(Fluid nesting)的子要素不用匹配父要素的宽度,子要素用100%来表示占满父要素的页面宽度。
自定义格网(Grid customization).Bootstrap允许通过修改variables.less的参数值来自定义格网系统。主要包括如表1-1所示的变量:
变量
默认值
说明
@gridColumns
12
列数
@gridColumnWidth
60px
每列的宽度
@gridGutterWidth
20px
列间距
表1-1 格网变量 我们通过修改以上值,并重新编译Bootstrap来实现自定义格网系统。如果添加新的列,需要同时修改grid.less.同样的,需要修改responsive.less来获得多设备兼容.
5 布局(Layout).本文最后我们讨论创建页面基础模板的布局。如前面所言,Bootstrap提供两种布局方式,包括固定(Fixed)和流式(Fliud)布局。如图1-5所示,左边为Fixed布局,右边为Fluid布局: 图1-5 布局(Layout) 固定布局代码如下:
<body>
<divclass="container"> ...
</div>
</body>
流式布局代码如下:
<divclass="container-fluid">
<divclass="row-fluid">
<divclass="span2">
<!--Sidebar content-->
</div>
<divclass="span10">
<!--Body content-->
</div>
</div>
</div>
如果对Bootstrap提供的布局不够满意,可以参见Less Frame Work 提供的模板。
最后,再次强调,官方文档极其优秀,强烈推荐各位直接参考和学习之。
参考文献与延伸阅读:
1.Bootstrap的来由和发展。
2.Less与Sass的介绍与对比.
3.Html5模板
4.Html5与Bootstrap混合项目(H5BP)
5.20个有用的Bootstrap资源
6.Bootstrap按钮生成器 http://charliepark.org/bootstrap_buttons/
7.前后端结合讨论
8. Bootstrap英文教程
BootStrap入门教程 (二)
上讲回顾:Bootstrap的手脚架(Scaffolding)提供了固定(fixed)和流式(fluid)两种布局,它同时建立了一个宽达940px和12列的格网系统。
基于手脚架(Scaffolding)之上,Bootstrap的基础CSS(Base CSS)提供了一系列的基础Html页面要素,旨在为用户提供新鲜、一致的页面外观和感觉。本文将主要深入讲解排版(Typography),表格(Table),表单(Forms),按钮(Buttons)这四个方面的内容。
1 排版 (Typography),它囊括标题(Headings),段落 (paragraphs), 列表(lists)以及其他内联要素。我们可以通过修改variables.less的两个变量:@baseFontSize 和 @baseLineHeight来控制整体排版的样式。Bootstrap同时还用了一些其他的算术方法来创建所有类型要素的margin,padding,line-height等。
1.1 标题和段落使用常见的html<h*></h*>和<p></P>即可表现,可以不必考虑margin,padding。两者显示效果如图2-1所示:
图2-1 标题与段落(Headings¶graphs)
1.2 强调(Emphasis).使用<strong>和<em>两个标签,前者使用粗体,后者使用斜体来强调标签内容。请注意<strong>标签在html4中定义语气更重的强调文本;在 HTML 5 中,<strong> 定义重要的文本。这些短语标签也可以通过定义CSS的方式来丰富效果。更多短语标签请参见[1].缩写(abbreviations ).使用<abbr>,它重新封装了该标签,鼠标滑过会异步地显示缩写的含义。引入title属性来显示原文,使用.initialism类对缩写以大写方式显示。
1.3 引用文字(Blockquotes).使用<blockquote>标签和<small>两个标签,前者<blockquote>是引用的文字内容,后者<small>是可选的要素,能够添加书写式的引用,比如内容作者。如图2-2所示
图2-2 引用(Blockquotes)
代码片段如下所示:
<divclass="row">
<divclass="span6 ">
<blockquoteclass="pull-right">
<p>凌冬将至. 我是黑暗中的利剑,长城上的守卫,抵御寒冷的烈焰,破晓时分的光线,唤醒眠者的号角,守护王国的坚盾。</p> 守夜人军团总司令.<small>蒙奇.D.<citetitle="">路飞</cite></small>
</blockquote>
</div>
<divclass="span6 ">
<blockquote>
<p>凌冬将至.
我是黑暗中的利剑,长城上的守卫,抵御寒冷的烈焰,破晓时分的光线,唤醒眠者的号角,守护王国的坚盾。</p> 守夜人军团总司令.<small>蒙奇.D.<citetitle="">路飞</cite></small>
</blockquote>
</div>
</div>
1.4列表(lists).Bootstrap提供三种标签来表现不同类型的列表。<ul>表示无序列表,<ul class="unstyled">表示无样式的无序列表,<ol>表示有序列表,<dl>表示描述列表,<dl class="dl-horizontal">表示竖排描述列表。图2-3较好显示了这几种列表:
图2-3 列表(lists)
2.表格(Table).依然使用<table><tr><th><td>等标签来表现表格。主要提供了四个css的类来控制表格的边和结构。表2-1显示了bootstrap的table可选项。
名字
Class
描述
Default
None
没有样式,只有行和列
Basic
.table
只有在行间有竖线
Bordered
.table-bordered
圆角和添加外边框
Zebra-stripe
.table-striped
为奇数行添加淡灰色的背景色
Condensed
.table-condensed
将横向的 padding 对切
表2-1 表格选项(Table Options)
我们可以将这些CSS类结合起来使用,如图2-4所示,显示一个混合的表格:
图2-4 表格(Table)
代码片段如下所示:
View Code
<divclass="span8">
<formclass="form-horizontal">
<fieldset>
<divclass="control-group">
<labelclass="control-label" for="focusedInput">Focused input</label>
<divclass="controls">
<inputclass="input-xlarge focused" id="focusedInput" type="text" value="This is focused…">
</div>
</div>
<divclass="control-group">
<labelclass="control-label">Uneditable input</label>
<divclass="controls">
<spanclass="input-xlarge uneditable-input">Some value here</span>
</div>
</div>
<divclass="control-group">
<labelclass="control-label" for="disabledInput">Disabled input</label>
<divclass="controls">
<inputclass="input-xlarge disabled" id="disabledInput" type="text" placeholder="Disabled input here…" disabled>
</div>
</div>
<divclass="control-group">
<labelclass="control-label" for="optionsCheckbox2">Disabled checkbox</label>
<divclass="controls">
<labelclass="checkbox">
<inputtype="checkbox" id="optionsCheckbox2" value="option1" disabled> This is a disabled checkbox
</label>
</div>
</div>
<divclass="control-group warning">
<labelclass="control-label" for="inputWarning">Input with warning</label>
<divclass="controls">
<inputtype="text" id="inputWarning">
<spanclass="help-inline">Something may have gone wrong</span>
</div>
</div>
<divclass="control-group error">
<labelclass="control-label" for="inputError">Input with error</label>
<divclass="controls">
<inputtype="text" id="inputError">
<spanclass="help-inline">Please correct the error</span>
</div>
</div>
<divclass="control-group success">
<labelclass="control-label" for="inputSuccess">Input with success</label>
<divclass="controls">
<inputtype="text" id="inputSuccess">
<spanclass="help-inline">Woohoo!</span>
</div>
</div>
<divclass="control-group success">
<labelclass="control-label" for="selectError">Select with success</label>
<divclass="controls">
<selectid="selectError">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<spanclass="help-inline">Woohoo!</span>
</div>
</div>
<divclass="form-actions">
<buttontype="submit" class="btn btn-primary">Save changes</button>
<buttonclass="btn">Cancel</button>
</div>
</fieldset>
</form>
</div>
3. 表单(Forms).Bootstrap的表单是非常惊艳的部分。最好的地方在于你如何使用这些hmtl标签,它都会有很好的表现效果,而且不需要其他多余的代码。无论多复杂的布局都可以根据这些简洁,可扩展,事件绑定的要素来轻易实现。主要提供了四四种表单选项,如表2-2所示:
名字
Class
描述
Vertical (default)
.form-vertical (not required)
堆放式, 可控制的左对齐标签
Inline
.form-inline
左对齐标签和简约的内联控制块
Search
.form-search
放大的圆角,具有美感的搜索框
Horizontal
.form-horizontal
左漂浮, 右对齐标签
推荐到官方文档去体验下各种表单要素的真实效果,在chrome,Firefox等现代浏览器下显示十分优雅。同时可以使用.control-group来控制表单输入、错误等状态,需要wekit内核。如图2-5所示:
图2-5 表单状态控制
代码片段如下:
View Code
<divclass="span8">
<formclass="form-horizontal">
<fieldset>
<divclass="control-group">
<labelclass="control-label" for="focusedInput">Focused input</label>
<divclass="controls">
<inputclass="input-xlarge focused" id="focusedInput" type="text" value="This is focused…">
</div>
</div>
<divclass="control-group">
<labelclass="control-label">Uneditable input</label>
<divclass="controls">
<spanclass="input-xlarge uneditable-input">Some value here</span>
</div>
</div>
<divclass="control-group">
<labelclass="control-label" for="disabledInput">Disabled input</label>
<divclass="controls">
<inputclass="input-xlarge disabled" id="disabledInput" type="text" placeholder="Disabled input here…" disabled>
</div>
</div>
<divclass="control-group">
<labelclass="control-label" for="optionsCheckbox2">Disabled checkbox</label>
<divclass="controls">
<labelclass="checkbox">
<inputtype="checkbox" id="optionsCheckbox2" value="option1" disabled> This is a disabled checkbox
</label>
</div>
</div>
<divclass="control-group warning">
<labelclass="control-label" for="inputWarning">Input with warning</label>
<divclass="controls">
<inputtype="text" id="inputWarning">
<spanclass="help-inline">Something may have gone wrong</span>
</div>
</div>
<divclass="control-group error">
<labelclass="control-label" for="inputError">Input with error</label>
<divclass="controls">
<inputtype="text" id="inputError">
<spanclass="help-inline">Please correct the error</span>
</div>
</div>
<divclass="control-group success">
<labelclass="control-label" for="inputSuccess">Input with success</label>
<divclass="controls">
<inputtype="text" id="inputSuccess">
<spanclass="help-inline">Woohoo!</span>
</div>
</div>
<divclass="control-group success">
<labelclass="control-label" for="selectError">Select with success</label>
<divclass="controls">
<selectid="selectError">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<spanclass="help-inline">Woohoo!</span>
</div>
</div>
<divclass="form-actions">
<buttontype="submit" class="btn btn-primary">Save changes</button>
<buttonclass="btn">Cancel</button>
</div>
</fieldset>
</form>
</div>
</div>
4.按钮(Buttons).Bootstrap提供多种样式的按钮,同样是通过CSS的类来控制,包括btn, btn-primary, btn-info,btn-success等不同颜色的按钮,亦可以简单通过.btn-large .btn-mini等CSS的class控制按钮大小,能够同时用在<a>,<button>,<input>标签上,非常简单易用。如图2-6所示,不同颜色的按钮:
图2-6 按钮(Buttons)
代码片段如下:
View Code
<tableclass="table table-bordered table-striped">
<thead>
<tr>
<th>Button</th>
<th>class=""</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><buttonclass="btn" href="#">Default</button></td>
<td><code>btn</code></td>
<td>Standard gray button with gra
展开阅读全文