在页面顶部有两个导航,一个固定顶部但不与另一个重叠

时间:2018-01-16 23:51:52

标签: bootstrap-4

我正在努力实现结果illustrated here。 促销横幅需要粘贴到页面顶部,而其下方的导航将向上滚动其余内容。

以下是我用来执行此操作的一些基本HTML。我还设置了body {padding-top:40px;},因此带有实际菜单的导航显示在促销横幅下方。

<nav class="navbar navbar-expand-md fixed-top justify-content-center" style="background: rgba(0,0,0,0.7); color: white;">Promotional banner text goes here promotional banner text goes here promotional banner text goes here</nav>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
  <a class="navbar-brand" href="index.php">Top navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarsExampleDefault">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <a class="nav-link" href="testimonials.php">Home</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="contact1.php">Link</a>
      </li>
    </ul>
    <span style="color: #FFFFFF;">(555) 555 5555</span>
  </div>
</nav>

如果视口宽度足以适合促销横幅的文本,那就太棒了,但如果文本突破到第二行,它会重叠菜单。在这种情况下,我理想情况下菜单只是在促销横幅下面进一步移动,但是目前我已经使用CSS body {padding-top:40px;}

解决这个问题的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

好的,我将促销横幅放入container并嵌套在container-fluid内。通常,容器不应该嵌套,但这是极少数例外情况之一。

您的导航栏也位于container内。这是为了确保宽4K显示器上的用户不会生你的气。我还用一些本地Bootstrap 4类替换了你的一些自定义css,而且几乎就是这样。

以下是代码:

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

<div class="container-fluid sticky-top" style="background: rgba(0,0,0,0.7);">
    <div class="container">
        <div class="row">
            <div class="col text-center">
                <div class="py-2 navbar-text text-white">Promotional banner text goes here promotional banner text goes here promotional banner text goes here. Promotional banner text goes here promotional banner text goes here promotional banner text goes here.</div>
            </div>
        </div>
    </div>
</div>

<nav class="navbar navbar-expand-md navbar-dark bg-dark">
   <div class="container">
       <a class="navbar-brand" href="index.php">Top navbar</a>
       <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
           <span class="navbar-toggler-icon"></span>
       </button>
       <div class="collapse navbar-collapse" id="navbarsExampleDefault">
           <ul class="navbar-nav mr-auto">
               <li class="nav-item">
                   <a class="nav-link" href="testimonials.php">Home</a>
               </li>
               <li class="nav-item">
                   <a class="nav-link" href="contact1.php">Link</a>
               </li>
           </ul>
           <span class="navbar-text text-white">(555) 555 5555</span>
       </div>
   </div>
</nav>

<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>
&#13;
&#13;
&#13;