CSS边栏与Navbar重叠,无法将div堆叠

时间:2019-02-04 20:33:45

标签: html css bootstrap-4

我正在尝试使侧边栏堆叠在导航栏下方,但是我无法使侧边栏停止与导航栏的上部重叠。由于某种原因,页脚很好。我希望边栏嵌套在导航栏下方并坐在网页的左侧。我使用的配置是标题中链接的bootstrap 4。

<!DOCTYPE html>
<html>
   <head>
      <title><%= full_title(yield(:title)) %></title>
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
      <style>
        .navbar{
        overflow: hidden;
        background-color: #d0f0c0;
        
        }
        #navbar a {
        float: right;
        display: block;
        color: #708090;
        text-align: center;
        padding: 14px;
        text-decoration: none;
        }
        #lower{
            display: block;
            margin: auto;
            position: relative;
        }
        .sidenav {
        height: 100%;
        width: 160px;
        position: fixed;
        z-index: 1;
        top: 0;
        left: 0;
        background-color: #f0e0c0;
        overflow-x: hidden;
        padding-top: 15%;
        display: block;
        margin: auto;
        }
        .sidenav a {
        padding: 6px 8px 6px 16px;
        text-decoration: none;
        font-size: 25px;
        color: #818181;
        display: block;
        }
        .sidenav a:hover {
        color: #f1f1f1;
        }
        .main {
        margin-left: 160px;
        padding: 0px 10px;
        }
        .container:before,
        .container:after {
            content: "";
            display: table;
        }

        .container:after {
            clear: both;
        }
        @media screen and (max-height: 450px) {
        .sidenav {padding-top: 15px;}
        .sidenav a {font-size: 18px;}
        }
      </style>
   </head>
   
   <body>
    <div class="container-flex">
      <header class="navbar navbar-fixed-top navbar-inverse">
         <div class="container">
            <div id="navbar">
               <div class="navbar-brand" style="cursor: pointer; float: right;"> 

               </div>
            </div>
         </div>
      </header>
    </div>     
      <div class="container-flex" style="height: 85%; padding: 0;">
         <%= yield %>
      </div>
   </body>
   <div class="container-flex">
        <div class="sidenav">
            <a href="#">About</a>
            <a href="#">Services</a>
            <a href="#">Clients</a>
            <a href="#">Contact</a>
        </div>
      </div> 
   <div class="navbar fixed-bottom" style="height: 10%;">
   </div>
</html>

1 个答案:

答案 0 :(得分:1)

您将.sidenav设置为top:0;表示它将在0px位置停留在顶部。我删除了它,这是结果:

<!DOCTYPE html>
<html>
   <head>
      <title><%= full_title(yield(:title)) %></title>
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
      <style>
        .navbar{
         width:100%!important;
        overflow: hidden;
        background-color: #d0f0c0;
        
        
        }
        #navbar a {
        width:100%!important;
        float: right;
        display: block;
        color: #708090;
        text-align: center;
        padding: 14px;
        text-decoration: none;
        }
        #lower{
            display: block;
            margin: auto;
            position: relative;
        }
        .sidenav {
        height: 100%;
        width: 160px;
        position: fixed;
        z-index: 1;
        
        left: 0;
        background-color: #f0e0c0;
        overflow-x: hidden;
        padding-top: 15%;
        display: block;
        margin: auto;
        }
        .sidenav a {
        padding: 6px 8px 6px 16px;
        text-decoration: none;
        font-size: 25px;
        color: #818181;
        display: block;
        }
        .sidenav a:hover {
        color: #f1f1f1;
        }
        .main {
        margin-left: 160px;
        padding: 0px 10px;
        }
        .container:before,
        .container:after {
            content: "";
            display: table;
        }

        .container:after {
            clear: both;
        }
        @media screen and (max-height: 450px) {
        .sidenav {padding-top: 15px;}
        .sidenav a {font-size: 18px;}
        }
      </style>
   </head>
   
   <body>
    <div class="container-flex">
      <header class="navbar navbar-fixed-top navbar-inverse">
         <div class="container">
            <div id="navbar">
               <div class="navbar-brand" style="cursor: pointer; float: right;"> 

               </div>
            </div>
         </div>
      </header>
    </div>     
      <div class="container-flex" style="height: 85%; padding: 0;">
         
      </div>
   </body>
   <div class="container-flex">
        <div class="sidenav">
            <a href="#">About</a>
            <a href="#">Services</a>
            <a href="#">Clients</a>
            <a href="#">Contact</a>
        </div>
      </div> 
   <div class="navbar fixed-bottom" style="height: 10%;">
   </div>
</html>