I am currently working on incorporating a bootstrap sticky navbar with a fixed height of 81px and smooth-scroll behavior into my project.
The website's HTML structure includes section tags like
<section class="section" id="news">
, which allows users to navigate to specific sections by clicking on <a class="nav-link" href="#news">News</a>
in the navbar. To achieve this, I am utilizing CSS and JS technologies that are loaded at the beginning of the <body>
:
- https://getbootstrap.com/docs/4.6/components/navbar/
- https://github.com/iamdustan/smoothscroll
- https://github.com/jonaskuske/smoothscroll-anchor-polyfill
Initially, everything works perfectly: when the browser window reaches the bootstrap specific --breakpoint-lg
of 992px, the navbar collapses into a burger menu. To accommodate the fixed-height navbar, I have added code to my custom.css
(loaded after the bootstrap.min.css
), following a helpful tip from https://css-tricks.com/fixed-headers-on-page-links-and-overlapping-content-oh-my/
@media (max-width: 991px) {
section {
padding-top: 382px;
margin-top: -382px;
}
}
@media (min-width: 992px) {
section {
padding-top: 80px;
margin-top: -80px;
}
}
While this setup functions properly on larger browser window sizes, when loaded on smaller widths (991px and below) - particularly on mobile devices - clicking on the navbar-link causes a slight vertical offset. Do you have any insights into why this occurs? It appears that my media query for max-width: 991px may not be working as intended.