为什么导航栏折叠按钮始终显示在我的网页上?

时间:2019-02-17 14:34:20

标签: html css bootstrap-4

<nav class="navbar navbar-expand-lg navbar-expand-md sticky-top">
    <div class="container-fluid">
        <a class="navbar-brand" href="index.html">Website </a>
        <button class="navbar-toggle collapsed " data-toggle="collapse" data-target="#navbarNavDropdown" aria-expanded="false">
                <i class="fa fa-bars"></i>
        </button>
        <div class="collapse navbar-collapse justify-content-end" id="navbarNavDropdown">
            <ul class="nav navbar-nav">
                <li class="nav-item active">
                    <a class="nav-link" href="index.html"> Home </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="link1.html"> Link1</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="link2.html"> Link2</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

本应仅在将窗口调整为小尺寸时显示的按钮始终显示在网站名称的右侧。全屏显示,即在计算机模式下查看时,它看起来很难看。我想将其从高分辨率设备中删除。告诉我我在哪里犯错。

2 个答案:

答案 0 :(得分:0)

因为Bootstrap 4导航栏的正确类名是 navbar-toggler ,而不是navbar-toggle

<nav class="navbar navbar-light navbar-expand-lg navbar-expand-md sticky-top">
    <div class="container-fluid">
        <a class="navbar-brand" href="index.html">Website </a>
        <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarNavDropdown" aria-expanded="false">
                <i class="fa fa-bars"></i>
        </button>
        <div class="collapse navbar-collapse justify-content-end" id="navbarNavDropdown">
            <ul class="nav navbar-nav">
                <li class="nav-item active">
                    <a class="nav-link" href="index.html"> Home </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="link1.html"> Link1</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="link2.html"> Link2</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

https://www.codeply.com/go/9uKT2H0EgP

答案 1 :(得分:-1)

仅当您希望图标仅在狭窄的视口中显示时,才使用类navbar-expand-sm。找到一个overview in the documentation。另外,您为汉堡包图标使用了错误的类:class="navbar-toggler",例如 吉姆已经说了。

<!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.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  <title>Hello, world!</title>
</head>

<body>

  <nav class="navbar navbar-expand-sm navbar-dark bg-dark">
    <a class="navbar-brand" href="#">Expand at sm</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample03" aria-controls="navbarsExample03" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

    <div class="collapse navbar-collapse" id="navbarsExample03">
      <ul class="navbar-nav mr-auto">
        <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="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="https://example.com" id="dropdown03" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
          <div class="dropdown-menu" aria-labelledby="dropdown03">
            <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>
      </ul>
      <form class="form-inline my-2 my-md-0">
        <input class="form-control" type="text" placeholder="Search">
      </form>
    </div>
  </nav>

  <!-- 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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>