bootstrap4
导航
创建一个简单的水平导航栏,可以在ul元素上添加.nav类,在每个li选项上添加.nav-item类,在每个链接上添加.nav-link类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <ul class="nav"> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul>
|
.justify-content-center类设置导航居中显示,.justify-content-end类设置导航右对齐。
1 2 3 4 5
| <!-- 导航居中 --> <ul class="nav justify-content-center"> <!-- 导航右对齐 --> <ul class="nav justify-content-end"> </div>
|
.flex-column 类用于创建垂直导航:
1
| <ul class="nav flex-column">
|
使用.nav-tabs 类可以将导航转化为选项卡。然后对于选中的选项使用 .active 类来标记。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <ul class="nav nav-tabs"> <li class="nav-item"> <a class="nav-link active" href="#">Active</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul>
|
.nav-pills类可以将导航项设置成胶囊形状。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <ul class="nav nav-pills"> <li class="nav-item"> <a class="nav-link active" href="#">Active</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul>
|
.nav-justified 类可以设置导航项齐行等宽显示。
1 2
| <ul class="nav nav-pills nav-justified">..</ul> <ul class="nav nav-tabs nav-justified">..</ul>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <ul class="nav nav-pills"> <li class="nav-item"> <a class="nav-link active" href="#">Active</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#">Dropdown</a> <div class="dropdown-menu"> <a class="dropdown-item" href="#">Link 1</a> <a class="dropdown-item" href="#">Link 2</a> <a class="dropdown-item" href="#">Link 3</a> </div> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <ul class="nav nav-tabs"> <li class="nav-item"> <a class="nav-link active" href="#">Active</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#">Dropdown</a> <div class="dropdown-menu"> <a class="dropdown-item" href="#">Link 1</a> <a class="dropdown-item" href="#">Link 2</a> <a class="dropdown-item" href="#">Link 3</a> </div> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul>
|
如果你要设置选项卡是动态可切换的,可以在每个链接上添加data-toggle="tab"属性。然后在每个选项对应的内容的上添加.tab-pane类。
如果你希望有淡入效果可以在.tab-pane后添加.fade类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <!-- Nav tabs --> <ul class="nav nav-tabs"> <li class="nav-item"> <a class="nav-link active" data-toggle="tab" href="#home">Home</a> </li> <li class="nav-item"> <a class="nav-link" data-toggle="tab" href="#menu1">Menu 1</a> </li> <li class="nav-item"> <a class="nav-link" data-toggle="tab" href="#menu2">Menu 2</a> </li> </ul>
<!-- Tab panes --> <div class="tab-content"> <div class="tab-pane active container" id="home">...</div> <div class="tab-pane container" id="menu1">...</div> <div class="tab-pane container" id="menu2">...</div> </div>
|
胶囊状动态选项卡只需要将以上实例的代码中data-toggle属性设置为data-toggle=“pill”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <!-- Nav pills --> <ul class="nav nav-pills"> <li class="nav-item"> <a class="nav-link active" data-toggle="pill" href="#home">Home</a> </li> <li class="nav-item"> <a class="nav-link" data-toggle="pill" href="#menu1">Menu 1</a> </li> <li class="nav-item"> <a class="nav-link" data-toggle="pill" href="#menu2">Menu 2</a> </li> </ul>
<!-- Tab panes --> <div class="tab-content"> <div class="tab-pane active container" id="home">...</div> <div class="tab-pane container" id="menu1">...</div> <div class="tab-pane container" id="menu2">...</div> </div>
|
导航栏
导航栏一般放在页面的顶部。我们可以使用.navbar类来创建一个标准的导航栏,后面紧跟:.navbar-expand-xl|lg|md|sm 类来创建响应式的导航栏 (大屏幕水平铺开,小屏幕垂直堆叠)。
导航栏上的选项可以使用ul元素并添加class="navbar-nav"类。然后在li元素上添加.nav-item类,a元素上使用.nav-link类。
1 2 3 4 5 6 7 8 9 10 11 12 13
| <nav class="navbar navbar-expand-sm bg-light"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link" href="#">Link 1</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link 2</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link 3</a> </li> </ul> </nav>
|
通过删除.navbar-expand-xl/lg/md/sm类来创建垂直导航栏。
1 2 3 4 5 6 7 8 9 10 11 12 13
| <nav class="navbar bg-light"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link" href="#">Link 1</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link 2</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link 3</a> </li> </ul> </nav>
|
可以使用以下类来创建不同颜色导航栏:.bg-primary,.bg-success,.bg-info,.bg-warning,.bg-danger,.bg-secondary,.bg-dark和.bg-light)。
提示: 对于暗色背景需要设置文本颜色为浅色的,对于浅色背景需要设置文本颜色为深色的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <!-- 灰底黑字 --> <nav class="navbar navbar-expand-sm bg-light navbar-light"> <ul class="navbar-nav"> <li class="nav-item active"> <a class="nav-link" href="#">Active</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul> </nav>
|
.navbar-brand类用于高亮显示品牌/Logo
1 2 3 4
| <nav class="navbar navbar-expand-sm bg-dark navbar-dark"> <a class="navbar-brand" href="#">Logo</a> ... </nav>
|
如果使用图片,可以使用 .navbar-brand 类来设置图片自适应导航栏。
1 2 3 4 5 6
| <nav class="navbar navbar-expand-sm bg-dark navbar-dark"> <a class="navbar-brand" href="#"> <img src="bird.jpg" alt="Logo" style="width:40px;"> </a> ... </nav>
|
通常,小屏幕上我们都会折叠导航栏,通过点击来显示导航选项。要创建折叠导航栏,可以在按钮上添加class=“navbar-toggler”,data-toggle=“collapse"与data-target=”#thetarget"类。然后在设置了class=“collapsenavbar-collapse” 类的div上包裹导航内容(链接),div元素上的id匹配按钮data-target的上指定的id.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <nav class="navbar navbar-expand-md bg-dark navbar-dark"> <!-- Brand --> <a class="navbar-brand" href="#">Navbar</a>
<!-- Toggler/collapsibe Button --> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar"> <span class="navbar-toggler-icon"></span> </button>
<!-- Navbar links --> <div class="collapse navbar-collapse" id="collapsibleNavbar"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> </ul> </div> </nav>
|