Hi everyone I am fairly new to web design and I am currently designing a website in which I am attempting to use a dropdown menu made using CSS and Javascript. I have nearly managed to get everything working however the only problem left to solve is that the dropdown menu content appears on the left of the container rather than underneath the relevant parent menu option.
function myFunction1() {
document.getElementById("myDropdown1").classList.toggle("show");
}
function myFunction2() {
document.getElementById("myDropdown2").classList.toggle("show");
}
function myFunction3() {
document.getElementById("myDropdown3").classList.toggle("show");
}
function myFunction4() {
document.getElementById("myDropdown4").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content1");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn2')) {
var dropdowns = document.getElementsByClassName("dropdown-content2");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn3')) {
var dropdowns = document.getElementsByClassName("dropdown-content3");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn4')) {
var dropdowns = document.getElementsByClassName("dropdown-content4");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.navbar {
float: left;
font-family: Impact, Haettenschweiler, Franklin Gothic Bold, Arial Black, " sans-serif";
height: auto;
width: 100%;
display: inline-block;
margin: 0;
padding: 0;
position: relative;
}
.navbar a {
font-size: 2em;
font-weight: 100;
color: white;
text-align: center;
}
.dropdown {
width: 100%;
}
.dropdown-content1 {
display: none;
position: absolute;
background-color: #f9f9f9;
width: 16.667%;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
padding: 0px;
height: auto;
}
.dropdown-content1 a {
text-decoration: none;
display: block;
width: 100%;
padding: 0px;
background-color: #000000;
border: 1px solid white;
text-align: center;
height: auto;
}
.dropdown-content1 a:hover {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
width: 100%;
padding: 0px;
background-color: #FFFFFF;
border: 1px solid black;
font-family: Impact, Haettenschweiler, Franklin Gothic Bold, Arial Black, " sans-serif";
text-align: center;
height: auto;
transition-duration: 0.5s;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2)
}
.dropdown-content2 {
display: none;
position: absolute;
background-color: #f9f9f9;
width: 16.667%;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
padding: 0px;
}
.dropdown-content2 a {
color: white;
text-decoration: none;
display: block;
width: 100%;
padding: 0px;
background-color: #000000;
border: 1px solid white;
font-family: Impact, Haettenschweiler, Franklin Gothic Bold, Arial Black, " sans-serif";
text-align: center;
height: auto;
font-size: 2em;
}
.dropdown-content2 a:hover {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
width: 100%;
padding: 0px;
background-color: #FFFFFF;
border: 1px solid black;
font-family: Impact, Haettenschweiler, Franklin Gothic Bold, Arial Black, " sans-serif";
text-align: center;
height: auto;
transition-duration: 0.5s;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2)
}
.dropdown-content3 {
display: none;
position: absolute;
background-color: #f9f9f9;
width: 16.667%;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
padding: 0px;
}
.dropdown-content3 a {
color: white;
text-decoration: none;
display: block;
width: 100%;
padding: 0px;
background-color: #000000;
border: 1px solid white;
font-family: Impact, Haettenschweiler, Franklin Gothic Bold, Arial Black, " sans-serif";
text-align: center;
height: auto;
font-size: 2em;
}
.dropdown-content3 a:hover {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
width: 100%;
padding: 0px;
background-color: #FFFFFF;
border: 1px solid black;
font-family: Impact, Haettenschweiler, Franklin Gothic Bold, Arial Black, " sans-serif";
text-align: center;
height: auto;
transition-duration: 0.5s;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2)
}
.dropdown-content4 {
display: none;
position: absolute;
background-color: #f9f9f9;
width: 16.667%;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
padding: 0px;
}
.dropdown-content4 a {
color: white;
text-decoration: none;
display: block;
width: 100%;
padding: 0px;
background-color: #000000;
border: 1px solid white;
font-family: Impact, Haettenschweiler, Franklin Gothic Bold, Arial Black, " sans-serif";
text-align: center;
height: auto;
font-size: 2em;
}
.dropdown-content4 a:hover {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
width: 100%;
padding: 0px;
background-color: #FFFFFF;
border: 1px solid black;
font-family: Impact, Haettenschweiler, Franklin Gothic Bold, Arial Black, " sans-serif";
text-align: center;
height: auto;
transition-duration: 0.5s;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2)
}
.show {
display: block;
}
.btn1 {
background-color: #71aace;
color: white;
padding: 0px;
margin: 0px;
font-size: 2em;
font-family: Impact;
border: 1px solid white;
cursor: pointer;
width: 16.667%;
float: left;
}
.btn1:hover,
.btn1:focus {
background-color: #FFFFFF;
color: black;
padding: 0px;
margin: 0px;
font-size: 2em;
font-family: Impact;
border: 1px solid black;
cursor: pointer;
width: 16.667%;
float: left;
box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
transition-duration: 0.5s;
}
.dropbtn1 {
background-color: #6c73b1;
color: white;
padding: 0px;
margin: 0px;
font-size: 2em;
border: 1px solid white;
cursor: pointer;
width: 16.667%;
float: left;
}
.dropbtn1:hover,
.dropbtn1:focus {
background-color: #FFFFFF;
color: black;
padding: 0px;
margin: 0px;
font-size: 2em;
font-family: "Impact";
border: 1px solid black;
cursor: pointer;
width: 16.667%;
float: left;
box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
transition-duration: 0.5s;
}
.dropbtn2 {
background-color: #d3c530;
color: white;
padding: 0px;
margin: 0px;
font-size: 2em;
font-family: Impact;
border: 1px solid white;
cursor: pointer;
width: 16.667%;
float: left;
}
.dropbtn2:hover,
.dropbtn2:focus {
background-color: #FFFFFF;
color: black;
padding: 0px;
margin: 0px;
font-size: 2em;
font-family: "Impact";
border: 1px solid black;
cursor: pointer;
width: 16.667%;
float: left;
box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
transition-duration: 0.5s;
}
.dropbtn3 {
background-color: #82c845;
color: white;
padding: 0px;
margin: 0px;
font-size: 2em;
font-family: Impact;
border: 1px solid white;
cursor: pointer;
width: 16.667%;
float: left;
}
.dropbtn3:hover,
.dropbtn3:focus {
background-color: #FFFFFF;
color: black;
padding: 0px;
margin: 0px;
font-size: 2em;
font-family: "Impact";
border: 1px solid black;
cursor: pointer;
width: 16.667%;
float: left;
box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
transition-duration: 0.5s;
}
.dropbtn4 {
background-color: #8f65a1;
color: white;
padding: 0px;
margin: 0px;
font-size: 2em;
font-family: Impact;
border: 1px solid white;
cursor: pointer;
width: 16.667%;
float: left;
}
.dropbtn4:hover,
.dropbtn4:focus {
background-color: #FFFFFF;
color: black;
padding: 0px;
margin: 0px;
font-size: 2em;
font-family: "Impact";
border: 1px solid black;
cursor: pointer;
width: 16.667%;
float: left;
box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
transition-duration: 0.5s;
}
<div class="navbar">
<button class="btn1" value="About Us" onclick="window.location.href=" https://uklivesound.000webhostapp.com/aboutus.html>About Us</button>
<div class="dropdown">
<button class="dropbtn1" onclick="myFunction1()">Rehearsals</button>
<div class="dropdown-content1" id="myDropdown1">
<a href="https://uklivesound.000webhostapp.com/liveroom.html">Live Room</a>
<a href="https://uklivesound.000webhostapp.com/isolationroom.html">Isolation Room</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn2" onclick="myFunction2()">Recording</button>
<div class="dropdown-content2" id="myDropdown2">
<a href="https://uklivesound.000webhostapp.com/audiorecording.html">Audio</a>
<a href="https://uklivesound.000webhostapp.com/videorecording.html">Video</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn3" onclick="myFunction3()">For Hire</button>
<div class="dropdown-content3" id="myDropdown3">
<a href="https://uklivesound.000webhostapp.com/hirepackages.html">Event Packages</a>
<a href="https://uklivesound.000webhostapp.com/largeevents.html">Large Events</a>
<a href="https://uklivesound.000webhostapp.com/equipmenthire.html">Equipment</a>
<a href="https://uklivesound.000webhostapp.com/bandhire.html">Bands</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn4" onclick="myFunction4()">Other Services</button>
<div class="dropdown-content4" id="myDropdown4">
<a href="https://uklivesound.000webhostapp.com/buyandsell.html">Buy & Sell</a>
<a href="https://uklivesound.000webhostapp.com/repairs.html">Repairs</a>
</div>
</div>
<button class="btn2" value="Contact Us" onclick="window.location.href=" https://uklivesound.000webhostapp.com/contact.html>Contact Us</button>