我想设置Bootstrap 4导航栏覆盖的宽度,当它通过点击汉堡图标显示,以匹配其内容,而不是占据整个屏幕。我查看了navbar,navbar-collapse和navbar-nav类,一些flex文档,我还不清楚如何执行此操作。
答案 0 :(得分:0)
您可以利用navbar-toggler
也可以处理导航栏之外的事实。
因此,您需要在导航栏外创建一个container
,将row
放在内部,将col-auto
放入该行。
将整个collapse navbar-collapse
div放入该自动列中,现在可以完全按照您的意愿执行操作。唯一的缺点是,对于导航栏的展开状态,您必须复制导航栏中的导航栏项列表。
另请注意,您需要将style="margin-top: -1px !important"
添加到外部容器中,否则它会突然出现一个像素。 (m-0
删除容器上的常规边距)
这是一个正常工作的代码段(点击下面的“运行代码段”):
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-DISABLED navbar-dark bg-dark">
<a class="navbar-brand" href="#">
<img class="brand-icon mr-2" src="https://placehold.it/60" width="30" height="30" alt="">Company
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</nav>
<div class="container m-0" style="margin-top: -1px !important">
<div class="row">
<div class="col-auto navbar-dark bg-dark">
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></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-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</div>
</div>
</div>
</div>