Bootstrap 4汉堡包未显示项目,但在开发工具中有所变化

问题描述

我对bootstrap 4汉堡包菜单有一些麻烦-在开发工具的检查器选项卡上,它似乎正在运行(元素正在更改),但在视图上没有任何作用。

index.html

<!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.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZonixN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <nav class="navbar navbar-expand-fluid navbar-dark bg-dark">
      <a class="nav-text" href="#">Test</a>
      
      <button class="navbar-toggler" data-toggle="collapse" data-target="#collapseTarget">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="collapseTarget">
        <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link" href="#">Link1</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link2</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#" >Link3</a>
          </li>
        </ul>
      </div>

    </nav>
    <div class="card">
      <div class="card-body">
        <h1 class="card-title" style="font-weight: 300; font-size: 72px; line-height: 84px;">Another Text</h1>
        <p class="card-text" style="width: 635px">text</a>
      </div>
    </div>

    <!-- jQuery first,then Popper.js,then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
  </body>
</html>

style.css

nav {
  height: 56px;
}

.navbar-dark .navbar-nav .nav-link {
  color: white;
}

.nav-text {
  font-family: Roboto;
  font-style: normal;
  font-weight: normal;
  font-size: 20px; 
  line-height: 23px;
  color: #FFFFFF;
  padding-top: 2px;
  padding-right: 18px;
}

.nav-link {
  padding-top: 19px;
  padding-bottom: 18px;
  font-family: Roboto;
  font-style: normal;
  font-weight: normal;
  font-size: 16px;
  line-height: 19px;
}

.card-body {
  padding-top: 64px;
  padding-left: 49px;
  padding-bottom: 76px;
  background: #E9ECEF;
}

.card-text {
  font-family: Roboto;
  font-style: normal;
  font-weight: normal;
  font-size: 16px;
  line-height: 19px;
}

我使用了其他一些示例,但是它们也不起作用。我的浏览器有问题吗?我已经在Chrome和Firefox上对其进行了测试。

解决方法

查看此代码段:

.navbar-dark .navbar-nav .nav-link {
    color: white;
}

.nav-text {
    font-family: Roboto;
    font-style: normal;
    font-weight: normal;
    font-size: 20px; 
    line-height: 23px;
    color: #FFFFFF;
    padding-top: 2px;
    padding-right: 18px;
}

.nav-link {
    padding-top: 19px;
    padding-bottom: 18px;
    font-family: Roboto;
    font-style: normal;
    font-weight: normal;
    font-size: 16px;
    line-height: 19px;
}

.card-body {
    padding-top: 64px;
    padding-left: 49px;
    padding-bottom: 76px;
    background: #E9ECEF;
}

.card-text {
    font-family: Roboto;
    font-style: normal;
    font-weight: normal;
    font-size: 16px;
    line-height: 19px;
}
<!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.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <nav class="navbar navbar-expand-fluid navbar-dark bg-dark">
        <a class="nav-text" href="#">Test</a>

        <button class="navbar-toggler" data-toggle="collapse" data-target="#collapseTarget">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="collapseTarget">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" href="#">Link1</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Link2</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#" >Link3</a>
                </li>
            </ul>
        </div>

    </nav>
    <div class="card">
        <div class="card-body">
            <h1 class="card-title" style="font-weight: 300; font-size: 72px; line-height: 84px;">Another Text</h1>
            <p class="card-text" style="width: 635px">text</a>
            </div>
        </div>

        <!-- jQuery first,then Popper.js,then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    </body>
    </html>

您需要将其从样式中删除:

nav {
    height: 56px;
}

此片段使您的<nav>固定高度,并推翻了崩溃效果类完成的高度调整

,

您的问题是这种样式:

nav {
  height: 56px;
}

另一种可能性是使用min-height更改高度:

nav {
   min-height: 56px;
}

nav的高度直接由boostrap处理。因此,如果您从CSS中删除该部分,它将起作用:

nav {
    min-height: 56px;
}
.navbar-dark .navbar-nav .nav-link {
    color: white;
}
.nav-text {
    font-family: Roboto;
    font-style: normal;
    font-weight: normal;
    font-size: 20px;
    line-height: 23px;
    color: #FFFFFF;
    padding-top: 2px;
    padding-right: 18px;
}
.nav-link {
    padding-top: 19px;
    padding-bottom: 18px;
    font-family: Roboto;
    font-style: normal;
    font-weight: normal;
    font-size: 16px;
    line-height: 19px;
}
.card-body {
    padding-top: 64px;
    padding-left: 49px;
    padding-bottom: 76px;
    background: #E9ECEF;
}
.card-text {
    font-family: Roboto;
    font-style: normal;
    font-weight: normal;
    font-size: 16px;
    line-height: 19px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

<nav class="navbar navbar-expand-fluid navbar-dark bg-dark">
    <a class="nav-text" href="#">Test</a>

    <button class="navbar-toggler" data-toggle="collapse" data-target="#collapseTarget">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="collapseTarget">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href="#">Link1</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link2</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#" >Link3</a>
            </li>
        </ul>
    </div>

</nav>
<div class="card">
    <div class="card-body">
        <h1 class="card-title" style="font-weight: 300; font-size: 72px; line-height: 84px;">Another Text</h1>
        <p class="card-text" style="width: 635px">text</a>
    </div>
</div>