简体   繁体   English

如何将下拉菜单与按钮对齐

[英]How to align the dropdown menu with the button

I have a dropdown menu with html, css and some js.我有一个带有 html、css 和一些 js 的下拉菜单。 It works fine, I also added some animations to it.它工作正常,我还添加了一些动画。 Now I would like to align it right under the click me button!现在我想在点击我按钮下对齐它! I have been looking around on the stack and tried with float and align but I can't.我一直在堆栈上环顾四周,并尝试使用浮动和对齐,但我做不到。 Sorry but I'm new to this, can anyone help me ?对不起,但我是新手,有人可以帮助我吗?

I appreciate any response, thanks.我很感激任何回应,谢谢。

 function myFunction() { var x = document.getElementById("Demo"); if (x.className.indexOf("w3-show") == -1) { x.className += " w3-show"; } else { x.className += " w3-hide"; setTimeout(function(){ x.className = x.className.replace(" w3-show", ""); x.className = x.className.replace(" w3-hide", ""); },500) } }
 /*Items menu*/ .user_menu_item { display: flex; align-items: center; } .user_menu_item:hover { background: whitesmoke; border-left: 4px solid blue; transition: 0.3s; } .user_menu_item:not(:hover) { transition: 0.3s; } .user_menu_item > a { display: flex; align-items: center; padding: 12px 10px 12px 10px; width: 100%; font-size: 14px; color: #75777D; } .w3-container { position: absolute; top: 15%; } .w3-dropdown-click { display: flex; position: relative; } .w3-dropdown-content { display: none; background-color: whitesmoke; min-width: 160px; overflow: hidden; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; position:relative; animation:animateFromBottom 0.6s } @keyframes animateFromBottom { from{bottom:-50px;opacity:0} to{bottom:0;opacity:1} } @keyframes animateToBottom { from{bottom:0;opacity:1} to{bottom:-50px;opacity:0} } .w3-bar-block .w3-bar-item { width:100%; display:block; padding:8px 16px; text-align:left; border:none; white-space:normal; float:none; outline:0 } .w3-show-block,.w3-show { display:block!important } .w3-dropdown-content.w3-hide { animation:animateToBottom 0.6s } .w3-button { float: right; }
 <button onclick="myFunction()" class="w3-button">Click Me!</button> <div class="w3-container"> <div class="w3-dropdown-click"> <div id="Demo" class="w3-dropdown-content w3-bar-block w3-border"> <div class="user_menu_item"> <a href="/account"> <span class="link_text">Dashboard</span> </a> </div> <div class="user_menu_item"> <a href="ordini"> <span class="link_text">I miei ordini</span> </a> </div> <div class="user_menu_item"> <a href="libreria"> <span class="link_text">Downloads</span> </a> </div> <div class="user_menu_item"> <a href="impostazioni"> <span class="link_text">Impostazioni</span> </a> </div> <div class="user_menu_item"> <a href="wp-login.php?action=logout"> <span class="link_text">Logout</span> </a> </div> </div> </div> </div>

You can create a container div to put both the menu and the button themselves into and then give that container absolute positioning properties like you're doing with your menu.您可以创建一个容器div以将菜单和按钮本身放入其中,然后像使用菜单一样为该容器提供绝对定位属性。 You'll have to adjust your menu position so when it appears it's not overlapping the button but I took care of that in the code.您必须调整菜单位置,以便当它出现时它不会与按钮重叠,但我在代码中处理了这一点。 Also, you'll need to modify the position on the menu itself if you want the button and the menu to be on the absolute right and not offset and overflowing the right side of the screen.此外,如果您希望按钮和菜单位于绝对右侧并且不偏移和溢出屏幕右侧,则需要修改菜单本身的位置。

See: https://developer.mozilla.org/pt-BR/docs/Web/CSS/position参见: https ://developer.mozilla.org/pt-BR/docs/Web/CSS/position

 function myFunction() { var x = document.getElementById("Demo"); if (x.className.indexOf("w3-show") == -1) { x.className += " w3-show"; } else { x.className += " w3-hide"; setTimeout(function() { x.className = x.className.replace(" w3-show", ""); x.className = x.className.replace(" w3-hide", ""); }, 500) } }
 .container { display: block; margin: 0 0; position: absolute; top: 10px; right: 0px; } /*Items menu*/ .user_menu_item { display: flex; align-items: center; } .user_menu_item:hover { background: whitesmoke; border-left: 4px solid blue; transition: 0.3s; } .user_menu_item:not(:hover) { transition: 0.3s; } .user_menu_item>a { display: flex; align-items: center; padding: 12px 10px 12px 10px; width: 100%; font-size: 14px; color: #75777D; } .w3-container { position: absolute; top: 25px; right: 0px; } .w3-dropdown-click { display: flex; position: relative; } .w3-dropdown-content { display: none; background-color: whitesmoke; min-width: 160px; overflow: hidden; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; position: relative; animation: animateFromBottom 0.6s } @keyframes animateFromBottom { from { bottom: -50px; opacity: 0 } to { bottom: 0; opacity: 1 } } @keyframes animateToBottom { from { bottom: 0; opacity: 1 } to { bottom: -50px; opacity: 0 } } .w3-bar-block .w3-bar-item { width: 100%; display: block; padding: 8px 16px; text-align: left; border: none; white-space: normal; float: none; outline: 0; } .w3-show-block, .w3-show { display: block !important } .w3-dropdown-content.w3-hide { animation: animateToBottom 0.6s } .w3-button { display: block; }
 <div class="container"> <button onclick="myFunction()" class="w3-button">Click Me!</button> <div class="w3-container"> <div class="w3-dropdown-click"> <div id="Demo" class="w3-dropdown-content w3-bar-block w3-border"> <div class="user_menu_item"> <a href="/account"> <span class="link_text">Dashboard</span> </a> </div> <div class="user_menu_item"> <a href="ordini"> <span class="link_text">I miei ordini</span> </a> </div> <div class="user_menu_item"> <a href="libreria"> <span class="link_text">Downloads</span> </a> </div> <div class="user_menu_item"> <a href="impostazioni"> <span class="link_text">Impostazioni</span> </a> </div> <div class="user_menu_item"> <a href="wp-login.php?action=logout"> <span class="link_text">Logout</span> </a> </div> </div> </div> </div> </div>

Add popup and button to the same parent element and set its position relative and then set top value of popup value to static value(height of btn)将弹出和按钮添加到同一个父元素并设置其相对位置,然后将弹出值的顶部值设置为静态值(btn 的高度)

 function myFunction() { var x = document.getElementById("Demo"); if (x.className.indexOf("w3-show") == -1) { x.className += " w3-show"; } else { x.className += " w3-hide"; setTimeout(function() { x.className = x.className.replace(" w3-show", ""); x.className = x.className.replace(" w3-hide", ""); }, 500); } }
 .main_container { position: relative; } /*Items menu*/ .user_menu_item { display: flex; align-items: center; } .user_menu_item:hover { background: whitesmoke; border-left: 4px solid blue; transition: 0.3s; } .user_menu_item:not(:hover) { transition: 0.3s; } .user_menu_item>a { display: flex; align-items: center; padding: 12px 10px 12px 10px; width: 100%; font-size: 14px; color: #75777d; } .w3-container { position: absolute; right: 0; top: 25px; } .w3-dropdown-click { display: flex; position: relative; } .w3-dropdown-content { display: none; background-color: whitesmoke; min-width: 160px; overflow: hidden; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; position: relative; animation: animateFromBottom 0.6s; } @keyframes animateFromBottom { from { bottom: -50px; opacity: 0; } to { bottom: 0; opacity: 1; } } @keyframes animateToBottom { from { bottom: 0; opacity: 1; } to { bottom: -50px; opacity: 0; } } .w3-bar-block .w3-bar-item { width: 100%; display: block; padding: 8px 16px; text-align: left; border: none; white-space: normal; float: none; outline: 0; } .w3-show-block, .w3-show { display: block !important; } .w3-dropdown-content.w3-hide { animation: animateToBottom 0.6s; } .w3-button { float: right; }
 <div class="main_container"> <button onclick="myFunction()" class="w3-button">Click Me!</button> <div class="w3-container"> <div class="w3-dropdown-click"> <div id="Demo" class="w3-dropdown-content w3-bar-block w3-border"> <div class="user_menu_item"> <a href="/account"> <span class="link_text">Dashboard</span> </a> </div> <div class="user_menu_item"> <a href="ordini"> <span class="link_text">I miei ordini</span> </a> </div> <div class="user_menu_item"> <a href="libreria"> <span class="link_text">Downloads</span> </a> </div> <div class="user_menu_item"> <a href="impostazioni"> <span class="link_text">Impostazioni</span> </a> </div> <div class="user_menu_item"> <a href="wp-login.php?action=logout"> <span class="link_text">Logout</span> </a> </div> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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