在 Bootstrap 5 中,当导航栏折叠时,如何将导航栏品牌移动到中心(当导航栏展开时,保持在左侧/右侧)
简单的解决方案
你可以通过在品牌元素后面添加一个空的DIV来非常轻松地解决这个问题:
<a class="navbar-brand" href="#">Brand Name</a>
<div></div>
为什么会这样?
导航栏使用显示柔性和 justify-content: space- Between。额外的 DIV 创建 3 列而不是 2 列,并且品牌位于中心。
片段
运行代码片段并调整大小以触发断点。折叠时品牌位于中间,否则显示在左侧。
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Brand Name</a>
<div></div>
<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
<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">
<a class="nav-link disabled" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/boo[email protected]/dist/js/bootstrap.bundle.min.js"></script>