I'm currently in the process of developing a football-focused website and am tackling the navigation bar. I've implemented code that allows the navbar to collapse with a hamburger button when the screen size shrinks. However, once I resize the screen back to its original width, the navbar content disappears.
Below is the snippet of my code:
const navbar = document.getElementById('navbar')
let number = 0
function toggleFunction() {
console.log(getComputedStyle(navbar).display)
if (number === 1) {
navbar.style.display = 'none'
number -= 1
} else {
navbar.style.display = 'inline-block'
navbar.style.position = 'absolute'
navbar.style.top = '75px'
navbar.style.width = '100%'
navbar.style.textAlign = 'center'
navbar.style.padding = ' 5px 0'
navbar.style.background = 'orange'
navbar.style.height = "auto";
number += 1
}
console.log(navbar)
}
.toggle-btn {
width: 35px;
position: absolute;
right: 80px;
top: 25px;
display: none;
}
.toggle-btn span {
display: inline-block;
width: 100%;
height: 2px;
background-color: #fff;
float: left;
margin-bottom: 8px;
}
@media (max-width: 800px) {
.toggle-btn {
display: block;
}
#navbar {
display: none;
}
#header {
display: block;
}
}
@media (min-width: 801px) {
#navbar {
display: inline-flex;
width: 100%;
justify-content: space-evenly;
padding-left: 15%;
align-self: center;
height: 75px;
align-items: center;
}
}
#header {
display: inline-flex;
width: 100%;
color: #fff;
background-color: #000;
height: 75px;
}
#navbar {
display: inline-flex;
width: 100%;
justify-content: space-evenly;
padding-left: 15%;
align-self: center;
height: 75px;
align-items: center;
}
<div class="toggle-btn" onclick="toggleFunction()">
<span></span>
<span></span>
<span></span>
</div>
<nav id="header">
<img src="pantherlogo.png" alt="Beverly Panthers Logo" id="logo">
<div id="navbar">
<h3><a href="index.html">HOME</a></h3>
<h3><a href="news.html">NEWS</a></h3>
<h3><a href="team.html">TEAMS</a></h3>
<h3><a href="history.html">HISTORY</a></h3>
<h3><a href="contacts.html">CONTACTS</a></h3>
</div>
</nav>
<div id="top">
<img src="footballimg.png" alt="football" id="footballimg">
<div id="motto">
<h1>Restore our pride.</h1>
</div>
</div>