引言
Bootstrap 是一个流行的前端框架,它可以帮助开发者快速构建响应式、移动优先的网站和应用程序。Bootstrap4 是 Bootstrap 的最新版本,它带来了许多改进和新特性。本文将带你从基础布局到组件应用,一步步掌握 Bootstrap4。
安装 Bootstrap4
首先,你需要将 Bootstrap4 添加到你的项目中。你可以从 Bootstrap 的官方网站下载 Bootstrap4 的压缩包,或者使用 CDN 链接直接引入。
使用 CDN 链接
<!-- 引入 Bootstrap4 CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css">
<!-- 引入 Bootstrap4 JS 和依赖的 Popper.js 和 jQuery -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.9.5/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.min.js"></script>
下载 Bootstrap4 压缩包
- 访问 Bootstrap 官网下载 Bootstrap4 压缩包。
- 解压压缩包,将
css和js文件夹中的内容分别放入你的项目中的相应文件夹。
基础布局
Bootstrap4 提供了一系列的栅格系统,可以帮助你快速创建响应式布局。
栅格系统
Bootstrap4 的栅格系统基于 12 列的布局,你可以通过修改列的数量来控制内容在不同屏幕上的显示方式。
<div class="container">
<div class="row">
<div class="col-md-6">左侧内容</div>
<div class="col-md-6">右侧内容</div>
</div>
</div>
在上面的例子中,.container 类用于创建一个响应式容器,.row 类用于创建一行,.col-md-6 类表示在中等及以上屏幕尺寸上,该列占 6 列宽度。
响应式工具
Bootstrap4 提供了响应式工具类,可以帮助你根据不同的屏幕尺寸调整样式。
<div class="container">
<div class="row">
<div class="col-6 col-md-4 col-lg-3">列宽调整</div>
</div>
</div>
在上面的例子中,.col-6 表示在手机屏幕上占 6 列宽度,.col-md-4 表示在平板屏幕上占 4 列宽度,.col-lg-3 表示在大屏幕上占 3 列宽度。
组件应用
Bootstrap4 提供了丰富的组件,可以帮助你快速构建网页。
按钮
<button type="button" class="btn btn-primary">按钮</button>
<button type="button" class="btn btn-secondary">按钮</button>
<button type="button" class="btn btn-success">按钮</button>
<button type="button" class="btn btn-danger">按钮</button>
<button type="button" class="btn btn-warning">按钮</button>
<button type="button" class="btn btn-info">按钮</button>
<button type="button" class="btn btn-light">按钮</button>
<button type="button" class="btn btn-dark">按钮</button>
表格
<table class="table">
<thead>
<tr>
<th scope="col">编号</th>
<th scope="col">姓名</th>
<th scope="col">年龄</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>张三</td>
<td>18</td>
</tr>
<tr>
<td>2</td>
<td>李四</td>
<td>20</td>
</tr>
</tbody>
</table>
卡片
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="..." alt="...">
<div class="card-body">
<h5 class="card-title">卡片标题</h5>
<p class="card-text">这是一些关于卡片的文本。</p>
<a href="#" class="btn btn-primary">按钮</a>
</div>
</div>
总结
通过本文的介绍,相信你已经对 Bootstrap4 有了一定的了解。从基础布局到组件应用,Bootstrap4 可以帮助你快速构建响应式、美观的网页。希望本文能帮助你轻松掌握 Bootstrap4。
