我无法将我的内容集中在引导导航栏中。我正在使用 bootstrap 3。我已经阅读了很多帖子,但是使用的 CSS 或方法不适用于我的代码!我真的很沮丧,所以这就像我最后的选择。任何帮助,将不胜感激!
<!DOCTYPE html> <html> <head> <title>Navigation</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Bootstrap --> <link href="css/bootstrap.min.css" rel="stylesheet" media="screen"> <link href="css/style.css" rel="stylesheet" media="screen"> </head> <body> <div class="container"> <nav class="navbar navbar-default" role="navigation"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul class="nav navbar-nav"> <li><a href="#">Link</a></li> <li><a href="#">Link</a></li> <li><a href="#">Link</a></li> <li><a href="#">Link</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></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><a href="#">Separated link</a></li> <li><a href="#">One more separated link</a></li> </ul> </li> </ul> </div><!-- /.navbar-collapse --> </nav> <h1>Home</h1> </div> <!--JAVASCRIPT--> <script src="js/jquery-1.10.2.min.js"></script> <script src="js/bootstrap.min.js"></script> </body> </html>
https://jsfiddle.net/amk07fb3/
我想这就是你要找的。您需要float: left从内部导航中删除以使其居中并使其成为内联块。
float: left
.navbar .navbar-nav { display: inline-block; float: none; vertical-align: top; } .navbar .navbar-collapse { text-align: center; }
http://jsfiddle.net/bdd9U/2/
编辑: 如果您只希望在导航未折叠时发生此效果,请在适当的媒体查询中将其包围。
@media (min-width: 768px) { .navbar .navbar-nav { display: inline-block; float: none; vertical-align: top; } .navbar .navbar-collapse { text-align: center; } }
http://jsfiddle.net/bdd9U/3/