我正在尝试创建导航栏,该导航栏的左侧是一个汉堡,图标居中。我该如何快速地做到这一点?
<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>
<div class="pos-f-t">
<nav id="navbar" class="navbar navbar-expand-sm fixed-top navbar-dark bg-dark d-flex flex-row">
<button class="navbar-toggler d-flex justify-content-start" type="button">
<span class="navbar-toggler-icon"></span>
</button>
<img src="https://picsum.photos/20" alt="apple icon" class="d-flex justify-content-center mx-auto" id="icon">
</nav>
</div>
预期结果: -汉堡保持左,但苹果图标保持居中。
实际结果: -汉堡稍微向右推苹果图标, -或什么都没有改变,只是坐在右侧。
答案 0 :(得分:2)
刘易斯,您的代码运行正常。我刚刚添加了bootstrap4 CSS文件。下面是我的代码。
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<div class="pos-f-t">
<nav
id="navbar"
class="navbar navbar-expand-sm fixed-top navbar-dark bg-dark d-flex flex-row"
>
<button class="navbar-toggler d-flex justify-content-start" type="button">
<span class="navbar-toggler-icon"></span>
</button>
<img
src="http://www.newdesignfile.com/postpic/2012/09/small-apple-logo_154074.jpg"
alt="apple icon"
class="d-flex justify-content-center mx-auto"
id="icon"
width="50px"
/>
</nav>
</div>
答案 1 :(得分:1)
答案 2 :(得分:0)
您可以在此处添加等于导航栏切换按钮的宽度(宽度= 56像素)的右侧填充到导航栏。它已经有一个16px的右填充。因此,我们的总填充应为16px + 56px = 72px。
这将使图像精确对准导航栏的中心。
.navbar-fix {
padding-right: 72px !important;
}
<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>
<div class="pos-f-t">
<nav id="navbar" class="navbar navbar-expand-sm fixed-top navbar-dark bg-dark d-flex flex-row navbar-fix">
<button class="navbar-toggler d-flex justify-content-start" type="button">
<span class="navbar-toggler-icon"></span>
</button>
<img src="https://picsum.photos/20" alt="apple icon" class="d-flex justify-content-center mx-auto" id="icon">
</nav>
</div>