CSS Bootstrap:导航栏列问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23596265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap: navbar column issues
提问by Chris Schmitz
I'm trying to build a navbar in Bootstrap using the column system and I'm running into some problems that I'm not sure what I'm doing wrong.
我正在尝试使用列系统在 Bootstrap 中构建导航栏,但遇到了一些我不确定自己做错了什么的问题。
I would like to end up with a navbar that looks like this:
我想最终得到一个如下所示的导航栏:
I'd like one row that has a left aligned column with the logo and a right aligned column with the user button and then a second row with center aligned buttons.
我想要一行带有徽标的左对齐列和带有用户按钮的右对齐列,然后是带有居中对齐按钮的第二行。
My thought was that I could create two divs with the class "col-lg-6 col-md-6", one for the logo and one for the User Button. These two would create the first 12 column row which would force the tags that follow onto a whole new row.
我的想法是我可以用类“col-lg-6 col-md-6”创建两个 div,一个用于徽标,另一个用于用户按钮。这两个将创建前 12 列的行,这将强制后面的标签进入一个全新的行。
When I try that, though, I end up with everything running inline and out of order.
但是,当我尝试这样做时,我最终发现所有内容都内联且无序运行。
I know I can do it by adding my own custom css (which is how I was able to mock up the first screen shot), but my preference would be to stick to using the bootstrap column system so my ad-hoc css doesn't f something up down the line. I would also like to understand what I did wrong (this is my first project using bootstrap so I'm still trying to get over the learning curve).
我知道我可以通过添加我自己的自定义 css(这是我能够模拟第一个屏幕截图的方式)来做到这一点,但我的偏好是坚持使用引导列系统,这样我的临时 css 就不会下线的东西。我还想了解我做错了什么(这是我使用引导程序的第一个项目,所以我仍在努力克服学习曲线)。
Here's my code for this demo page:
这是我的演示页面代码:
The HTML
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/additions.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="collapse navbar-collapse col-lg-6 col-md-6" >
<ul class="nav navbar-nav navbar-left">
<li><img src="images/genericLogo.png"><!-- <a href="#">MySite.com</a> --></li>
</ul>
</div>
<div class="collapse navbar-collapse navbar-right col-lg-6 col-md-6">
<div class="dropdown">
<button id="userMenu" class="btn dropdown-toggle btn-primary" type="button" data-toggle="dropdown">
<span>[email protected] </span><span class="caret userButton"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="userMenu">
<li role="presentation"><a href="#" tabindex="-1" role="menuitem">View Details</a></li>
<li role="presentation"><a href="#" tabindex="-1" role="menuitem">Edit</a></li>
<li role="presentation" class="divider"></li>
<li role="presentation"><a href="#" tabindex="-1" role="menuitem">Logout</a></li>
</ul>
</div>
</div>
<div class="collapse navbar-collapse" id="mainNav">
<ul id="navlist" class="nav navbar-nav">
<li><a href="#">Reports</a></li>
<li><a href="#">Account Management</a></li>
<li><a href="#">Messages</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">Request a Quote</a></li>
<li><a href="#">MainSite.com</a></li>
</ul>
</div>
<div class="navbar-header">
<button class="navbar-toggle" data-toggle="collapse" data-target="#mainNav">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
</div>
</div>
<div class="container-fluid">
<h1>Test</h1>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-12">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-12">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-12">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-12">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-12">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-6">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-6">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-12">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-12">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-12">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-12">testitem <br />testItem</div>
<div class="demorow col-lg-1 col-md-2 col-sm-6 col-xs-12">testitem <br />testItem</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
The additional css
额外的css
.demorow{
background-color:gray;
padding:10px;
border:1px solid black;
}
/* Centering Navbar items */
.navbar .navbar-nav {
display: inline-block;
float: none;
}
.navbar .navbar-collapse {
text-align: center;
}
/*Adding padding to content for a fixed navbar*/
body {
padding-top: 110px;
}
/*Adding padding to content for a fixed navbar when nav items are collapsed*/
@media screen and (max-width: 768px) {
body { padding-top: 40px; }
}
/* Make dropdown menu items the same width as the dropdown wrapper*/
.dropdown{
width:100%;
}
.dropdown-menu{
width:100%;
}
/* left alight dropdown text*/
.dropdown-menu > li {
text-align:left;
}
Any help would be appreciated.
任何帮助,将不胜感激。
回答by Razor Hyman
You just have to add following div element before following line where
您只需要在以下行之前添加以下 div 元素
<div class="collapse navbar-collapse" id="mainNav">
add following div element
添加以下 div 元素
<div class="clearfix"></div>
so final code looks similar to:
所以最终的代码看起来类似于:
<!-- SOME CODE -->
<div class="clearfix"></div>
<div class="collapse navbar-collapse" id="mainNav">
<!-- SOME CODE -->
回答by Zim
Another approach is to use the nav-justified
in a new row
. Then you can eliminate the custom navbar CSS..
另一种方法是nav-justified
在新的row
. 然后你可以消除自定义导航栏 CSS..
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="collapse navbar-collapse" id="mainNav">
<ul id="navlist" class="nav nav-justified">
<li><a href="#">Reports</a></li>
<li><a href="#">Account Management</a></li>
<li><a href="#">Messages</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">Request a Quote</a></li>
<li><a href="#">MainSite.com</a></li>
</ul>
</div>
</div>
</div>
回答by glimmer
You should not be trying to build a navbar by columns you define yourself but use the bootstrap native navbar which should do what you want it to and collapses really nicely into a menu button for smaller screens. Docs are here: https://getbootstrap.com/components/#navbar. Good Luck.
您不应该尝试通过您自己定义的列来构建导航栏,而是使用引导程序原生导航栏,它应该按照您的意愿行事,并且非常好地折叠成用于较小屏幕的菜单按钮。文档在这里:https: //getbootstrap.com/components/#navbar。祝你好运。