Bootstrap 4容器全屏宽度,无容器流体类

时间:2018-01-29 20:43:20

标签: html css twitter-bootstrap css3 bootstrap-4

我想使用媒体查询为容器提供100%的宽度,使元素包含在更大的屏幕中而不包含在小屏幕中,反之亦然。这段代码只是一个例子,它适用于Bootstrap v4 alpha 6,但在上一个版本中它不再起作用了。

有人知道解决方案吗?

JSFiddle

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.bundle.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <nav class="navbar navbar-light bg-dark">
    <a class="navbar-brand" href="#">Navbar</a>
  </nav>
</div>
var array = [] as [String]

2 个答案:

答案 0 :(得分:2)

尝试以下代码。当页面宽度小于navbar 时,它会使500px符合页面的宽度,否则它会对您的container类产生正常影响元件。如果您想更改页面大小并进行测试,可以使用 jsFiddel demo

enter image description here

&#13;
&#13;
@media screen and (max-width: 1000px) {
  .container {
      padding-right:0 !important;
      padding-left:0 !important;
      max-width: initial !important;
  }
}
body{
  background:#a7b7bf !important;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <nav class="navbar navbar-light bg-dark">
    <a class="navbar-brand" href="#">Navbar</a>
  </nav>
</div>
&#13;
&#13;
&#13;

如果您希望始终navbar填充页面的整个宽度,您可以轻松地从包装div中删除container类:

&#13;
&#13;
body{
  background:#a7b7bf !important;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

<div>
  <nav class="navbar navbar-light bg-dark">
    <a class="navbar-brand" href="#">Navbar</a>
  </nav>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

以下是如何在不使用container-fluid类的情况下正确执行此操作,而是将所有内容放入container(单击“运行代码段”按钮并展开到全屏以查看它确实已满宽度):

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<nav class="navbar navbar-expand-lg navbar-light bg-light">
   <div class="container">
       <a class="navbar-brand" href="#">Navbar</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>

       <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>
           <form class="form-inline my-2 my-lg-0">
               <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
               <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
           </form>
       </div>
   </div>
</nav>


<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>