因为公司选择了使用BootStrap3作为项目的前台展示框架,所以花了半天时间来学习Bootstrap, 如果你是第一次听说,或者说以前听说过,但没有使用过这个框架的话,希望这篇入门实战能够让你快速掌握。

什么是BootStrap?

基于HTML,CSS,JAVASCRIPT的简洁灵活的流行前端框架及交互组件集,由Twitter的经验丰富的工程师设计师奉献。说得更具体点就是基于12列的栅格布局的HTML,Javascript使用定制的jQuery插件,用LESS构建CSS等技术构建的响应式设计,跨设备,跨浏览器,而且最主要就是入门门槛非常低,既能用来开发简单的小东西,也能构造更为复杂的应用。完美支持HTML5/CSS 标签和语法,源码托管在GitHub

如何入手?

1,下载bootstrap,官网下载customize components. 使用到的jquery可以使用CDN,直接至官网http://jquery.com的最下面有一段cdn的链接,复制下来即可,至于是使用http还是https由你自己决定。我做的示例就是直接使用的http.

2,工具和将要使用的类库都准备好了,接下来,在notepad++中输入html代码,在我看来,手写html是一种好习惯,先建出框架出来,

<!DOCTYPE html>

<html>
<head>
<title>Bootstrap3</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/bootstrap.min.css" rel="stylesheet">
</head> <body>
<div class="navbar navbar-inverse navbar-static-top">
<div class="container">
<a href="#" class="navbar-brand">BootStrap3 Study</a>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
</body> </html>

navbar_only

3,  个人从这个简单的框架中得出的一个重要的结论就是div布局牢记,不管什么,先最好弄个div套住,然后再二层弄个div并冠以container class是最佳实践。例如要创建一个头部的导航菜单,先创建一个div,然后再弄个二级div叫container,第三层就是菜单内容,可以平行多个。如图示

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap3 Study</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap_study.css" rel="stylesheet">
</head>
<body>
<div class="navbar navbar-inverse navbar-static-top">
<div class="container">
<a class ="navbar-brand" href="#">Bootstrap3</a>
<button class="navbar-toggle" data-toggle="collapse" data-target=".navHeadMenu" >
<span class="icon-bar"/>
<span class="icon-bar"/>
<span class="icon-bar"/>
</button>
<div class="collapse navbar-collapse navHeadMenu">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Blog</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Technologies<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Java</a></li>
<li><a href="#">C#</a></li>
</ul>
</li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
</div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

点开查看全部源代码

bootstrap.css是很语义化的一个css. 真心建议阅读一下源代码。

4, 接下来就建一个底部的navigator, 按照我的体验,先弄个div与head navigator平级,然后里面再弄个container的div. 如下面这样子。

<div class="navbar navbar-default navbar-fixed-bottom">
<div class="container">
<p class="navbar-text pull-left">Create by BP</p>
<a class="navbar-btn btn btn-danger pull-right" href="#">Subscribe on Tudou</a>
</div>
</div>

头部的navigator用的是navbar-static-top, 顶部嘛,当然是top,所以底部使用navbar-fixed-bottom, 靠左靠右使用的语义就是拉到左边,拉到右边。单词之间的空隙用的是-就可以了。

5, 其实,我上面说的有点绝对,并不能说是先弄个div,应该说弄一个有意义的div才合适,换句通俗点的话,就是弄个有class名称的div,然后再套个container的div. 举个例子就是我想在网页的上面显示一点大的字体块,用来说明一些简明扼要的信息,可以使用到jumbtron这个类。那么我的html就是在body的下一级加一个如下的div

        <div class="container">
<div class="jumbotron">
<center>
<h1>Hello, World!</h1>
<p>As a best practice, we highly recommend using the element whenever possible to ensure matching cross-browser rendering.</p>
<a class="btn btn-default" href="#">Watch Now!</a>
<a class="btn btn-info" href="#">Tweet it!</a>
</center>
</div>
</div>

jumbotron是有圆角定义的,如果你将其移到container那个地方的话,圆角就没了,所以还是另套一个div 最为合适。

6,最后讲一个重量级的自适应的表格布局,在做这个布局之前,牢记一点,bootstrap的布局是一个基于12格的布局。作为表格,肯定是有行row和列col的。对于语义化很的bootstrap3自然也会考虑到这些,所以,在上面5点的基础上,可以很快得到一个代码编写流程:container的div下面建一个row的div,然后再在row的下面建一些col即可完成。

<div class="container">
<div class="row">
<div class="col-md-3">
。。。。。
</div>
</div>
</div>

架构是这样子,因为col列里面是占3个格的,共12格,所以col列这个div应该重复4次。

        <div class="container">
