折叠的Navbar仅显示在小屏幕大小的引导程序

时间:2017-07-17 00:08:57

标签: html css twitter-bootstrap

我正在尝试在小型设备上实现折叠的导航栏。这工作正常。

但是,在中型和大型屏幕上,导航栏根本不显示。我希望导航栏看起来像中型和大型屏幕的普通导航栏,然后折叠到小型设备的导航栏按钮。我正在关注引导程序模板,并且不知道为什么导航栏根本没有出现。

<body>
<div class = "container">

<nav class="navbar navbar-default" >


        <div class="navbar-header">
            <a class="navbar-brand" href="#">
                <img alt="logo" src="logo.png" width="250px;">
             </a>


        <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#collapse-navbar" aria-expanded="false" aria-controls="navbar">
            &#9776;
        </button>
        </div>


  <div class="navbar-collapse collapse" id="collapse-navbar">    
     <ul class="nav navbar-nav">
        <li class= "nav-item"><a class= "nav-link active" href="#">Home</a></li>
        <li class= "nav-item"><a class= "nav-link" href="#">Build</a></li>
        <li class= "nav-item"><a class= "nav-link" href="#">About</a></li>
    </ul>



    </div>

</nav> 
</div>

以下是代码笔链接:https://codepen.io/gkunthara/pen/VWdrYj

2 个答案:

答案 0 :(得分:1)

您正在使用bootstrap 3标记并链接到bootstrap 4 css链接到bootstrap 3&#39; css和javascript,如下所示:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

或者将导航栏标记更改为bootstrap 4的标记,如下所示:

<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">
    <img alt="logo" src="logo.png" width="250px;">
  </a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="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="#">Build</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">About</a>
      </li>
    </ul>
  </div>
</nav>

答案 1 :(得分:0)

您需要在中型和大型屏幕上显示.navbar-header button。所以改变

@media (min-width: 768px)
{
    .navbar-toggle {
        display: none;
    }
}

这个

@media (min-width: 768px)
{
    .navbar-toggle {
        display: none;
    }

    .navbar-header button {
        display: block;
    }
}

https://codepen.io/julysfx/pen/jwRYQB