Bootstrap 4 Navbar对齐左侧的徽标中心和切换图标

时间:2018-06-18 13:58:16

标签: twitter-bootstrap bootstrap-4 navbar

我想让徽标居中并在导航栏的左侧链接,当它们在小型设备中折叠时,切换按钮应显示在左侧,此处显示在右侧,徽标应固定(否)崩溃)

<nav class="navbar navbar-expand-sm navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
  </div>
</nav>

2 个答案:

答案 0 :(得分:2)

Bootstrap 4 Navbar使用flexbox,因此可以实现而无需额外的CSS 。您需要一个空的spacer div来保持徽标居中。

Changes not staged for commit:   (use "git add <file>..." to update
what will be committed)   (use "git checkout -- <file>..." to discard
changes in working directory)

         modified:   doc/pySPM Documentation.ipynb

Toggler离开,品牌中心:
https://www.codeply.com/go/VfuMAtIoXi

相关:
How to center nav-items in Bootstrap?
Bootstrap NavBar with left, center or right aligned items

答案 1 :(得分:0)

当然,对不起,我应该早点补充一下。所以基本上我们正在做的是将.navbar品牌移动到中间,给它设置0 auto参数,并调整.navbar-collapse(所以我们菜单的项目)到左边,从顶部给出10px空间导航。然后使用媒体查询,它将开始仅在767px工作,我们将.navbar品牌的cetralize,所以它将始终在中间,我们现在调整.navbar-toggler(菜单图标),因为我们做了更好的菜单项,现在正在获取默认位置,为它们提供以下样式:position:inherit; 然后我还将navbar-expand-sm类修改为navbar-expand-md以更改断点

.navbar-brand {
	margin: 0 auto;
}
.navbar-collapse {
	position: absolute;
	top: 10px;
}

@media (max-width: 767px) {
.navbar-brand {
	text-align: center;
}
.navbar-toggler {
	position: absolute;
	top: 10px;
}
.navbar-collapse {
	position: inherit;
}
}
<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-md navbar-light bg-light"> <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"> <span class="navbar-toggler-icon"></span> </button>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav">
      <li class="nav-item active"> <a class="nav-link" href="#">Home</a> </li>
      <li class="nav-item"> <a class="nav-link" href="#">Features</a> </li>
      <li class="nav-item"> <a class="nav-link" href="#">Pricing</a> </li>
      <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li>
    </ul>
  </div>
</nav>