html - 如何在html/css中移动下拉菜单?

标签 html css

如何将下拉菜单移动到特定区域?

HTML 代码:

<div class="dropdown">
    <button onclick="myFunction()" class="dropbtn">Dropdown</button>
    <div id="myDropdown" class="dropdown-content">
        <a href="mirrors.html">Mirror Page</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
    </div>
</div>
    
    
<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 menu 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>

CSS 代码:

/* Dropdown Button */
.dropbtn {
    background-color: #262829;
    color: white;
    padding: 16px;
    font-size: 16px;
    right:80px;
    border: none;
    cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
    position: relative;

    display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* Links inside the dropdown */
.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}

我想做的事情的图片:

想把下拉框移到恢复页面上

最佳答案

你必须添加一个top: x px;用于您的下拉内容,以便将其放在标题栏下方。像这样:

.dropdown-content {
    display: none;
    position: absolute;
    top: 30px;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

关于html - 如何在html/css中移动下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41664101/

相关文章:

html - Rails Mailer 自定义动态内容

html - Css3 响应式 slider 自动化

html - CSS - 文本溢出 : ellipsis fixing

javascript - 如何在 CSS 中设置默认颜色?

html - 菜单在 chrome 上消失,直到你在开发者工具中弄乱它

javascript - 如何更改 img src 链接鼠标悬停?

css - angular2 : how to manually add css files by condition to index. html?

html - 单击另一个 div 时显示无父 div,纯 css 可能吗?

javascript - 如何使用 Javascript 中的 execCommand 将 "id"设置为标签?

javascript - 如何编译HTML?