Bootstrap 4 - 如何防止折叠菜单折叠导航中的所有元素

Bootstrap 4 - How to prevent collapsing menu from collapsing all elements in the nav

我正在使用 bootstrap 4 导航栏,我想防止徽标和语言切换在单击汉堡菜单时随菜单一起移动和折叠。这是我所有的代码。另外,这是我的 codepen 的 link。这可能吗?

https://codepen.io/asizemore/pen/LYEgvOy?editors=1100

CSS

.logo {
  max-width: 130px;
}

.btn {
  font-family: 'Muli', sans-serif;
  font-weight: 900;
}

.btn {
  background: rgb(142, 63, 41);
  color: rgb(202, 171, 159);
}

.active {
  background: rgb(166, 73, 47);
  color: white;
}

HTML

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>

!--   NAV BAR -->
<nav class="navbar navbar-light fixed-top navbar-expand-lg bg-white">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <a class="navbar-brand mt-lg-0" href="#"><img src="https://via.placeholder.com/200x50" class="logo img-fluid"></a>

  <div class="collapse navbar-collapse" id="navbarTogglerDemo03">
    <ul class="navbar-nav mx-auto mt-2 mt-lg-0">
      <li class="nav-item">
        <a class="nav-link" href="#">PURPOSE</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">FAMILY</a>
      </li>
       <li class="nav-item">
        <a class="nav-link" href="#">AGRICULTURE</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">ART</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">CONTACT</a>
      </li>

    </ul>

  </div>

<!--   LANGUAGE SWITCH -->

<div class="btn-group btn-toggle lang mr-1"> 
  <input type="button" name="lang" class="btn" value="ENG">

  <input type="button" name="lang" class="btn active" value="SPA">
</div>
</nav>

将您的语言开关放在 .navbar-item 中,紧跟在 .navbar-brand 之后,然后给它一个 order: 1(它比 0 的默认值大 - 所以它会在 .navbar 未折叠的时间间隔内显示为其父项的最后一项)。由于您使用的是 .navbar-expand-lg,它将是 @media (min-width: 992px),但如果您使用 .navbar-expand-md,它将是 min-width: 768px - 或 576px for -sm.更多关于 responsive breakpoints here.

当导航折叠时,您不需要改变顺序,所以语言切换器显示在菜单之前(这就是我们首先将它放在徽标之后的原因)。看到它工作:

.logo {
  max-width: 130px;
}

.btn {
  font-family: 'Muli', sans-serif;
  font-weight: 900;
}

.btn {
  background: rgb(142, 63, 41);
  color: rgb(202, 171, 159);
}

.active {
  background: rgb(166, 73, 47);
  color: white;
}

@media(min-width: 992px) {
  .language-switch {
    order: 1;
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>

<nav class="navbar navbar-light fixed-top navbar-expand-lg bg-white">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand mt-lg-0" href="#"><img src="https://via.placeholder.com/200x50" class="logo img-fluid"></a>
  <div class="nav-item language-switch">
    <div class="btn-group btn-toggle lang mr-1">
      <input type="button" name="lang" class="btn" value="ENG">
      <input type="button" name="lang" class="btn active" value="SPA">
    </div>
  </div>
  <div class="collapse navbar-collapse" id="navbarTogglerDemo03">
    <ul class="navbar-nav mx-auto mt-2 mt-lg-0">
      <li class="nav-item">
        <a class="nav-link" href="#">PURPOSE</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">FAMILY</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">AGRICULTURE</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">ART</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">CONTACT</a>
      </li>
    </ul>
  </div>
</nav>

哦,当它们实际上不适合一行(设备窄于 ~450px 时,你需要想出一个解决方案来对齐这三个(徽标、语言切换和切换器) ).但这基本上是你要问的问题。

注意:如果您希望在折叠导航栏时首先显示徽标,您只需将其放在切换器之前,然后为切换器提供 ml-auto mr-3ml-3 mr-auto 使其固定右侧或左侧(恕我直言,居中有点奇怪)。示例:

.logo {
  max-width: 130px;
}

.btn {
  font-family: 'Muli', sans-serif;
  font-weight: 900;
}

.btn {
  background: rgb(142, 63, 41);
  color: rgb(202, 171, 159);
}

.active {
  background: rgb(166, 73, 47);
  color: white;
}

@media(min-width: 992px) {
  .language-switch {
    order: 1;
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>

!-- NAV BAR -->
<nav class="navbar navbar-light fixed-top navbar-expand-lg bg-white">
  <a class="navbar-brand mt-lg-0" href="#"><img src="https://via.placeholder.com/200x50" class="logo img-fluid"></a>
  <button class="navbar-toggler mr-auto" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="nav-item language-switch">
    <div class="btn-group btn-toggle lang mr-1">
      <input type="button" name="lang" class="btn" value="ENG">
      <input type="button" name="lang" class="btn active" value="SPA">
    </div>
  </div>
  <div class="collapse navbar-collapse" id="navbarTogglerDemo03">
    <ul class="navbar-nav mx-auto mt-2 mt-lg-0">
      <li class="nav-item">
        <a class="nav-link" href="#">PURPOSE</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">FAMILY</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">AGRICULTURE</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">ART</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">CONTACT</a>
      </li>
    </ul>
  </div>
</nav>

通过在徽标上使用 order: -1 在适当的响应间隔(即低于 991px在你的情况下)。