<div class="row">
<div class="col-md-3">
<h3><a href = "#">$500 Gaming PC Build</a></h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
<a class="btn btn-default" href="#">Read More</a>
</div>
<div class="col-md-3">
<h3><a href = "#">$500 Gaming PC Build</a></h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
<a class="btn btn-default" href="#">Read More</a>
</div>
<div class="col-md-3">
<h3><a href = "#">$500 Gaming PC Build</a></h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
<a class="btn btn-default" href="#">Read More</a>
</div>
<div class="col-md-3">
<h3><a href = "#">$500 Gaming PC Build</a></h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
<a class="btn btn-default" href="#">Read More</a>
</div>
</div>
</div>

点开查看全部的col-md-3

7, modal窗体的使用,在bootstrap3里面,实现一个模式窗体是不需要写任何js代码的。定义一个modal fade的div,然后千万千万记得设一个id, 至于modal窗体的结构就是:modal-dialog->modal-content->modal-header->modal-body->modal-footer. 如果你很好奇我为什么上一句说千万千万记得设一个id,那么你悟了,如果有id,那么你在类似于a 标签中就可以这样子用<a href="#id" data-toggle="modal">然后你点击这个链接时,就弹出了自定义的模式窗体了。

<div class="modal fade" id="contact">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4>Contact US</h4>
</div>
<div class="modal-body">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
</div>
<div class="modal-footer">
<a class="btn btn-default" data-dismiss="modal">Close</a>
<a class="btn btn-primary" data-dismiss="modal">OK</a>
</div>
</div>
</div>
</div> =============
invoke code like following:
<li><a href="#contact" data-toggle="modal">Contact</a></li>

点出查看详细实现代码

8, 一般的,文章不可能是像6所说的几列显示,一篇文章就是一页是很符合人的阅读习惯的,bootstrap也考虑到了这些,就定义出了一个panel类出来。当然带图片的话,还可以带个featuredImg的class出来。可以定义一个结构:panel panel-default ->panel-body->page-header...

        <div class="container">
<div class="row">
<div class="col-lg-9">
<div class="panel panel-default">
<div class="panel-body">
<div class="page-header">
<h3>Whatever you want <small>Posted on Oct 26th</small></h3>
</div>
<img class="featuredImg" src="img/Lighthouse.jpg" width="100%"/>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
<h4> A heading</h4>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
</div>
</div>
</div>
</div>
</div>

点击这里查看完整的panel结构

9, 由于8中使用的是col-lg-9,剩余3列的,我们用来做SideBar的效果,使用list-group。

                <div class="col-lg-3">
<div class="list-group">
<a href="#" class="list-group-item active">
<h4 class = "list-group-item-heading">Lorem ipsum</h4>
<p class = "list-group-item-text">Fusce consequat eu risus sit amet vehicula. Maecenas auctor odio ipsum. Phasellus convallis est eu cursus lacinia.</p>
</a>
<a href="#" class="list-group-item">
<h4 class = "list-group-item-heading">Lorem ipsum</h4>
<p class = "list-group-item-text">Fusce consequat eu risus sit amet vehicula. Maecenas auctor odio ipsum. Phasellus convallis est eu cursus lacinia.</p>
</a>
<a href="#" class="list-group-item">
<h4 class = "list-group-item-heading">Lorem ipsum</h4>
<p class = "list-group-item-text">Fusce consequat eu risus sit amet vehicula. Maecenas auctor odio ipsum. Phasellus convallis est eu cursus lacinia.</p>
</a>
</div>
</div>

完整sidebar结构点击这里

10,对modal进行一些advance, 可以在modal-content里面加个form-horizontal也是可以的。也就是将modal-header/body/footer将在form里面。

                    <form class="form-horizontal">
<div class="modal-header">
<h4>Contact US</h4>
</div>
<div class="modal-body">
<div class ="form-group">
<label for = "contact-name" class = "col-lg-2 control-label">Name:</label>
<div class = "col-lg-10">
<input type = "text" class = "form-control" id = "contact-name" placeholder = "Full Name">
</div>
</div>
<div class = "form-group">
<label for = "contact-email" class = "col-lg-2 control-label">Email:</label>
<div class = "col-lg-10">
<input type = "email" class = "form-control" id = "contact-email" placeholder = "you@example.com">
</div>
</div>
<div class = "form-group">
<label for = "contact-msg" class = "col-lg-2 control-label">Message:</label>
<div class = "col-lg-10">
<textarea class = "form-control" rows = "8"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<a class="btn btn-default" data-dismiss="modal">Close</a>
<button class="btn btn-primary" type="submit">Send</button>
</div>
</form>

完整的form结构

如此上面这般,第一个自适应的网页就可以说是完成了,有头部菜单导航,有底部说明,有中间算是广告的醒目标题,也有自适应的grid表格布局。

