一、导航栏
1.基本
导航栏需要使用.navbar 来定义,并使用 .navbar-expand{-sm|-md|-lg|-xl} 用于响应式布局以及使用配色方案Class 。
<nav class="navbar navbar-expand-lg bg-light">
<a href="#" class="navbar-brand">
<img src="http://getbootstrap.com/docs/4.1/assets/brand/bootstrap-solid.svg" alt="" width="30"
class="align-top"> Bootstrap
</a>
<ul class="navbar-nav">
<li class="nav-item"><a href="#" class="nav-link">入门</a></li>
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">城市</a>
<div class="dropdown-menu">
<a href="#" class="dropdown-item">北京</a>
<div class="dropdown-divider"></div>
<a href="#" class="dropdown-item">深圳</a>
</div>
</li>
</ul>
<!-- <div class="navbar-nav">
<a href="#" class="nav-item nav-link">入门</a>
<a href="#" class="nav-item nav-link">布局</a>
</div> -->
</nav>
2.表单
在导航栏中使用 .form-inline放置各种表单控制元件和组件
<nav class="navbar bg-light mt-5">
<a href="#" class="navbar-brand">
<img src="http://getbootstrap.com/docs/4.1/assets/brand/bootstrap-solid.svg" alt="" width="30"
class="align-top"> Bootstrap
</a>
<form action="#" class="form-inline">
<!-- <input type="search" class="form-control mr-sm-2" placeholder="Search">
<button type="submit" class="btn btn-outline-success mt-2 mt-sm-0">Search</button> -->
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">@</span>
</div>
<input type="search" class="form-control mr-sm-2" placeholder="Search">
</div>
<button type="submit" class="btn btn-outline-success mt-2 mt-sm-0">Search</button>
</form>
</nav>
3.文本处理
可以使用.navbar-text选择器来包裹文字-这已经对文本字符串的垂直对齐、水平间距作了处理优化。
<nav class="navbar navbar-expand-lg bg-light mt-5">
<a href="#" class="navbar-brand">
<img src="http://getbootstrap.com/docs/4.1/assets/brand/bootstrap-solid.svg" alt="" width="30"
class="align-top"> Bootstrap
</a>
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a href="#" class="nav-link">入门</a></li>
<li class="nav-item"><a href="#" class="nav-link">布局</a></li>
<li class="nav-item"><a href="#" class="nav-link">内容</a></li>
</ul>
<span class="navbar-text">这是导航当中的文本</span>
</nav>
4.配色方案
<nav class="navbar navbar-expand-lg bg-dark navbar-dark mt-5">
<a href="#" class="navbar-brand">
<img src="http://getbootstrap.com/docs/4.1/assets/brand/bootstrap-solid.svg" alt="" width="30"
class="align-top"> Bootstrap
</a>
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a href="#" class="nav-link">入门</a></li>
<li class="nav-item"><a href="#" class="nav-link">布局</a></li>
<li class="nav-item"><a href="#" class="nav-link">内容</a></li>
</ul>
<form action="#" class="form-inline">
<input type="search" class="form-control mr-sm-2" placeholder="Search">
<button type="submit" class="btn btn-outline-success mt-2 mt-sm-0">Search</button>
</form>
</nav>
5.居中容器
你可以把导航条包裹在一个 .container容器中,从而使之在网页中呈现居中效果
<div class="container">
<nav class="navbar navbar-expand-lg bg-dark navbar-dark mt-5">
<a href="#" class="navbar-brand">
<img src="http://getbootstrap.com/docs/4.1/assets/brand/bootstrap-solid.svg" alt="" width="30"
class="align-top"> Bootstrap
</a>
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a href="#" class="nav-link">入门</a></li>
<li class="nav-item"><a href="#" class="nav-link">布局</a></li>
<li class="nav-item"><a href="#" class="nav-link">内容</a></li>
</ul>
<form action="#" class="form-inline">
<input type="search" class="form-control mr-sm-2" placeholder="Search">
<button type="submit" class="btn btn-outline-success mt-2 mt-sm-0">Search</button>
</form>
</nav>
</div>
6.定位
<nav class="navbar navbar-light bg-light fixed-top">
<a href="#" class="navbar-brand">因定在顶部</a>
</nav>
<nav class="navbar navbar-light bg-light fixed-bottom">
<a href="#" class="navbar-brand">因定在底部</a>
</nav>
<nav class="navbar navbar-light bg-light sticky-top">
<a href="#" class="navbar-brand">粘性定位在浏览器的顶部</a>
</nav>
7.响应式
当内容在按钮后面折叠时,导航栏可以使用 .navbar-toggler、.navbar-collapse 和 .navbar-expand{-sm|-md|-lg|-xl}的 class样式来更改,结合其它常用样式,你可以根据需要定义显示或隐藏特定元素。
<nav class="navbar navbar-expand-md bg-success navbar-dark mt-5">
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbar">
<a href="#" class="navbar-brand">
<img src="http://getbootstrap.com/docs/4.1/assets/brand/bootstrap-solid.svg" alt="" width="30"
class="align-top"> Bootstrap
</a>
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a href="#" class="nav-link">入门</a></li>
<li class="nav-item"><a href="#" class="nav-link">布局</a></li>
</ul>
<form action="#" class="form-inline">
<input type="search" class="form-control mr-sm-2" placeholder="Search">
<button type="submit" class="btn btn-outline-light mt-2 mt-sm-0">Search</button>
</form>
</div>
</nav>
8.拓展
<!-- 左侧是logo,右侧是按钮 -->
<nav class="navbar navbar-expand-md bg-success navbar-dark mt-5">
<a href="#" class="navbar-brand">
<img src="http://getbootstrap.com/docs/4.1/assets/brand/bootstrap-solid.svg" alt="" width="30"
class="align-top"> Bootstrap
</a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbar1">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbar1">
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a href="#" class="nav-link">入门</a></li>
<li class="nav-item"><a href="#" class="nav-link">布局</a></li>
<li class="nav-item"><a href="#" class="nav-link">内容</a></li>
</ul>
<form action="#" class="form-inline">
<input type="search" class="form-control mr-sm-2" placeholder="Search">
<button type="submit" class="btn btn-outline-light mt-2 mt-sm-0">Search</button>
</form>
</div>
</nav>
<!-- 左侧是按钮,右侧是logo -->
<nav class="navbar navbar-expand-md bg-success navbar-dark mt-5">
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbar2">
<span class="navbar-toggler-icon"></span>
</button>
<a href="#" class="navbar-brand">
<img src="http://getbootstrap.com/docs/4.1/assets/brand/bootstrap-solid.svg" alt="" width="30"
class="align-top"> Bootstrap
</a>
<div class="collapse navbar-collapse" id="navbar2">
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a href="#" class="nav-link">入门</a></li>
<li class="nav-item"><a href="#" class="nav-link">布局</a></li>
<li class="nav-item"><a href="#" class="nav-link">内容</a></li>
</ul>
<form action="#" class="form-inline">
<input type="search" class="form-control mr-sm-2" placeholder="Search">
<button type="submit" class="btn btn-outline-light mt-2 mt-sm-0">Search</button>
</form>
</div>
</nav>