Home>
How can I make it so that when the menu is expanded, it is inserted not on top of the menu elements, but between them?
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1"><style>.dropbtn {
background-color: #3498DB;
color:white;
padding: 16px
font-size: 16px;
border: none
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display:block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
</style></head><body><h2>Clickable Dropdown</h2>Click on the button to open the dropdown menu.
<div class="dropdown"><ul><li><button onclick="myFunction()" class="dropbtn">Dropdown</button></li> <div id="myDropdown" class="dropdown-content"> <a href="#home">Home</a> <a href="#about">About</a> <a href="#contact">Contact</a> </div> <li><button onclick="myFunction()" class="dropbtn">Dropdown</button></li> <div id="myDropdown" class="dropdown-content"> <a href="#home">Home</a> <a href="#about">About</a> <a href="#contact">Contact</a> </div> </ul></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 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></body></html>
-
Answer # 1
-
Answer # 2
Change the position of .dropdown-content to relative and it will work.
/* 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'); } } } }
.dropbtn { background-color: #3498DB; color:white; padding: 16px font-size: 16px; border: none cursor: pointer; } .dropbtn:hover, .dropbtn:focus { background-color: #2980B9; } .dropdown { position: relative; display: inline-block } .dropdown-content { display: none; position: relative; background-color: #f1f1f1; min-width: 160px overflow: auto; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display:block; } .dropdown a:hover {background-color: #ddd;} .show {display: block;}
<h2>Clickable Dropdown</h2>
p>Click on the button to open the dropdown menu.
Related questions
- javascript : Block scrolling by clicking on the arrows
- javascript : Console gives error "Uncaught TypeError: Cannot read properties of null (reading 'style')" [duplicate]
- javascript : How to change the color of the scale in the video?
- javascript : html css + js how to position two charts (Chart_js) side by side rather than one below the other
- javascript : How to highlight one whole line in long HTML text
- javascript : How to emulate safari browser on windows
- javascript : Console throws an error: Uncaught TypeError: Cannot set properties of null (setting 'src')
- javascript : Why won't the animation start?
- javascript : Wrong handler in JS media requests
Change the position of .dropdown-content to relative and it will work.