Bootstrap 4折叠导航栏丢失了navbar-brand

时间:2016-08-04 16:15:38

标签: html5 navbar collapse bootstrap-4

我正试图设计一个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">
    &#9776;
  </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是使用显示,但这没有任何效果。

感谢您的任何建议!

1 个答案:

答案 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">
    &#9776;
  </button>
  <a class="navbar-brand" href="#">Responsive navbar</a>
  <div class="collapse navbar-toggleable-xs" id="exCollapsingNavbar2">
  ....

现在,您的导航栏应如下图所示在小视口中显示: navbar on small viewports

您可能还想将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">

然后它应该如下所示:

enter image description here

当您点击该按钮时,第一个链接仍然会浮动在.nav-brand

旁边

enter image description here

使用以下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/