.navbar {
background-color: #F91F46;
}
.src-bar {
border: 0;
border-radius: 5px;
outline: none;
padding-left: 15px;
width: 30vw;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" integrity="sha512-b2QcS5SsA8tZodcDtGRELiGv5SaKSk1vDHDaQRda0htPYWZ6046lr3kJ5bAAQdpV2mmA/4v0wQF9MyU6/pDIAg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/js/bootstrap.min.js" integrity="sha512-WW8/jxkELe2CAiE4LvQfwm1rajOS8PHasCCx+knHG0gBHt8EXxS6T6tJRTGuDQVnluuAvMxWF4j8SNFDKceLFg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<nav class="navbar navbar-expand-md fixed-top">
<div class="container-fluid">
<a class="navbar-brand ms-3" href="#">
<img src="img/Logo.avif" width="40px" class="d-inline-block" />
</a>
<form role="search" class="search-bar">
<div class="input-group">
<input class="src-bar" type="search" placeholder="Search For Products" />
<button class="btn bg-white" type="submit">
<i class="fa fa-search"></i>
</button>
</div>
</form>
<div class="justify-content-end">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#nav-colps" aria-controls="nav-colps" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="nav-colps">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown </a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li>
<hr class="dropdown-divider" />
</li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
</div>
</div>
</div>
</nav>
enter image description here
当切换时,我希望折叠div居中。我知道原因-我把折叠和切换btn
放在justify-end
div
里面,因为我需要切换btn
的位置在右边。现在当切换时,我的折叠在右边,但我希望当切换时,折叠div
居中。
当我删除justify-end
div
时,我得到了它的左侧,但如果我删除它,在md
和lg
视图中,每个nav元素都会移动到nav
的左侧。
x1c 0d1x的数据
它看起来像这个或中心
1条答案
按热度按时间gr8qqesn1#
我们确实需要将
.nav-collapse
移动到justify-end
元素之外,以在较窄的视口中获得所需的布局。要使菜单显示在右侧,请通过
flex-grow-0
类将flex-grow: 0
应用于.nav-collapse
元素来覆盖.nav-collapse
上的默认flex-grow: 1
。为了使链接元素在狭窄的视口中居中,
.navbar-nav
中的元素是垂直的flex布局,所以我们可以通过align-items-center
类应用align-items: center
:个字符
您还可以考虑通过
text-center
类应用text-align: center
,将link元素 text 居中设置为窄视口:的字符串