我一直在努力使用Bootstrap 3使导航栏居中,我设法通过navbar-center类使导航栏居中,并且运行良好,但是当我在Tablet上查看时,最后一个导航项落下了在DIV下,似乎是造成问题的左手50%。
这是我的导航栏:
<div class="navbar-wrapper">
<div class="container-fluid">
<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>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-center">
<li class="active"><a href="#" class="">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Students</a></li>
<li><a href="#">Members</a></li>
<li><a href="#">News</a></li>
<li><a href="#">Join SAFEE</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</div>
</div>
</nav>
</div>
</div>
这是我的导航栏的CSS:
.navbar, .dropdown-menu{
background:rgba(255,255,255,0.25);
border: none;
}
.nav>li>a, .dropdown-menu>li>a:focus, .dropdown-menu>li>a:hover, .dropdown-menu>li>a, .dropdown-menu>li{
border-bottom: 3px solid transparent;
}
.nav>li>a:focus, .nav>li>a:hover,.nav .open>a, .nav .open>a:focus, .nav .open>a:hover, .dropdown-menu>li>a:focus, .dropdown-menu>li>a:hover{
border-bottom: 3px solid transparent;
background: none;
}
.navbar a, .dropdown-menu>li>a, .dropdown-menu>li>a:focus, .dropdown-menu>li>a:hover, .navbar-toggle{
color: black;
}
.dropdown-menu{
-webkit-box-shadow: none;
box-shadow:none;
}
.nav li:hover:nth-child(8n+1), .nav li.active:nth-child(8n+1){
border-bottom: #C4E17F 3px solid;
}
.nav li:hover:nth-child(8n+2), .nav li.active:nth-child(8n+2){
border-bottom: #F7FDCA 3px solid;
}
.nav li:hover:nth-child(8n+3), .nav li.active:nth-child(8n+3){
border-bottom: #FECF71 3px solid;
}
.nav li:hover:nth-child(8n+4), .nav li.active:nth-child(8n+4){
border-bottom: #F0776C 3px solid;
}
.nav li:hover:nth-child(8n+5), .nav li.active:nth-child(8n+5){
border-bottom: #DB9DBE 3px solid;
}
.nav li:hover:nth-child(8n+6), .nav li.active:nth-child(8n+6){
border-bottom: #C49CDE 3px solid;
}
.nav li:hover:nth-child(8n+7), .nav li.active:nth-child(8n+7){
border-bottom: #669AE1 3px solid;
}
.nav li:hover:nth-child(8n+8), .nav li.active:nth-child(8n+8){
border-bottom: #62C2E4 3px solid;
}
.navbar-toggle .icon-bar{
color: #fff;
background: #fff;
}
**@media (min-width: 768px)
{
.navbar-nav.navbar-center
{
position: absolute;
left: 50%;
transform: translatex(-50%);
}
}**
我已将代码标记为以粗体显示,这是我在http://safee.smudgehost.co.uk/上测试Boostrap的网站,我只是想将自己的NAV居中,因此无论使用哪种设备,它都位于中心,然后在移动设备上折叠为移动导航。
感谢您的帮助。
答案 0 :(得分:2)
这是我最终设法解决的方法。
我在名为center-nav的容器中添加了一个CSS类,并添加了以下CSS。
display: flex;
justify-content: center;
它能胜任工作并且可以在所有屏幕尺寸上工作,感谢您的建议:)