Bootstrap是由Twitter公司推出的一个前端开发框架,因为其灵活性、易用性和兼容性,被越来越多的前端工程师使用。本文将详细介绍如何在网页中使用Bootstrap的导航条、下拉菜单、轮播和栅格布局等常用功能。
一、导航条
导航条是网站中必不可少的部分之一。Bootstrap提供了一些样式和组件,使得我们能够快速地创建漂亮的导航条。以下是一个创建Bootstrap导航条的示例代码:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">LOGO</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="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
以上代码会创建一个带有LOGO、三个菜单项和下拉按钮的导航条。当屏幕宽度小于某一阈值时,菜单项会自动折叠,只剩下下拉按钮。点击下拉按钮后,会弹出菜单项。具体说明见下方示例一。
二、下拉菜单
下拉菜单是Bootstrap非常实用的组件之一,可以创建选择性较多的菜单项。以下是一个创建Bootstrap下拉菜单的示例代码:
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown button
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Action 1</a>
<a class="dropdown-item" href="#">Action 2</a>
<a class="dropdown-item" href="#">Action 3</a>
</div>
</div>
以上代码会创建一个下拉按钮,点击后会弹出三个菜单项。具体说明见下方示例二。
三、轮播
轮播是网页设计中比较重要的元素之一,可以用来展示一系列图片或其他内容。Bootstrap提供了一个非常方便的轮播组件,以下是一个创建Bootstrap轮播的示例代码:
<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 class="d-block w-100" src="img/slide1.jpg" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="img/slide2.jpg" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="img/slide3.jpg" alt="Third slide">
</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定位和排版的最核心部分,可以方便地让一个网页在不同的设备上呈现不同的效果。以下是一个创建Bootstrap栅格布局的示例代码:
<div class="container">
<div class="row">
<div class="col-sm-4">Column 1</div>
<div class="col-sm-4">Column 2</div>
<div class="col-sm-4">Column 3</div>
</div>
</div>
以上代码会创建一个带有三列的栅格布局,当屏幕宽度小于sm的时候,列会自动折叠。具体说明见下方示例四。
示例一
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Logo</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">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Product</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Product 1</a>
<a class="dropdown-item" href="#">Product 2</a>
<a class="dropdown-item" href="#">Product 3</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
以上代码会创建一个带有logo、导航菜单、下拉菜单、搜索框的导航条。当屏幕大小变化时,会自动折叠到下拉按钮内。
示例二
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown button
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Action 1</a>
<a class="dropdown-item" href="#">Action 2</a>
<a class="dropdown-item" href="#">Action 3</a>
</div>
</div>
以上代码会创建一个下拉按钮,点击后会弹出三个选项。
示例三
<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 class="d-block w-100" src="img/slide1.jpg" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="img/slide2.jpg" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="img/slide3.jpg" alt="Third slide">
</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>
以上代码会创建一个带有三张轮播图片、左右箭头和小圆点的轮播。
示例四
<div class="container">
<div class="row">
<div class="col-sm-4">Column 1</div>
<div class="col-sm-4">Column 2</div>
<div class="col-sm-4">Column 3</div>
</div>
</div>
以上代码会创建一个带有三列的栅格布局,当屏幕宽度小于sm的时候,列会自动折叠。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:bootstrap学习使用(导航条、下拉菜单、轮播、栅格布局等) - Python技术站