我使用Bootstrap 4,将navbar-brand
设置在中间,并将所有元素设置在右侧。我想将我的nav
元素设置在navbar-brand
下。
此外,我想在nav
的右侧设置“登录”和“注销”,但是使用mr-auto
无效。
html:
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<a href="#" class="navbar-brand">Academind</a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarMenu">
<ul class="navbar-nav ">
<li class="nav-item ">
<a href="#" class="nav-link">Users</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Products</a>
</li>
</ul>
<ul class="navbar-nav mr-auto ">
<li class="nav-item ">
<a href="#" class="nav-link">Login</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Log out</a>
</li>
</ul>
</div>
</nav>
css:
@media (min-width: 768px) {
.navbar-brand {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
}
答案 0 :(得分:2)
要右对齐登录名,您可以使用ml-auto
自动填充左侧一侧,这会将链接推到右侧。>
选项1:
然后调整导航栏的min-height
。使用align-items-end
将链接推到底部,然后根据需要在顶部对齐品牌。这样将显示2个导航栏行。
@media (min-width: 567px) {
.navbar-brand
{
position: absolute;
top: 5px;
left: 50%;
transform: translateX(-50%);
}
.navbar {
min-height: 90px;
}
}
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<a href="#" class="navbar-brand">Academind</a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse align-self-end" id="navbarMenu">
<ul class="navbar-nav">
<li class="nav-item">
<a href="#" class="nav-link">Users</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Products</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item ">
<a href="#" class="nav-link">Login</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Log out</a>
</li>
</ul>
</div>
</nav>
https://www.codeply.com/go/07ibCvkQJi
选项2 :(删除多余的CSS)
只需将flex-column
应用于navbar
,然后将品牌和切换器包装在一个flexbox div中。使用自动保证金工具将品牌居中。
<nav class="navbar navbar-expand-sm navbar-dark bg-dark flex-column align-items-stretch">
<div class="d-flex">
<a href="#" class="navbar-brand mx-sm-auto mr-auto">Academind</a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="collapse navbar-collapse w-100" id="navbarMenu">
<ul class="navbar-nav">
<li class="nav-item">
<a href="#" class="nav-link">Users</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Products</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="#" class="nav-link">Login</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Logout</a>
</li>
</ul>
</div>
</nav>
https://www.codeply.com/go/qVJPhAy6fS
答案 1 :(得分:1)
您可以使用bootstrap4类d-flex
,而无需任何额外的CSS:
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="w-100 d-flex flex-column">
<div class="w-100 d-flex justify-content-center align-items-center">
<a href="#" class="navbar-brand">Academind</a>
<button class="navbar-toggler ml-auto" data-toggle="collapse" data-target="#navbarMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="navbarMenu">
<ul class="navbar-nav mr-auto">
<li class="nav-item ">
<a href="#" class="nav-link">Users</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Products</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item ">
<a href="#" class="nav-link">Login</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Logout</a>
</li>
</ul>
</div>
</div></nav>