I would like to create a right side inline navigation bar with dropdown functionality. When the screen size is small, I want to hide all the menus and display a button. Clicking on the button should reveal a vertical navigation bar.
I am facing an issue in the media query where I am unable to toggle the visibility of the dropdown menu.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Business 2</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.nav {
float: right;
list-style: none;
text-align: left;
margin: 0;
display:inline;
}
.nav > li {
display:inline-block;
padding: 10px 9px 10px 9px;
}
.nav > li a {
text-decoration: none;
color: black;
font-size: 16px;
padding: 20px 15px 20px 15px;
}
.nav > li > a:hover {
color: black;
}
ul li > ul li {
position:relative;
display:none;
width:150px;
height:50px;
line-height:50px;
}
.dropdown {
display: inline-block;
}
.dropdown .dropbtn {
border: none;
outline: none;
color: black;
background-color: inherit;
margin: 0;
}
.nav-right a:hover {
background-color: #E7E7E7;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content {
display: none;
position: absolute;
min-width: 80px;
top: 40px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
background-color: white;
text-align: left;
}
.dropdown-content {
display: none;
position: absolute;
background-color: black;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.nav .icon {
display: none;
}
@media screen and (max-width: 680px) {
ul.nav li a {display: none;}
ul > li .dropbtn{
display: none;
}
ul.nav li a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 680px) {
ul.nav.responsive {position: relative;}
ul.nav.responsive li a.icon{
position: absolute;
right: 0;
top: 0;
}
ul.nav.responsive li {
display: block;
text-align: left;
}
ul.nav.responsive li a {
display: block;
text-align: left;
}
ul.nav.responsive li.dropdown-content {position: relative;}
ul.nav.responsive li.dropdown{
display: block;
width: 100%;
text-align: left;
}
}
</style>
</head>
<body>
<ul class="nav" id="topnav">
<li><a href="#" class="active" >item1</a></li>
<li>
<div class="dropdown">
<button class="dropbtn">item2
<i class="fa fa-caret-down"></i></button>
<div class="dropdown-content">
<a href="#">subitem1</a>
<a href="#">subitem2</a>
<a href="#">subitem3</a>
</div>
</div>
</li>
<li><a href="#">item3</a></li>
<li>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</li>
</ul>
<script>
function myFunction() {
var x = document.getElementById("topnav");
if (x.className === "nav") {
x.className += " responsive";
} else {
x.className = "nav";
}
}
</script>
</body>
</html>
Best regards,