06 前端之Bootstrap框架
前端之Bootstrap框架
一、简介
Bootstrap 是最受欢迎的 HTML、CSS 和 JS 框架,用于开发响应式布局、移动设备优先的 WEB 项目。
该框架是很多前端框架的鼻祖 学会它几乎可以熟悉很多框架的原理
二、引入方式
地址:https://v3.bootcss.com/
bootstrap需要依赖于jQuery才能正常执行(动态效果)
PS: 第一次使用该框架的时候最好采用本地导入的方式,让pycharm记住bootstrap的关键字。
本地引入(最完整的)
1.引入jQuery
2.引入bootstrap的css文件
3.引入bootstrap的js文件
将下载完的文件夹拖到pycharm编程项目中即可。(推荐3.4版本非常稳定)
CDN引入
1.引入jQuery CDN
2.引入bootstrap css的 CDN
3.引入bootstrap js的 CDN
导入到HTML文件中的<head>即可,如下图
三、布局容器
下面进入第一个重要知识点,学会后就可以自己搭建页面了。
首先了解下多数浏览页面的左右两侧都有空白区域————称谓左右留白
container 左右留白
container-fluid 左右不留白
实际效果图:
四、栅格系统
Bootstrap 提供了一套响应式、移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列,即响应式布局。
该系统能够自动监测浏览器窗口大小,从而根据大小来进行变化。
参数及使用:
row 行 # 一个row就是一行 一行是固定的12份
col-md-1 占几份
col-sm-1 占几份
col-xs-1 占几份
col-lg-1 占几份
实测:
代码:
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
<style>
.c1{
background-color: red;
height: 200px;
border: 5px solid black;
}
</style>
</head>
<body>
<div class="container">
<div class="row"></div>
<div class="col-lg-6 c1"></div>
<div class="col-lg-6 c1"></div>
</div>
</body>
</html>
实际效果图:
这样只是在网页端是分布式布局,但是切换到移动端就变了,所以需要增加代码。
<div class="col-lg-6 c1 col-sm-4"></div>
<div class="col-lg-6 c1 col-sm-8"></div>
我这里将移动端页面改为4、8分成。
以下就是栅格系统需要掌握的代码参数
PS:对于小白来说,掌握这些就足够了!
五、列偏移
列偏移:就是将任意份数的一行,进行向左或向右移动的操作。
col-md-offset-3 向右移动3份
六、表格与表单
6.1表格
编写一个表格,如下折叠代码。
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
// 向右移动3份,居中
<div class="col-md-6 col-md-offset-3">
// 文本直接text-center即可。
<h2 class="text-center">用户数据表单</h2>
// 添加表格的样式,用着三个即可。
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>张三</td>
<td>18</td>
</tr>
</tbody>
<tbody>
<tr>
<td>2</td>
<td>李四</td>
<td>18</td>
</tr>
</tbody>
<tbody>
<tr>
<td>3</td>
<td>王五</td>
<td>18</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
使用框架调整页面样式一般都是操作标签的class属性。
即table中的class,添加了多种样式。
class = "table"
table-hover 悬浮
table-bordered 增加边框
table-striped 增加背景颜色
实际效果图如下:
增加颜色后的body代码如下:
body代码
<body>
<div class="container">
<div class="row">
<!-- 向右移动3份,居中-->
<div class="col-md-6 col-md-offset-3">
<!-- 文本直接text-center即可。-->
<h2 class="text-center">用户数据表单</h2>
<!-- 添加表格的样式,用着三个即可。-->
<table class="table table-hover table-bordered table-striped">
<thead>
<tr class="active">
<th>ID</th>
<th>Name</th>
<th>class="active"</th>
</tr>
</thead>
<tbody>
<tr class="success">
<td>1</td>
<td>张三</td>
<td>class="success"</td>
</tr>
</tbody>
<tbody>
<tr class="info">
<td>2</td>
<td>李四</td>
<td>class="info"</td>
</tr>
</tbody>
<tbody>
<tr class="warning">
<td>3</td>
<td>王五</td>
<td>class="warning"</td>
</tr>
</tbody>
<tbody>
<tr class="danger">
<td>4</td>
<td>赵六</td>
<td>class="danger"</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
实际效果图:
6.2表单form
在form表单的优化页面中,只需记住下面一行代码即可。
<input type="text" class="form-control" disabled>
form-control 优化页面
disbaled 禁止其与用户有任何交互
input输入框内阴影提示:
点击查看代码
<form class="form-inline">
<div class="form-group">
<label class="sr-only" for="exampleInputEmail3">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail3" placeholder="Email">
</div>
<div class="form-group">
<label class="sr-only" for="exampleInputPassword3">Password</label>
<input type="password" class="form-control" id="exampleInputPassword3" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button type="submit" class="btn btn-default">Sign in</button>
</form>
实际效果图如下:
七、按钮
为 a、button 或 input 元素添加按钮类(button class)即可使用 Bootstrap 提供的样式。
想让谁变成按钮,就在他标签内添加class = "btn" 即可。
预定义样式
预定义样式代码
<!-- Standard button -->
<button type="button" class="btn btn-default">(默认样式)Default</button>
<!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
<button type="button" class="btn btn-primary">(首选项)Primary</button>
<!-- Indicates a successful or positive action -->
<button type="button" class="btn btn-success">(成功)Success</button>
<!-- Contextual button for informational alert messages -->
<button type="button" class="btn btn-info">(一般信息)Info</button>
<!-- Indicates caution should be taken with this action -->
<button type="button" class="btn btn-warning">(警告)Warning</button>
<!-- Indicates a dangerous or potentially negative action -->
<button type="button" class="btn btn-danger">(危险)Danger</button>
<!-- Deemphasize a button by making it look like a link while maintaining button behavior -->
<button type="button" class="btn btn-link">(链接)Link</button>
实际效果图如下:
八、组件
如何使用
复制图标下面的代码
不要和其他组件混合使用
图标类不能和其它组件直接联合使用。它们不能在同一个元素上与其他类共同存在。应该创建一个嵌套的 <span> 标签,并将图标类应用到这个 <span> 标签上。
只对内容为空的元素起作用
图标类只能应用在不包含任何文本内容或子元素的元素上。
综上以及学习bootstrap官网内容后,娱乐的代码如下:
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="bootstrap-3.4.1-dist/css/bootstrap-theme.min.css">
<script src="bootstrap-3.4.1-dist/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.min.css">
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">标题</h3>
</div>
<div class="panel-body">
<div class="jumbotron">
<h1>Hello, world!</h1>
<p>...</p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p>
</div>
</div>
</div>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="row">
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://img1.baidu.com/it/u=1875739781,4152007440&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=576" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://img1.baidu.com/it/u=1875739781,4152007440&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=576" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://img1.baidu.com/it/u=1875739781,4152007440&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=576" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://img1.baidu.com/it/u=1875739781,4152007440&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=576" alt="...">
</a>
</div>
</div>
<div class="progress">
<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 45%">
<span class="sr-only">45% Complete</span>
</div>
</div>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="..." alt="...">
<div class="carousel-caption">
...
</div>
</div>
<div class="item">
<img src="..." alt="...">
<div class="carousel-caption">
...
</div>
</div>
...
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="https://img0.baidu.com/it/u=3112218522,2073304928&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1026" alt="...">
<div class="caption">
<h3>Thumbnail label</h3>
<p>...</p>
<p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
</div>
</div>
</div>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="https://img0.baidu.com/it/u=3112218522,2073304928&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1026" alt="...">
<div class="caption">
<h3>Thumbnail label</h3>
<p>...</p>
<p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
</div>
</div>
</div>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="https://img0.baidu.com/it/u=3112218522,2073304928&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1026" alt="...">
<div class="caption">
<h3>Thumbnail label</h3>
<p>...</p>
<p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
</div>
</div>
</div>
</div>
<a href="" class="btn btn-primary">点我</a>
<input type="reset" class="btn btn-info">
<input type="reset" class="btn btn-info btn-sm">
<input type="reset" class="btn btn-info btn-lg">
<input type="reset" class="btn btn-info btn-block">
<h2 class="text-center">用户数据 <span class="glyphicon glyphicon-heart"></span></h2>
<table class="table table-hover table-striped">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>PWD</th>
</tr>
</thead>
<tbody>
<tr class="success">
<td>1</td>
<td>jason</td>
<td>123</td>
</tr>
<tr class="danger">
<td>2</td>
<td>kevin</td>
<td>321</td>
</tr>
<tr class="warning">
<td>3</td>
<td>tony</td>
<td>222</td>
</tr>
</tbody>
<tbody></tbody>
</table>
<h2 class="text-center">用户注册 <i class="fa fa-bath" aria-hidden="true" style="color: red"></i></h2>
<form action="">
<p>username:
<input type="text" class="form-control" value="jason" disabled>
</p>
<p>password:
<input type="text" class="form-control">
</p>
<p>
<input type="radio">男
<input type="radio">女
<input type="radio">其他
</p>
<p>
<select name="" id="" class="form-control">
<option value="">111</option>
<option value="">222</option>
<option value="">333</option>
</select>
</p>
<input type="submit" class="btn btn-danger btn-block">
</form>
<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
<span class="sr-only">Loading...</span>
<i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i>
<span class="sr-only">Loading...</span>
<i class="fa fa-refresh fa-spin fa-3x fa-fw"></i>
<span class="sr-only">Loading...</span>
<i class="fa fa-cog fa-spin fa-3x fa-fw"></i>
<span class="sr-only">Loading...</span>
<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
<span class="sr-only">Loading...</span>
<img src="..." alt="..." class="img-rounded">
<img src="..." alt="..." class="img-circle">
<img src="..." alt="..." class="img-thumbnail">
</div>
</div>
</div>
</body>
</html>