Navbar 指導航條,它在移動設備上顯示爲摺疊狀態,在寬屏幕上水平展開。這裏 是一個線上例子。javascript
{提示} 響應式導航條依賴 collapse 插件,定製 Bootstrap 時務必要包含。html
{設備的可訪問性}
務必使用<nav>
元素;若是使用<div>
元素,須要設置屬性role="navigation"
。這樣能讓輔助設備道這是一個導航條。java
一個典型導航條的結構以下:bootstrap
nav.navbar.navbar-default .container .nav-header a.nav-brand
使用 .nav-brand
的標籤通常是 <a>
,也能夠是 <span>
或者 <div>
等標籤。ide
這裏 是在線例子。spa
添加 .navbar-fixed-top
。插件
<nav class="navbar navbar-default navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <a href="#" class="navbar-brand"> GoodPress </a> </div> </div> </nav>
添加 .navbar-fixed-bottom
。code
<nav class="navbar navbar-default navbar-fixed-bottom"> <div class="container"> <div class="navbar-header"> <a href="#" class="navbar-brand"> GoodPress </a> </div> </div> </nav>
<nav class="navbar navbar-default navbar-static-top"> <div class="container"> <div class="navbar-header"> <a href="#" class="navbar-brand"> GoodPress </a> </div> </div> </nav>
靜態頂部導航條與固定在頂部的導航條的區別,兩處:component
.navbar-brand
的內容<nav class="navbar navbar-default"> <div class="container"> <div class="navbar-header"> <a href="#" class="navbar-brand"> GoodPress </a> </div> </div> </nav>
<nav class="navbar navbar-default"> <div class="container"> <div class="navbar-header"> <a href="#" class="navbar-brand"> <img alt="Brand" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/780908/profile/profile-80_2.jpg" height="20" height="20"> </a> </div> </div> </nav>
圖片的寬高都要指定爲 20
,這是最佳排版大小。orm
.navbar-brand
同級內容<nav class="navbar navbar-default"> <div class="container"> <div class="navbar-header"> <a href="#" class="navbar-brand">Brand</a> </div> <button type="button" class="btn btn-default navbar-btn">Sign in</button> </div> </nav>
.navbar-btn
讓按鈕在導航條中居中顯示。
<nav class="navbar navbar-default"> <div class="container"> <div class="navbar-header"> <a href="#" class="navbar-brand">Brand</a> </div> <form class="navbar-form navbar-left" role="search"> <div class="form-group"> <input type="text" class="form-control" placeholder="Search"> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </nav>
表單必須添加 .navbar-form
,讓表單在導航條中水平、居中顯示。
.navbar-left
是可選的,表示將表單放置在導航欄左邊;還有對應的 .navbar-right
,表示將表單放置在導航欄右邊。
<nav class="navbar navbar-default"> <div class="container"> <div class="navbar-header"> <a href="#" class="navbar-brand">Brand</a> </div> <p class="navbar-text">Signed in as Mark Otto</p> </div> </nav>
右對齊文本——添加 .navbar-right
。
<nav class="navbar navbar-default"> <div class="container"> <div class="navbar-header"> <a href="#" class="navbar-brand">Brand</a> </div> <p class="navbar-text navbar-right">Signed in as Mark Otto</p> </div> </nav>
<nav class="navbar navbar-default"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Project name</a> </div> <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li role="separator" class="divider"></li> <li class="dropdown-header">Nav header</li> <li><a href="#">Separated link</a></li> <li><a href="#">One more separated link</a></li> </ul> </li> </ul> <ul class="nav navbar-nav navbar-right"> <li class="active"><a href="./">Default <span class="sr-only">(current)</span></a></li> <li><a href="../navbar-static-top/">Static top</a></li> <li><a href="../navbar-fixed-top/">Fixed top</a></li> </ul> </div><!--/.nav-collapse --> </div><!--/.container --> </nav>
http://getbootstrap.com/components/#navbar
(完)