skip to Main Content

I want to give the container a 100% width using media queries, having the elements contained in a larger screen and not contained in a small one or vice versa. This code is just an example and it works with Bootstrap v4 alpha 6, but in the last release it doesn’t work anymore.

Anybody knows a solution?

JSFiddle

@media screen and (max-width: 1000px) {
  container {
    width: 100%;
  }
}
<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>

2

Answers


  1. Here’s how you do it correctly without using the container-fluid class but instead putting everything inside into a container (click the “run code snippet” button and expand to full screen to see that it’s indeed full width):

    <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>
    Login or Signup to reply.
  2. Try the following code. It fit the navbar to the width of the page when the page width is less than 500px, else it has the normal effects of a container class on your element. If you want change the page size and test it you can use jsFiddel demo.

    enter image description here

    @media screen and (max-width: 1000px) {
      .container {
          padding-right:0 !important;
          padding-left:0 !important;
          max-width: initial !important;
      }
    }
    body{
      background:#a7b7bf !important;
    }
    <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>

    And if you want always the navbar fill full width of the page you can easily remove container class from wrapper div:

    body{
      background:#a7b7bf !important;
    }
    <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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search