如何修复dropmenue内容的位置

时间:2017-05-02 09:37:02

标签: html css

我正在创建一个带下拉列表的菜单栏... 但下拉内容从导航栏中下降......

请帮帮我。

enter image description here

                        body {margin:0;}

.navbar {
  overflow: hidden;
  background-color: gold;
  position: fixed;
  top: 0;
  width: 100%;
}

.navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;

  text-decoration: none;
  font-size: 17px;
}

.main {

  margin-top: 65px;
}
.footer {
  overflow: hidden;
  background-color: gold;
  position: fixed;
  bottom: 0;
  width: 100%;
}

.footer a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}
</style>
<style>
.dropbtn {
    background-color: red;
    color: gold;
    padding: 25px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
    background-color: gold;
    color: red;
}

.dropdown {
    float: right;
    position: relative;
    display: block;

}

.dropdown-content {
    display: none;
    float: right;
    margin-top: 65px;
    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    right: 0;
    z-index: 1;
}

.dropdown-content li {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}
a{
    text-decoration: none;
    color: black;
}
.dropdown a:hover {background-color: blue}

.show {display:inline-block;}
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>

<div class="navbar">
  <a href="#home"><img src="typ.png" width="62px"></a>
  <button onclick="myFunction()" class="dropbtn dropdown fa fa-bars fa-lg"></button> 
</div>
<ul id="myDropdown" class="dropdown-content">
    <li><a href="#home">Home</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#contact">Contact</a></li>
  </ul>
<script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>
<div class="footer">
  <a><img src="typ.png" width="25px"></a>

</div>
<div class="main">
<style>

.mySlides {display:none}

/* Slideshow container */
.slideshow-container {
  width: 100%;
  position: absolute;
  margin: auto;
  z-index: -1;
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;

  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  height: 13px;
  width: 13px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active {
  background-color: #717171;
}

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .text {font-size: 11px}
}
</style>

<div class="slideshow-container">

<div class="mySlides fade">
  <div class="numbertext">Caption Text</div>
  <img src="1.png" style="width:100%">
</div>

<div class="mySlides fade">
  <div class="numbertext">Caption Two</div>
  <img src="2.png" style="width:100%">
</div>

<div class="mySlides fade">
  <div class="numbertext">Caption Three</div>
  <img src="1.png" style="width:100%">
</div>



<div class="text" style="text-align:center">
  <span class="dot"></span> 
  <span class="dot"></span> 
  <span class="dot"></span> 
</div>
</div>
<script>
var slideIndex = 0;
showSlides();

function showSlides() {
    var i;
    var slides = document.getElementsByClassName("mySlides");
    var dots = document.getElementsByClassName("dot");
    for (i = 0; i < slides.length; i++) {
       slides[i].style.display = "none";  
    }
    slideIndex++;
    if (slideIndex> slides.length) {slideIndex = 1}    
    for (i = 0; i < dots.length; i++) {
        dots[i].className = dots[i].className.replace(" active", "");
    }
    slides[slideIndex-1].style.display = "block";  
    dots[slideIndex-1].className += " active";
    setTimeout(showSlides, 2000); // Change image every 2 seconds
}
</script>

  </div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

设置为0px

