简体   繁体   中英

bootstrap 4 flex sticky footer and fixed top navbar

I am using the sticky footer navbar template from Boostrap as a starting point and trying to have the gray div a fixed height of 120px, and the blue div taking up the remaining height.

<!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>

and the css file

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;
    }

I also tried taking the code form codepen I found and pasting it in the template and tweeking the CSS but I never got it to work.

[ 1] is what the code posted above looks like in a browser

and I was looking to end up with something that looks like the image below when the height of the blue div is less then the height of the screen - the 40px sticky footer and the 120px grey div.
[ 2]

When the content of the blue screen is greater then the height of the screen, then show the scroll bars as necessary and have sticky footer act as a normal footer and shown below the 120px gray div.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM