Html Bootstrap 导航栏 100% 宽度不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30680697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:36:05  来源:igfitidea点击:

Bootstrap navbar 100% width not working

htmlcsstwitter-bootstrap

提问by Mr. ED

On my bootstrap nav bar I am trying to make to go to the full with on page even when nav bar is on top of image.

在我的引导导航栏上,即使导航栏位于图像顶部,我也试图在页面上显示完整内容。

But I have tried width 100% on .navbar-wrapperand navbarand navbar-defaultBut for some reason the navbar will still not go full width of page.

但是我已经尝试了 100% 的宽度.navbar-wrapper并且navbarnavbar-default但是由于某种原因导航栏仍然不会完全宽度页面。

Live Examples

活生生的例子

Here is my codepen Code View

这是我的 codepen代码视图

Here is my codepen Full View

这是我的代码笔完整视图

QuestionHow can I make my navbar width 100% work so it goes to the full width of page?

问题如何使我的导航栏宽度 100% 工作,使其达到页面的全宽?

CSS

CSS

/* Special class on .container surrounding .navbar, used for positioning it into place. */
.navbar-wrapper {
    position: absolute;
    top: 0;
    right: 0;
    width: 100% !important;
    left: 0;
    z-index: 20;
}

/* Flip around the padding for proper display in narrow viewports */
.navbar-wrapper > .container {
    padding-right: 0;
    padding-left: 0;
}

.navbar-wrapper .navbar {
    padding-right: 15px;
    padding-left: 15px;
    width: 100% !important;
}

.navbar-inverse {
  width: 100% !important;
    background-color: rgba(0, 0, 0, 0.5) !important;
}

.navbar-inverse .nav > li > a {
    color: #FFFFFF;
}

.navbar-inverse .nav > li > a:hover {
    background: none;
}

.navbar-brand a {
    color: #FFFFFF;
}

.main-header-background {
    background-size: 100% 100%;
    background-repeat: no-repeat;
    background-position: center; 
    background-image: url('./images/stars/img-stars-2.jpg');
    height: 620px;
    width: 100%;
    margin-bottom: 60px;
}

HTML

HTML

<div class="navbar-wrapper">
<div class="container">

<nav class="navbar navbar-inverse">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="navbar-brand">
<a href="#">Brand</a>
</div>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

<ul class="nav navbar-nav">
<li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
<li class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>

<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>

</div><!-- /.navbar-collapse -->
</nav>
</div>
</div>

<div class="main-header-background">
</div>  

采纳答案by user2977636

You could get the width of the navbar to go the full length of the page by making the container inherit from ".navbar-wrapper". You may also want to get rid of the padding you have set in ".navbar-wrapper .navbar"

通过使容器从“.navbar-wrapper”继承,您可以使导航栏的宽度达到页面的全长。您可能还想摆脱在“.navbar-wrapper .navbar”中设置的填充

The following styles worked for me: See codpen: http://codepen.io/JordanLittell/pen/wadWxv

以下样式对我有用:请参阅 codpen:http://codepen.io/JordanLittell/pen/wadWxv

/* Special class on .container surrounding .navbar, used for positioning it into place. */

/* 围绕 .navbar 的 .container 上的特殊类,用于将其定位到位。*/

.navbar-wrapper {
    position: absolute;
    top: 0;
    right: 0;
    width: 100% !important;
    left: 0;
    z-index: 20;
}

/* Flip around the padding for proper display in narrow viewports */
.navbar-wrapper > .container {
    padding-right: 0;
    padding-left: 0;
    width: inherit;
}

.navbar-wrapper .navbar {
    width: 100% !important;
}

.navbar-inverse {
  width: 100% !important;
    background-color: rgba(0, 0, 0, 0.5) !important;
  border-radius: 0;
}

.navbar-inverse .nav > li > a {
    color: #FFFFFF;
}

.navbar-inverse .nav > li > a:hover {
    background: none;
}

.navbar-brand a {
    color: #FFFFFF;
}

.main-header-background {
    background-size: 100% 100%;
    background-repeat: no-repeat;
    background-position: center; 
    background-image: url('http://www.riwakawebsitedesigns.com/external/images/stars/img-stars-2.jpg');
  height: 620px;
  width: 100%;
  margin-bottom: 60px;
}

回答by Ivan Sivak

Seems you are using .containerclass instead of container-fluidclass. As per bootstrap documentation:

似乎您正在使用.container类而不是container-fluid类。根据引导程序文档:

Turn any fixed-width grid layout into a full-width layout by changing your outermost .containerto .container-fluid.

通过将最外层更改.container.container-fluid.

http://getbootstrap.com/css/#grid-example-fluid

http://getbootstrap.com/css/#grid-example-fluid

updated codepen: http://codepen.io/anon/pen/gpWMzJ

更新代码笔:http://codepen.io/anon/pen/gpWMzJ

回答by Mathieu David

You should use the class container-fluidinstead of container.

您应该使用类container-fluid而不是container.

  • containerhas predefined width values for the different screen sizes (xs, sm, md, lg)

  • container-fluidfills the available width

  • container具有针对不同屏幕尺寸(xs、sm、md、lg)的预定义宽度值

  • container-fluid填充可用宽度

Also container's have some padding applied, and generally they are used in conjunction with row's that have the inverse margin.

container的具有施加一些填充,并且通常它们被配合使用,row“具有逆余量秒。

So if you want full-widthyou either have to wrap your navbar in a row (inside your container) or you just don't use container's at all.

因此,如果您想要全角,则必须将导航栏排成一行(在容器内),或者根本不使用container's。