滚动页面时Navbar无法固定在顶部问题求助(附代码)
Hey there! Let's sort out why your navbar isn't sticking to the top when you scroll. I noticed a couple of common issues in your code and setup that might be causing this:
1. Wrong Bootstrap Class Name
You're using the sticky class, but Bootstrap's official class for a sticky top navbar is sticky-top (not just sticky). If you want the navbar to stay fixed at the top permanently (even before scrolling past it), use fixed-top instead.
2. Corrected Navbar Code
Here's the fixed version of your navbar with the proper class and complete toggle button attributes:
<nav id="strumenti" class="navbar navbar-expand-sm navbar-light bg-light sticky-top"> <a class="navbar-brand" href="#"> <img src="C:\Users\Fabry\Desktop\scuola\html\Immagini\UltrasAvellino\clan.ico" width="30" height="30" class="d-inline-block align-top" alt=""> Ultras Avellino </a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <!-- Add your collapsed navigation links here --> <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> <div class="navbar-nav"> <a class="nav-link active" href="#">Home <span class="sr-only">(current)</span></a> <a class="nav-link" href="#">Features</a> <a class="nav-link" href="#">About</a> </div> </div> </nav>
3. Additional Troubleshooting Tips If It Still Fails
- Parent Element Overflow: Double-check that the parent container of your navbar doesn't have
overflow: hiddenoroverflow: autoset—this breaks sticky positioning entirely. - Bootstrap Dependencies: Ensure you've loaded Bootstrap's CSS and JS correctly (plus jQuery and Popper.js if you're using Bootstrap 4, since the collapse button relies on these).
- Permanent Fixed Top (Optional): If you want the navbar stuck at the top always, replace
sticky-topwithfixed-top. Just add padding to your body to prevent content from being hidden under the navbar:body { padding-top: 56px; /* Matches the default height of Bootstrap's small navbar */ }
内容的提问来源于stack exchange,提问作者 SlaaSh
相关产品推荐
相关产品推荐

