我正在使用Bootstrap导航栏创建网站标题,如下所示:
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<a class="navbar-brand all-upper mb-1 h1 ml-1" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-expand">
<button class="btn btn-primary mr-3" onclick="goToTrip()">Button</button>
</div>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link all-upper" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link all-upper" href="#">Settings</a>
</li>
<li class="nav-item">
<a class="nav-link all-upper" href="#">Log Out</a>
</li>
</ul>
</div>
</nav>
因此,我在导航栏的右侧有一些链接,在左侧有一个按钮。
但是,当屏幕变小时,按钮和折叠菜单会像这样改变其位置:
如果没有该按钮,则一切正常:
那么如何将物品保留在原处?
答案 0 :(得分:2)
@media screen and (max-width: 540px) {
.navbar-brand{
flex-grow: 2;
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<a class="navbar-brand all-upper mb-1 h1 ml-1" href="#">Brand</a>
<div class="navbar-expand">
<button class="btn btn-primary mr-3" onclick="goToTrip()">Button</button>
</div>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link all-upper" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link all-upper" href="#">Settings</a>
</li>
<li class="nav-item">
<a class="nav-link all-upper" href="#">Log Out</a>
</li>
</ul>
</div>
</nav>
您可以尝试这个吗?您只是将导航栏切换器放在了错误的位置。
再试一次?这就是你想要的吗?
答案 1 :(得分:0)
<div class="navbar-expand">
<button class="btn btn-primary mr-3" onclick="goToTrip()">Button</button>
</div>
由于您已经在nav标签中定义了navbar-expand
,因此div中不应存在navbar-expand-sm
类
答案 2 :(得分:0)
不需要额外的CSS。只需更改按钮的位置,然后使用mr-auto
使其保持在左侧即可。
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<a class="navbar-brand all-upper mb-1 h1 ml-1" href="#">Brand</a>
<div class="navbar-expand mr-auto">
<button class="btn btn-primary mr-3" onclick="goToTrip()">Button</button>
</div>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarToggle">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link all-upper" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link all-upper" href="#">Settings</a>
</li>
<li class="nav-item">
<a class="nav-link all-upper" href="#">Log Out</a>
</li>
</ul>
</div>
</nav>