bootstrap 4 flex粘性页脚和固定顶部导航栏

时间:2019-03-07 22:22:36

标签: css twitter-bootstrap flexbox sticky-footer

我正在使用Boostrap的粘性页脚导航栏模板作为起点,并尝试使灰色div的固定高度为120px,蓝色div占用剩余的高度。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   <link rel="icon" href="../../../../favicon.ico">
  <title>Sticky Footer Navbar Template for Bootstrap</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
 <link href="sticky-footer-navbar.css" rel="stylesheet">
</head>
<body>
<header>
  <!-- Fixed navbar -->
  <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
    <a class="navbar-brand" href="#">Fixed navbar</a>
     <div class="collapse navbar-collapse" id="navbarCollapse">
      <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>
           </ul>
           </div>
  </nav>
</header>
<!-- Begin page content -->
<div  class="container-fluid">
<div class="row bg-primary d-flex flex-1 flex-column"> 
  ermentum, tortor tellus ultricies erat, et ultricies magna nisi at 
</div>
<div class="row bg-secondary flex-0" style="height:120px;">
</div>
</div>
<footer class="footer">
  <div class="container">
    <span class="text-muted">Place sticky footer content here.</span>
  </div>
</footer>
 <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>
 </body>
 </html>

和CSS文件

html {
  position: relative;
 min-height: 100%;
}
 body {
    /* Margin bottom by footer height */
    margin-bottom: 40px;
 }
 .footer {
   position: absolute;
   bottom: 0;
   width: 100%;
   /* Set the fixed height of the footer here */
   height: 40px;
   line-height: 40px; /* Vertically center the text there */
   background-color: #f5f5f5;
  }

  body > .container-fluid {
      padding: 50px 15px 0;
   }

   .footer > .container {
       padding-right: 15px;
       padding-left: 15px;
    }

我还尝试采用我发现的代码形式codepen并将其粘贴到模板中,然后对CSS进行修改,但是我从未使它起作用。

1 个答案:

答案 0 :(得分:0)

[1]是上面发布的代码在浏览器中的外观

当蓝色div的高度小于屏幕高度时,我一直希望得到下图所示的图像-40px的粘性页脚和120px的灰色div。
[2]

当蓝色屏幕的内容大于屏幕高度时,请根据需要显示滚动条,并使粘性页脚用作普通页脚,并显示在120px的灰色div以下。