bootstrap 4 导航栏在左侧和右侧有填充
Posted
技术标签:
【中文标题】bootstrap 4 导航栏在左侧和右侧有填充【英文标题】:bootstrap 4 navbar has padding on left and right side 【发布时间】:2019-03-25 11:21:50 【问题描述】:我不确定我在这里缺少什么,我知道我可以添加 CSS 以从父 div 中删除填充,但我认为引导程序会使导航栏成为页面的整个宽度?这是我的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Contact</title>
<!--Bootstrap CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="container-fluid" id="outer">
<header>
<nav class="navbar navbar-expand-lg navbar-light bg-secondary">
<a class="navbar-brand" href="#">
<img src="\movieTheater.jpg" >
Movie Theater
</a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="home.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.html">Contact</a>
</li>
</ul>
</div>
</nav>
</header>
</div>
</body>
</html>
【问题讨论】:
或者在container-fluid
标签之前使用nav
标签。
【参考方案1】:
container-fluid 类的宽度为 100%,但应用了一些填充。要管理填充,您可以使用 Bootstrap 4 中的 p-*
类(阅读文档中的 spacing section)。在您的特定情况下,您需要 p-0
class 来删除填充。另外,我添加了折叠菜单按钮,如果您不包含它,您的navbar
在折叠时将无法正常工作。检查下一个示例:
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<div class="container-fluid p-0" id="outer">
<header>
<nav class="navbar navbar-expand-lg navbar-light bg-secondary">
<a class="navbar-brand" href="#">
<img src="\movieTheater.jpg" > Movie Theater
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="home.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.html">Contact</a>
</li>
</ul>
</div>
</nav>
</header>
</div>
【讨论】:
一旦我将padding 0
添加到container-fluid
,就会出现水平滚动以上是关于bootstrap 4 导航栏在左侧和右侧有填充的主要内容,如果未能解决你的问题,请参考以下文章
Bootstrap 4导航栏,带有品牌中心和左侧,中心和右侧的链接[重复]
Bootstrap 3 Navbar 左侧和右侧导航栏项目的左侧折叠菜单
Twitter bootstrap 3:导航栏在词缀触发时改变宽度