当试图让导航栏保持最高时,它会堆叠起来

时间:2017-04-13 11:22:13

标签: html css html5 css3

您好,这是我现在的代码:

<!DOCTYPE html>
<html>
<head>
<style>
body {margin:0;}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  font-family: "Verdana","Arial", sans-serif;
  position: fixed;



}

.topnav a:hover {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {display: none;}
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }

}

h1 {
    font-family: "Verdana","Arial", sans-serif;
}

.active {
    background-color: #4CAF50;
}
</style>
</head>
<body>

<div class="topnav" id="myTopnav">
  <a class="active" href="index.html">Home</a>
  <a href="opdracht1.html">Opdracht 1</a>
  <a href="opdracht2.html">Opdracht 2</a>
  <a href="opdracht3.html">Opdracht 3</a>
  <a href="opdracht4.html">Opdracht 4</a>
  <a href="downloads.html">Downloads</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" 
onclick="myFunction()">&#9776;</a>
</div>

<div style="padding:20px;background-color:#b2d1d1;height:100%;">
<center><h1 class="h1">Persephone</h1>

<img src="Persephone.jpg" height="540"width="540">



</div>

 <div class="footer">
        Website made by Arne Wessel G2g
    </div>

<script>
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
</script>

</body>
</html>

我的网站已经响应但我还希望导航栏保持在顶部,页脚在滚动时保持在底部。所以如果有人能帮助我那就太好了!

1 个答案:

答案 0 :(得分:1)

您需要使用位置:固定在 .topnav 而非链接上,并对 .footer 执行相同的操作并添加bottom:0; < / p>

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
body {
  margin: 0;
}

.topnav {
  overflow: hidden;
  background-color: #333;
  position: fixed;
  width:100%;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  font-family: "Verdana", "Arial", sans-serif;
  
}

.topnav a:hover {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}

h1 {
  font-family: "Verdana", "Arial", sans-serif;
}

.active {
  background-color: #4CAF50;
}
.footer{
    position: fixed;
    bottom:0;
}
<div class="topnav" id="myTopnav">
  <a class="active" href="index.html">Home</a>
  <a href="opdracht1.html">Opdracht 1</a>
  <a href="opdracht2.html">Opdracht 2</a>
  <a href="opdracht3.html">Opdracht 3</a>
  <a href="opdracht4.html">Opdracht 4</a>
  <a href="downloads.html">Downloads</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>

<div style="padding:20px;background-color:#b2d1d1;height:100%;">
  <center>
    <h1 class="h1">Persephone</h1>

    <img src="Persephone.jpg" height="540" width="540">



</div>

<div class="footer">
  Website made by Arne Wessel G2g
</div>