Bootstrap 4导航栏折叠不堆叠导航项目

时间:2018-07-19 15:32:20

标签: html responsive-design bootstrap-4 navbar

我有一个奇怪的地方,当我的Bootstrap4导航栏折叠在狭窄模式下展开菜单时,它没有堆叠导航项目。我认为这与全宽度导航项目相关的导航输入配置有关。为了使搜索输入向左堆叠而与nav-links向右堆叠,我将它们放置在两个单独的ul中,并在父div之间应用了justify-content-between的flex-row。这给了我想要的全角外观,但完全破坏了折叠菜单中的堆叠。

以下是chrome中折叠和展开结果的图像。

Full width nav

Expanded collapsed nav

这是完整的代码,因为只有我玩Bootstrap4

nav{
	-webkit-box-shadow: 0px 6px 15px 0px rgba(0,0,0,0.5);
	-moz-box-shadow: 0px 6px 15px 0px rgba(0,0,0,0.5);
	box-shadow: 0px 6px 15px 0px rgba(0,0,0,0.5);
}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
    
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.1/css/all.css" integrity="sha384-O8whS3fhG2OnA5Kas0Y9l3cfpmYjapjI0E4theH4iuMD+pLhbf6JI0jIMfYcK3yZ" crossorigin="anonymous">
    <link rel="stylesheet" href="../C4 Boiler/css/nav.css">
    <title>Boiler Plate</title>
  </head>
  <body>
    <nav class="navbar sticky-top navbar-expand-md navbar-dark bg-dark">
      <a class="navbar-brand" href="#">
        <img src="https://picsum.photos/50" width="50" height="50" alt="Test">
      </a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse justify-content-between" id="navbarNavDropdown">
        <ul class="navbar-nav flex-row">
          <form class="form-inline mr-auto">
            <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">
              <i class="fas fa-search fa-lg"></i>
            </button>
          </form>
        </ul>
        <ul class="navbar-nav flex-row-reverse">
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Profile
            </a>
            <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownMenuLink">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
              <a class="dropdown-item" href="#">Something else here</a>
            </div>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Info
            </a>
            <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownMenuLink">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
              <a class="dropdown-item" href="#">Something else here</a>
            </div>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Features</a>
          </li>          
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
        </ul>
      </div>
      
    </nav>
    <div class="title-box container">
      <div>
        
      </div>
    </div>
    <div class="project-box container">
    </div>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <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.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
  </body>
</html>

如果有人有任何建议,我将不胜感激。

谢谢 马特

1 个答案:

答案 0 :(得分:0)

您的示例几乎可以正常工作。

问题是您在.flex-row上添加的.flex-row-reverse.navbar-nav类。我看不到这两个类没有增加任何价值,因此我删除了它们。我对导航栏菜单项进行了重新排序,以便它们以相反的顺序排列,即从“首页”,“功能”,“ 2”下拉列表中。

我也将input-group与插件一起用于搜索输入,因为您的搜索按钮会溢出并转到小屏幕的下一行。为了使搜索输入宽度为100%,我在其上使用了.w-100类。

小提琴: http://jsfiddle.net/aq9Laaew/101531/

enter image description here

enter image description here