gpt4 book ai didi

css - Bootstrap 5 导航栏和固定顶部位置

转载 作者:行者123 更新时间:2023-12-05 01:59:18 24 4
gpt4 key购买 nike

我正在尝试将导航栏与固定顶部类一起使用,以便菜单在折叠时不会下拉页面元素。但是当我使用固定顶部类时,导航栏将越过容器(宽度),就好像它在容器流体中一样,而我使用容器类将菜单和站点放在页面中间(不是全宽)。有没有办法让导航栏具有固定顶部但不是全宽?

这是代码:

<div class="container" style="border: 1px solid;">
<nav class="navbar fixed-top navbar-expand-lg">
<div class="container-fluid">
<a class="navbar-brand" href="#"><img src="n-img/logo.png" class="img-responsive"></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="n-index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Chi Siamo</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Dove Siamo</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contatti</a>
</li>
</ul>
</div>
</div>
</nav>
</div> <!-- end container -->

最佳答案

当使用 position:fixed 时,元素从正常的 DOM 流中移除,因此它不再与其他页面元素相关,包括其父容器。

如果您想将导航栏限制为容器宽度,请使用导航栏内部的容器来包裹导航栏内容...

<nav class="navbar fixed-top navbar-expand-lg">
<div class="container">
<a class="navbar-brand" href="#"><img src="n-img/logo.png" class="img-responsive"></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="n-index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Chi Siamo</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Dove Siamo</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contatti</a>
</li>
</ul>
</div>
</div>
</nav>

https://codeply.com/p/AFQj4wf6DD

关于css - Bootstrap 5 导航栏和固定顶部位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67934511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com