My website features a navigation bar that is created using the following code:
body {
margin: 3em;
}
#navbar {
overflow: hidden;
background-color: #333;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
<div id="navbar">
<a class="active" href="javascript:void(0)">Home</a>
<a href="javascript:void(0)">News</a>
<a href="javascript:void(0)">Contact</a>
</div>
window.onscroll = function() {myFunction()};
var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
} else {
navbar.classList.remove("sticky");
}
}
Despite setting a margin of 3em for the body, the navbar div appears to extend past the right side of the body. How can I prevent this expansion?
I attempted adding width: 100%;
and box-sizing: border-box;
to the #navbar
CSS rules, but unfortunately, they did not solve the issue. I am running out of ideas for a solution.