的边距.dropdown-content

   <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    body {margin:0;}
    
    .navbar {
      overflow: hidden;
      background-color: gold;
      position: fixed;
      top: 0;
      width: 100%;
    }
    
    .navbar a {
      float: left;
      display: block;
      color: #f2f2f2;
      text-align: center;
      
      text-decoration: none;
      font-size: 17px;
    }
    
    .main {
    
      margin-top: 65px;
    }
    .footer {
      overflow: hidden;
      background-color: gold;
      position: fixed;
      bottom: 0;
      width: 100%;
    }
    
    .footer a {
      float: left;
      display: block;
      color: #f2f2f2;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
      font-size: 17px;
    }
    </style>
    <style>
    .dropbtn {
        background-color: red;
        color: gold;
        padding: 25px;
        font-size: 16px;
        border: none;
        cursor: pointer;
    }
    .dropbtn:hover, .dropbtn:focus {
        background-color: gold;
        color: red;
    }
    
    .dropdown {
        float: right;
        position: relative;
        display: block;
        
    }
    
    .dropdown-content {
        display: none;
    	float: right;
        margin-top: 0px;
        background-color: #f9f9f9;
        min-width: 160px;
        overflow: auto;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        right: 0;
        z-index: 1;
    }
    
    .dropdown-content li {
        color: black;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
    }
    a{
    	text-decoration: none;
    	color: black;
    }
    .dropdown a:hover {background-color: blue}
    
    .show {display:inline-block;}
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    </head>
    <body>
    
    <div class="navbar">
      <a href="#home"><img src="typ.png" width="62px"></a>
      <button onclick="myFunction()" class="dropbtn dropdown fa fa-bars fa-lg"></button> 
    </div>
    <ul id="myDropdown" class="dropdown-content">
        <li><a href="#home">Home</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#contact">Contact</a></li>
      </ul>
    <script>
    /* When the user clicks on the button, 
    toggle between hiding and showing the dropdown content */
    function myFunction() {
        document.getElementById("myDropdown").classList.toggle("show");
    }
    
    // Close the dropdown if the user clicks outside of it
    window.onclick = function(event) {
      if (!event.target.matches('.dropbtn')) {
    
        var dropdowns = document.getElementsByClassName("dropdown-content");
        var i;
        for (i = 0; i < dropdowns.length; i++) {
          var openDropdown = dropdowns[i];
          if (openDropdown.classList.contains('show')) {
            openDropdown.classList.remove('show');
          }
        }
      }
    }
    </script>
    <div class="footer">
      <a><img src="typ.png" width="25px"></a>
      
    </div>
    <div class="main">
    <style>
    
    .mySlides {display:none}
    
    /* Slideshow container */
    .slideshow-container {
      width: 100%;
      position: absolute;
      margin: auto;
      z-index: -1;
    }
    
    /* Caption text */
    .text {
      color: #f2f2f2;
      font-size: 15px;
      
      position: absolute;
      bottom: 8px;
      width: 100%;
      text-align: center;
    }
    
    /* Number text (1/3 etc) */
    .numbertext {
      color: #f2f2f2;
      font-size: 12px;
      padding: 8px 12px;
      position: absolute;
      top: 0;
    }
    
    /* The dots/bullets/indicators */
    .dot {
      height: 13px;
      width: 13px;
      margin: 0 2px;
      background-color: #bbb;
      border-radius: 50%;
      display: inline-block;
      transition: background-color 0.6s ease;
    }
    
    .active {
      background-color: #717171;
    }
    
    /* Fading animation */
    .fade {
      -webkit-animation-name: fade;
      -webkit-animation-duration: 1.5s;
      animation-name: fade;
      animation-duration: 1.5s;
    }
    
    @-webkit-keyframes fade {
      from {opacity: .4} 
      to {opacity: 1}
    }
    
    @keyframes fade {
      from {opacity: .4} 
      to {opacity: 1}
    }
    
    /* On smaller screens, decrease text size */
    @media only screen and (max-width: 300px) {
      .text {font-size: 11px}
    }
    </style>
    
    <div class="slideshow-container">
    
    <div class="mySlides fade">
      <div class="numbertext">Caption Text</div>
      <img src="1.png" style="width:100%">
    </div>
    
    <div class="mySlides fade">
      <div class="numbertext">Caption Two</div>
      <img src="2.png" style="width:100%">
    </div>
    
    <div class="mySlides fade">
      <div class="numbertext">Caption Three</div>
      <img src="1.png" style="width:100%">
    </div>
    
    
    
    <div class="text" style="text-align:center">
      <span class="dot"></span> 
      <span class="dot"></span> 
      <span class="dot"></span> 
    </div>
    </div>
    <script>
    var slideIndex = 0;
    showSlides();
    
    function showSlides() {
        var i;
        var slides = document.getElementsByClassName("mySlides");
        var dots = document.getElementsByClassName("dot");
        for (i = 0; i < slides.length; i++) {
           slides[i].style.display = "none";  
        }
        slideIndex++;
        if (slideIndex> slides.length) {slideIndex = 1}    
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active", "");
        }
        slides[slideIndex-1].style.display = "block";  
        dots[slideIndex-1].className += " active";
        setTimeout(showSlides, 2000); // Change image every 2 seconds
    }
    </script>
    
      </div>
    </body>
    </html>

答案 1 :(得分:0)

类.dropdown-content的margin-top为65px,你可以设置为0。 像这样:

.dropdown-content{margin-top: 0;}