我是一个初学者,从一个Web应用程序开始,使用Bootstrap 4来设计我的Web。这里有一个简单的问题。
当代码是

<nav class="navbar navbar-expand-sm navbar-dark sticky-top">
  <div class="container" style="padding:0;">
    <a class="navbar-brand text-info">The Bros</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar"><span class="navbar-toggler-icon"></span></button>
    <div class="collapse navbar-collapse" id="collapsibleNavbar">
      <ul class="nav nav-justified">
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

但当我只改变第一和第二的顺序时,它就起作用了,如下所示:
(其余与前相同)
<div class="container" style="padding:0;">
<nav class="navbar navbar-expand-sm navbar-dark sticky-top">

由于我想使导航条的背景变为彩色,所以我想使用第二种方法,但导航条不粘在顶部。
有办法解决吗?

最佳答案

我想把导航条的背景染成彩色
不管怎样,在这两种情况下你都能做到。
在第一种情况下,您可以这样做(单击下面的“运行代码段”按钮并展开到整页):

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-sm navbar-dark bg-dark sticky-top">
    <div class="container p-0">
        <a class="navbar-brand text-info">The Bros</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
                <span class="navbar-toggler-icon"></span>
            </button>
        <div class="collapse navbar-collapse" id="collapsibleNavbar">
            <ul class="nav nav-justified">
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<main class="container mt-4">
    <div class="row">
        <div class="col">
            <p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sit voluptates consequatur nostrum beatae doloribus cupiditate incidunt voluptate aliquid quae, alias omnis, quisquam illum! Molestias, nisi ullam odit consectetur nesciunt laborum.
            </p>
            <p style="margin-top: 999px;">
                Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sit voluptates consequatur nostrum beatae doloribus cupiditate incidunt voluptate aliquid quae, alias omnis, quisquam illum! Molestias, nisi ullam odit consectetur nesciunt laborum.
            </p>
        </div>
    </div>
</main>

第二种情况是:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="container p-0 sticky-top">
    <nav class="navbar navbar-expand-sm navbar-dark bg-dark">
        <a class="navbar-brand text-info">The Bros</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
               <span class="navbar-toggler-icon"></span>
           </button>
        <div class="collapse navbar-collapse" id="collapsibleNavbar">
            <ul class="nav nav-justified">
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
            </ul>
        </div>
    </nav>
</div>

<main class="container mt-4">
    <div class="row">
        <div class="col">
            <p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sit voluptates consequatur nostrum beatae doloribus cupiditate incidunt voluptate aliquid quae, alias omnis, quisquam illum! Molestias, nisi ullam odit consectetur nesciunt laborum.
            </p>
            <p style="margin-top: 999px;">
                Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sit voluptates consequatur nostrum beatae doloribus cupiditate incidunt voluptate aliquid quae, alias omnis, quisquam illum! Molestias, nisi ullam odit consectetur nesciunt laborum.
            </p>
        </div>
    </div>
</main>

所以,在第二种情况下,您只需将sticky-top类添加到.container而不是navbar。
还要注意间隔类p-0(padding:0)和mt-4(margin top 4 units)的使用。

关于html - 嵌套在div中时,Bootstrap 4导航栏粘性顶部不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48655884/

10-16 13:41