我正试图设计一个Bootstrap 4导航栏,它可以保留导航栏品牌而不会在折叠时消失。
这是我的代码:
<nav class="navbar navbar-light bg-faded">
<button class="navbar-toggler hidden-sm-up" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar2" aria-controls="exCollapsingNavbar2" aria-expanded="false" aria-label="Toggle navigation">
☰
</button>
<div class="collapse navbar-toggleable-xs" id="exCollapsingNavbar2">
<a class="navbar-brand" href="#">Responsive navbar</a>
<ul class="nav navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
</ul>
</div>
</nav>
所以,为了清楚起见,当导航栏折叠时,导航项和导航栏品牌消失了,我希望导航栏品牌仍然可见。
我能想到的唯一的CSS是使用显示,但这没有任何效果。
感谢您的任何建议!
答案 0 :(得分:3)
您应该更改HTML标记,并确保.nav-brand
超出<div class="collapse navbar-toggleable-xs" id="exCollapsingNavbar2">
:
<nav class="navbar navbar-light bg-faded">
<button class="navbar-toggler hidden-sm-up" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar2" aria-controls="exCollapsingNavbar2" aria-expanded="false" aria-label="Toggle navigation">
☰
</button>
<a class="navbar-brand" href="#">Responsive navbar</a>
<div class="collapse navbar-toggleable-xs" id="exCollapsingNavbar2">
....
您可能还想将pull-xs-right
课程添加到&#34;汉堡包&#34;按钮:
<button class="navbar-toggler hidden-sm-up pull-xs-right" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar2" aria-controls="exCollapsingNavbar2" aria-expanded="false" aria-label="Toggle navigation">
然后它应该如下所示:
当您点击该按钮时,第一个链接仍然会浮动在.nav-brand
:
使用以下SCSS代码从float-left
中移除.nav-brand
以获取额外的小视口:
.navbar {
@include media-breakpoint-down(xs) {
.navbar-brand {
float: none;
}
}
}
上面的SCSS代码编译成CSS代码如下:
@media (max-width: 543px) {
.navbar .navbar-brand {
float: none;
}
}
有关Bootstrap 4响应式导航栏的更多信息,请访问:http://bassjobsen.weblogs.fm/bootstrap-4s-responsive-navbars/