willtcrook
willtcrook

Reputation: 19

How to move drop-down menu in html/css?

How do I move the drop down menu onto a certain area ?

HTML CODE:

<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 CODE:

/* 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;}

IMAGE OF WHAT I WANT DONE:

want to move the dropdown onto the restoration page

Upvotes: 1

Views: 10785

Answers (2)

nico_lrx
nico_lrx

Reputation: 290

You have to add a top: x px; for your dropdown content in order to place it below your header bar. Like this:

.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);
}

Upvotes: 1

Wais Kamal
Wais Kamal

Reputation: 6180

Any button is set to display: block by default. To align it next to the dropdown buttons, use either float: left or display: inline-block.

Upvotes: 0

Related Questions