如何使用bootstrap将菜单集中在一起?
HTML:
<nav class='navbar navbar-default'>
<div class='container-fluid container'>
<div class='navbar-header'>
<a class='navbar-brand' href='#'>Title</a>
</div>
<div id='navbar' class='navbar-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>
</ul>
</div>
</div>
</nav>
这是基本的,但我尝试了各种解决方案,它不会使菜单居中,它总是显示左对齐。我可以将列表元素居中,而不是列表ITSELF。
答案 0 :(得分:1)
@media (min-width:768px) {
/* centered navigation */
.nav.navbar-nav {
float: left;
}
.nav.navbar-nav {
clear: left;
float: left;
margin: 0;
padding: 0;
position: relative;
left: 50%;
text-align: center;
}
.nav.navbar-nav > li {
position: relative;
right: 50%;
}
.nav.navbar-nav li {
text-align: left
}
}
答案 1 :(得分:1)
试试这个:
.navbar-default > .container-fluid {
display: flex;
justify-content: center;
}
需要IE10的供应商前缀。 IE10下面不支持。
答案 2 :(得分:0)
使用bootstrap类行并使用第二个.class col-md-6
。col-md-offset-3
这可能会对你有所帮助
检查jsbin
中的输出
@charset "utf-8";
/* CSS Document */
body{
margin:0;
padding:0;
}
.navbar.navbar-default{
background-image:none;
background-color:rgba(89,63,127,0.79);
border:none;
}
.navbarheader{
color:#FFFFFF;
margin-top:-10px;
font-family: 'Lobster', cursive;
}
.nav,.navbar-nav{
font-size:15px;
color:white;
}
<html>
<head>
<title>Landing Page</title>
<link rel="stylesheet" type="text/css" href="MylandingSS.css"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity= "sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet"/>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top">
<!----------------HEADER------------------------------>
<div class="navbar-header navbar-brand header"><h1 class="navbarheader">abcdefg</h1></div>
<!------------------LINKS----------------------------------->
<div class="row">
<div class="col-md-6 col-md-offset-3">
<ul class="nav navbar-nav">
<li><a href="">Page 1</a></li>
<li><a href="">Page 2</a></li>
<li><a href="">Page 3</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
答案 3 :(得分:0)
以整个导航栏的内容为中心,(以两者 品牌和链接为中心)..
.navbar .navbar-header,
.navbar-collapse {
float:none;
display:inline-block;
vertical-align: top;
}
@media (max-width: 768px) {
.navbar-collapse {
display: block;
}
}
http://www.codeply.com/go/1lrdvNH9GI
如果您只想使链接居中,请参阅此问题: Center content in responsive bootstrap navbar