Bootstrap3 入门实战的更多相关文章

  1. Spark入门实战系列--10.分布式内存文件系统Tachyon介绍及安装部署

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Tachyon介绍 1.1 Tachyon简介 随着实时计算的需求日益增多,分布式内存计算 ...

  2. 003-Tuple、Array、Map与文件操作入门实战

    003-Tuple.Array.Map与文件操作入门实战 Tuple 各个元素可以类型不同 注意索引的方式 下标从1开始 灵活 Array 注意for循环的until用法 数组的索引方式 上面的for ...

  3. Scala深入浅出实战经典-----002Scala函数定义、流程控制、异常处理入门实战

    002-Scala函数定义.流程控制.异常处理入门实战 Scala函数定义 语句结束无分号 定义无参函数 def 函数名称(参数名称:参数类型)[:Unit=]{ 函数体 } 老师的代码 我的实际代码 ...

  4. Spark入门实战系列--1.Spark及其生态圈简介

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .简介 1.1 Spark简介 年6月进入Apache成为孵化项目,8个月后成为Apache ...

  5. Spark入门实战系列--2.Spark编译与部署(上)--基础环境搭建

    [注] 1.该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取: 2.Spark编译与部署将以CentOS 64位操作系统为基础,主要是考虑到实际应用 ...

  6. Spark入门实战系列--2.Spark编译与部署(中)--Hadoop编译安装

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .编译Hadooop 1.1 搭建环境 1.1.1 安装并设置maven 1. 下载mave ...

  7. Spark入门实战系列--2.Spark编译与部署(下)--Spark编译安装

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .编译Spark .时间不一样,SBT是白天编译,Maven是深夜进行的,获取依赖包速度不同 ...

  8. Spark入门实战系列--3.Spark编程模型(上)--编程模型及SparkShell实战

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Spark编程模型 1.1 术语定义 l应用程序(Application): 基于Spar ...

  9. Spark入门实战系列--3.Spark编程模型(下)--IDEA搭建及实战

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 . 安装IntelliJ IDEA IDEA 全称 IntelliJ IDEA,是java语 ...

随机推荐

  1. 大整数算法[10] Comba乘法(实现)

    ★ 引子 上一篇文章讲了 Comba 乘法的原理,这次来讲讲如何实现.为了方便移植和充分发挥不同平台下的性能,暂时用了三种不同的实现方式: 1.单双精度变量都有的情况. 2.只有单精度变量的情况. 3 ...

  2. UCOS 请求任务删除函数 及其应用

    有时候,如果任务A拥有内存缓冲区或信号量之类的资源,而任务B想删除该任务,这些资源就可能由于没被释放而丢失.在这种情况下,用户可以想法子让拥有这些资源的任务在使用完资源后,先释放资源,再删除自己.用户 ...

  3. Swift —构造函数与存储属性初始化-备

    构造函数的主要作用是初始化实例,其中包括:初始化存储属性和其它的初始化.在Rectangle类或结构体中,如果在构造函数中初始化存储属性width和height后,那么在定义他们时就不需要初始化了. ...

  4. 关于nginx架构探究(3)

    Nginx 模块综述 Nginx 所有的代码都是以模块的新式组织的,包括核心模块和功能模块.Nginx加载模块的时候不想Apache一样动态加载,它是直接被编译到二进制执行文件中,所以,如果想要加载新 ...

  5. GUI(主)线程与子线程之间的通信(用信号槽通讯)

    在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. 程序截图: 上代码: #include <QtGui> #inc ...

  6. Linux企业级项目实践之网络爬虫(27)——多路IO复用

    与多线程和多进程相比,I/O多路复用的最大优势是系统开销小,系统不需要建立新的进程或者线程,也不必维护这些线程和进程. 主要应用: (1)客户程序需要同时处理交互式的输入和服务器之间的网络连接 (2) ...

  7. JQ无法修改input的type属性的替代解决方法

    需要实现的效果:一个输入框,当输入框未获得焦点的时候,显示为 “请输入密码”:当输入内容并失去焦点的时候,输入内容显示为”*****”,如果没有输入仍然显示“请输入密码”: 方法一:使用text,隐藏 ...

  8. Linux之V4L2视频采集编程详解

     V4L2(Video For Linux Two) 是内核提供给应用程序访问音.视频驱动的统一接口. Linux系统中,视频设备被当作一个设备文件来看待,设备文件存放在 /dev目录下,完整路径的设 ...

  9. 详解Java反射各种应用

    Java除了给我们提供在编译期得到类的各种信息之外,还通过反射让我们可以在运行期间得到类的各种信息.通过反射获取类的信息,得到类的信息之后,就可以获取以下相关内容: Class对象 构造器 变量 方法 ...

  10. 浅析NSTimer & CADisplayLink内存泄露

    偶得前言 NSRunLoop与定时器 - invalidate的作用 我们如何解决? 偶得前言 本篇文章中我们主要谈谈NSTimer\CADisplayLink在使用过程中牵扯到内存泄露的相关问题及解 ...