I'm experiencing an issue with my navbar and its drop-down menus. When I hover over the submenu, it doesn't stay visible as expected. I've tried different approaches such as using jQuery, the + operator in CSS, and even creating a separate hidden div to maintain the appearance of the submenu without success. The jQuery example I attempted throws an error stating that submenu is not defined, leaving me unsure of how to resolve it. Despite my efforts, the submenu still fails to display properly.
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0;
}
.navbar {
display: flex;
align-items: center;
overflow: hidden;
background-color: #333;
}
.navbar a {
float: left;
font-size: 39px;
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.navbar a:hover {
background-color: #ddd;
}
.navbar a.active {
background-color: #04aa6d;
color: white;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
/* transition: height 0.25s; */
}
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: red;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
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;
text-align: left;
}
.dropdown-submenu {
/* ---Check jQuery--- */
display: none;
z-index: 1;
left:100%;
top: 20px;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
position: absolute;
}
.dropdown-submenu:hover {
background-color: #ddd;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.collapse {
position: absolute;
background-color: rgb(80, 78, 78);
color: #fffefed7;
box-shadow: 0.3rem 0.3rem 5.5rem #525c5c, 0em 0em 0.9em #ffffff6b;
cursor: pointer;
top: 4.5%;
border-radius: 50%;
border-color: rgba(32, 30, 30, 0.863);
display: flex; /* or inline-flex */
align-items: center;
justify-content: center;
width: 25px;
height: 25px;
right: 50%;
transition: background-color 0.2s ease-out 1ms;
}
.collapse:hover {
background-color: rgb(192, 192, 192);
}
.collapse:active {
transform: translateY(1px);
background-color: rgb(109, 103, 103);
}
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel = "stylesheet" href = "cssjavascript/style.css"/>
<script src="cssjavascript/addon.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e697c6f6a6e1c1e00001a16">[email protected]</a>,100..700,0..1,-50..200" />
<style>
h3 {
left: 2%;
max-width: 200px;
padding: 20px;
margin: 20px;
}
p {
max-width: 200px;
padding: 20px;
margin: 20px;
word-wrap: break-word;
}
</style>
<title>Contact Page</title>
</head>
<body>
<div class="navbar">
<a href="#home">Home</a>
<a href="#news">FAQ</a>
<div class="dropdown">
<button class="dropbtn">Coding ▼
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a class = "parentdrop" href="#">Web Development ▶</a>
<div class = "dropdown-submenu">
<a href="#">HTML</a>
<a href="#">CSS</a>
<a href="#">JavaScript</a>
</div>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
<a href="contact.html">Contact Us</a>
<button title = "Collapse Navbar" class = "collapse"><span class="material-symbols-outlined">
expand_less
</span></button>
</div>
<h1 style = "position: relative; display: flexbox; text-align:center; width: auto;">Contact Page</h1>
<form action="https://formsubmit.co/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e88b9a91989c8789868c8b9a89928da88f85898184c68b8785">[email protected]</a>" method="POST" class = "formDiv">
<input type="text" name="_subject" placeholder = "Write your subject here" required>
<input type="email" name="email" placeholder = "Email Address" required>
<!-- Change redirect value from local IP address to actual website link later -->
<input type="hidden" name="_next" value="http://127.0.0.1:5500/thankyou.html">
<input type = "text" name = "message" placeholder = "Write your message here..." required>
<button type="submit">Send</button>
</form>
<script>
$(document).ready(function(){
let submenu = document.getElementsByClassName("dropdown-submenu");
$(".parentdrop").on({
mouseenter: function () {
for (let i = 0; i < submenu.length; i++) submenu[i].style.display = "block";
},
mouseleave: function () {
for (let i = 0; i < submenu.length; i++) submenu[i].style.display = "none";
}
});
});
$(".dropdown-submenu").on({
mouseenter: function () {
for (let i = 0; i < submenu.length; i++) submenu[i].style.display = "block";
},
mouseleave: function () {
for (let i = 0; i < submenu.length; i++) submenu[i].style.display = "none";
}
});
</script>
</body>
</html>