在当今的网页设计中,Bootstrap 4 是一个非常受欢迎的前端框架,它提供了丰富的CSS组件和JavaScript插件,使得开发者能够快速构建响应式、移动优先的网页。本文将为您盘点一些实用的Bootstrap 4 CSS实战案例,帮助您更好地掌握这个框架。
案例一:响应式导航栏
导航栏是网页中常见的元素,Bootstrap 4 提供了响应式导航栏组件,可以适应不同屏幕尺寸。以下是一个简单的响应式导航栏案例:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>
</nav>
案例二:轮播图
轮播图是展示多个图片或内容的好方法,Bootstrap 4 提供了轮播图组件。以下是一个简单的轮播图案例:
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="..." class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="..." class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="..." class="d-block w-100" alt="...">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
案例三:表格
表格是展示数据的重要方式,Bootstrap 4 提供了丰富的表格样式。以下是一个简单的表格案例:
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Position</th>
<th scope="col">Office</th>
<th scope="col">Age</th>
<th scope="col">Start date</th>
<th scope="col">Salary</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">John Doe</th>
<td>Manager</td>
<td>New York</td>
<td>28</td>
<td>2012/01/12</td>
<td>$89,815</td>
</tr>
<tr>
<th scope="row">Mike Smith</th>
<td>Designer</td>
<td>San Francisco</td>
<td>24</td>
<td>2012/10/25</td>
<td>$90,000</td>
</tr>
<tr>
<th scope="row">Jane Doe</th>
<td>Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/23</td>
<td>$92,000</td>
</tr>
</tbody>
</table>
案例四:卡片
卡片是Bootstrap 4中非常实用的组件,可以用于展示图片、标题、描述和按钮等信息。以下是一个简单的卡片案例:
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="..." alt="...">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
通过以上实战案例,相信您已经对Bootstrap 4的CSS组件有了更深入的了解。在实际开发中,您可以结合自己的需求,灵活运用这些组件,打造出精美的网页。祝您学习愉快!
