使用bootstrap设置顶部导航,并将导航栏固定,代码如下:
1 <nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top"> 2 <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation"> 3 <span class="navbar-toggler-icon"></span> 4 </button> 5 <a class="navbar-brand" href="#">Navbar</a> 6 7 <div class="collapse navbar-collapse" id="navbarTogglerDemo03"> 8 <ul class="navbar-nav mr-auto mt-2 mt-lg-0"> 9 <li class="nav-item active"> 10 <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> 11 </li> 12 <li class="nav-item"> 13 <a class="nav-link" href="#">Link</a> 14 </li> 15 <li class="nav-item"> 16 <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a> 17 </li> 18 </ul> 19 <form class="form-inline my-2 my-lg-0"> 20 <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"> 21 <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button> 22 </form> 23 </div> 24 </nav> 25 <div class="container-fluid"> 26 <h1>我的第一个 Bootstrap 页面</h1> 27 <p>使用了 .container-fluid,100% 宽度,占据全部视口(viewport)的容器。</p> 28 </div>
代码文本:
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<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="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</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>
<div class="container-fluid">
<h1>我的第一个 Bootstrap 页面</h1>
<p>使用了 .container-fluid,100% 宽度,占据全部视口(viewport)的容器。</p>
</div>
在浏览器里预览,发现导航会把下面的内容遮挡住,发现是因为导航设置了fixed-top,而下面的模块样式没有处理,所以就遮挡住了下面的内容
解决办法:
1.去掉导航的fixed-top样式类
2.添加样式ody{margin-top:60px;/* 或者其他更大的值 */}
done!