var dropdownContent = document.getElementById("myDropdown");
var isOpen = false;
function toggleDropdown() {
if (!isOpen) {
fadeIn(dropdownContent);
} else {
fadeOut(dropdownContent);
}
}
function fadeIn(element) {
var opacity = 0;
element.style.display = "block";
var fadeInInterval = setInterval(function() {
if (opacity < 1) {
opacity += 0.1;
element.style.opacity = opacity;
} else {
clearInterval(fadeInInterval);
}
}, 30);
isOpen = true;
}
function fadeOut(element) {
var opacity = 1;
var fadeOutInterval = setInterval(function() {
if (opacity > 0) {
opacity -= 0.1;
element.style.opacity = opacity;
} else {
clearInterval(fadeOutInterval);
element.style.display = "none";
}
}, 30);
isOpen = false;
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
fadeOut(dropdownContent);
}
}
.container {
width: 1200px;
margin: 0 auto;
padding: 0 15px;
}
ul {
list-style: none;
}
.header {
padding: 20px 50px;
gap: 225px;
background-color: rgba(255, 255, 255, 0.7);
box-shadow: 0px -3px 21px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(5px);
position: fixed;
z-index: 1;
width: 100%;
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
.logo img {
width: 120px;
}
.header .main_menu {
display: flex;
}
.header .main_menu li a {
display: block;
font-family: 'Josefin Sans', sans-serif;
font-weight: 700;
color: black;
text-decoration: none;
font-size: 18px;
padding: 10px 25px;
margin: 0px 3px;
}
.header .main_menu li {
position: relative;
}
#home {
background-color: #CD285B;
color: #f1f1f1;
border-radius: 5px;
}
.main_menu li:hover>a {
background-color: #FFD7E3;
border-radius: 5px;
}
.dropbtn {
display: flex;
align-items: center;
font-family: 'Josefin Sans', sans-serif;
font-weight: 600;
background-color: #CD285B;
color: white;
padding: 12px 20px;
border-radius: 5px;
font-size: 18px;
border: none;
cursor: pointer;
height: 100%;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown .dropbtn .fa {
font-size: 12px;
}
.dropbtn .fa {
padding-left: 10px;
margin-left: 5px;
}
.dropdown-content {
display: none;
position: absolute;
font-family: 'Josefin Sans', sans-serif;
font-weight: 600;
font-size: 18px;
margin-top: 25px;
gap: 10px;
right: -27%;
background-color: rgba(255, 255, 255, 0.7);
border: 1px solid rgba(255, 255, 255, 0.7);
box-shadow: 0px 182px 73px rgba(205, 40, 91, 0.01), 0px 103px 62px rgba(205, 40, 91, 0.05), 0px 46px 46px rgba(205, 40, 91, 0.09), 0px 11px 25px rgba(205, 40, 91, 0.1), 0px 0px 0px rgba(205, 40, 91, 0.1);
border-radius: 10px;
width: 235px;
z-index: 1;
align-items: center;
}
.dropdown-content a {
display: flex;
flex-direction: row;
align-items: center;
padding: 15px 20px;
margin: 6px;
color: black;
font-size: 16px;
color: #303030;
text-decoration: none;
}
.dropdown a:hover {
background-color: #f1f1f1;
border: 1px solid rgba(219, 0, 84, 0.588);
margin: 5px;
border-radius: 10px;
}
.dropdown:hover .dropbtn {
background-color: #cd285cdc;
transition: all .3s ease-in-out;
}
.dropdown-content .fa-solid {
color: #303030;
font-size: 16px;
padding-right: 10px;
}
.show {
display: block;
}
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sample Business</title>
<link rel="logo icon" href="framelogoicon.png" />
<link rel="stylesheet" href="FrameDevG.css">
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://kit.fontawesome.com/afde9d8d8e.js" crossorigin="anonymous"></script>
</head>
<body class="prevent-select">
<header class="header">
<div class="container">
<div class="navbar">
<div class="logo">
<a href="">
<img src="framelogo1.png" alt="Business Sample Logo">
</a>
</div>
<nav>
<ul class="main_menu">
<li><a href="index.html" id="home">Home</a></li>
<li><a href="FrameDevG.html" id="gallery">Gallery</a></li>
<li><a href="FrameDevAbout.html" id="about-us">About Us</a></li>
</ul>
</nav>
<div class="dropdown">
<button class="dropbtn" onclick="toggleDropdown()">Book Now<i class="fa fa-sort-desc" onclick="showMenu()"></i> </button>
<div class="dropdown-content" id="myDropdown">
<a href="#bookframe"><i class="fa-solid fa-music"></i>Music Video</a>
<a href="#bookframe"><i class="fa-solid fa-camera"></i>Photography</a>
<a href="#bookframe"><i class="fa-solid fa-globe"></i>Website Development</a>
<a href="#bookframe"><i class="fa-solid fa-palette"></i>Graphic Design</a>
<a href="#bookframe"><i class="fa-solid fa-clapperboard"></i>Commercial</a>
<a href="#bookframe"><i class="fa-solid fa-chart-line"></i>Marketing Content</a>
</div>
</div>
</div>
</div>
</header>
I tried adding media queries and JavaScript but I don’t now why I can’t make it responsive. When I try to smaller device the navigator is cut off.
**I want the navigator to be responsive and has a hamburger menu when viewed in smaller devices. **
It’s my first time making a navigator with dropdown menu that’s why I don’t really know what to do or I’m not too familiar with it.
Your help will help me a lot since I could use this with my future assignments etc.
2
Answers
Something like the following would do it. I marked where the new code is:
You can use media queries to make elements look different on mobile: