You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

滚动页面时Navbar无法固定在顶部问题求助(附代码)

Fixing Your Navbar Sticky Top Issue

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: hidden or overflow: auto set—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-top with fixed-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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:26:30