Search code examples
twitter-bootstrapnavbargrid-layout

Is it possible to set width of "navbar-fixed-top" using Bootstrap "col-" classes?


I don't control width of "navbar-fixed-top" using Bootstrap "col-" classes. Suggestions I found so far recommend additions to Bootstrap CSS or use of 'container-fluid', though I haven't figured out how to control width with "col--" at the latter option.

Basically, I want to get the same to the following script but with navbar fixed to the top.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
  <div class="col-md-2"></div>
  <div class="col-md-8">

    <nav class="navbar navbar-inverse">
      <div class="navbar-header">
        <a class="navbar-brand" href="#">WebSiteName</a>
      </div>
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a></li>
        <li><a href="#">Page 3</a></li>
      </ul>
    </nav>

    <p>1. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
    <p>2. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
    <p>3. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
    <p>4. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>

  </div>
  <div class="col-md-2"></div>
</body>
</html>

I've used this W3Schools page on navbar-fixed-top to test HTML above. Do you have any working ideas?


Solution

  • The solution I come so far utilizes .container from Bootstrap CSS. Please see the script below. The only imperfection I noticed so far is a pixel-wide difference in width of navbar and content in various stages of browser resize.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Case</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
      <!-- start of css overwriting container, add 'container-overwrite' to <nav class> !-->
      <style>
        @media (min-width: 768px) {
        .container-overwrite{
            width:96.3%;
        }}
    
        @media (min-width: 992px) {
        .container-overwrite{
            width:63.7%;
        }}
    
        @media (min-width: 1200px) {
        .container-overwrite{
            width:64.7%;
        }}
    
        body {
              padding-top: 70px;
        }
      </style>
      <!-- end of css !-->
    
    </head>
    <body>
    
      <div class="col-md-2"></div>
      <div class="col-md-8">
        <div class="container">
    
          <nav class="navbar navbar-inverse navbar-fixed-top container container-overwrite">
            <div class="navbar-header">
              <a class="navbar-brand" href="#">WebSiteName</a>
            </div>
            <ul class="nav navbar-nav">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="#">Page 1</a></li>
              <li><a href="#">Page 2</a></li>
              <li><a href="#">Page 3</a></li>
            </ul>
          </nav>
        </div>
    
        <p>1. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
        <p>2. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
        <p>3. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
        <p>4. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
      </div>
      <div class="col-md-2"></div>
    </body>
    </html>