Center content in responsive bootstrap navbar

后端 未结 11 2010
自闭症患者
自闭症患者 2020-11-22 08:40

I\'m having trouble centering my content in the bootstrap navbar. I\'m using bootstrap 3. I\'ve read many posts, but the CSS or methods used will not work with my code! I\'m

相关标签:
11条回答
  • 2020-11-22 08:48

    Use following html

    <link rel="stylesheet" href="app/components/directives/navBar/navBar.scss"/>
    <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid">
            <!-- Collect the nav links, forms, and other content for toggling -->
                <ul class="nav navbar-nav">
                    <li><h5><a href="#/">Home</a></h5></li>
                    <li>|</li>
                    <li><h5><a href="#products">About</a></h5></li>
                    <li>|</li>
                    <li><h5><a href="#">Contacts</a></h5></li>
                    <li>|</li>
                    <li><h5><a href="#cart">Cart</a></h5></li>
                </ul>
        </div><!-- /.container-fluid -->
    </nav>
    

    And css

    .navbar-nav {
      width: 100%;
      text-align: center;
    }
    .navbar-nav > li {
      float: none;
      display: inline-block;
    }
    .navbar-default {
      background-color: white;
      border-color: white;
    }
    .navbar-default .navbar-nav>.active>a, .navbar-default .navbar-nav>.active>a:hover, .navbar-default .navbar-nav>.active>a:focus{
      background-color:white;
    }
    

    Modify according to your needs

    0 讨论(0)
  • 2020-11-22 08:49

    this should work

     .navbar-nav {
        position: absolute;
        left: 50%;
        transform: translatex(-50%);
    }
    
    0 讨论(0)
  • 2020-11-22 08:50

    Seems like all these answers involve custom css on top of bootstrap. The answer I am providing utilizes only bootstrap so I hope it comes to use for those that want to limit customizations.

    This was tested with Bootstrap V3.3.7

    <footer class="navbar-default navbar-fixed-bottom">
        <div class="container-fluid">
            <div class="row">
                <div class="col-xs-offset-5 col-xs-2 text-center">
                    <p>I am centered text<p>
                </div>
                <div class="col-xs-5"></div>
            </div>
        </div>
    </footer>
    
    0 讨论(0)
  • 2020-11-22 08:52

    There's another way to do this for layouts that doesn't have to put the navbar inside the container, and which doesn't require any CSS or Bootstrap overrides.

    Simply place a div with the Bootstrap container class around the navbar. This will center the links inside the navbar:

    <nav class="navbar navbar-default">
      <!-- here's where you put the container tag -->
      <div class="container">
    
        <div class="navbar-header">
        <!-- header and collapsed icon here -->
        </div>
    
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
          <!-- links here -->
          </ul>
        </div>
      </div> <!-- close the container tag -->
    </nav> <!-- close the nav tag -->
    

    If you want the then align body content to the center navbar, you also put that body content in the Bootstrap container tag.

    <div class="container">
      <! -- body content here -->
    </div>
    

    Not everyone can use this type of layout (some people need to nest the navbar itself inside the container). Nonetheless, if you can do it, it's an easy way to get your navbar links and body centered.

    You can see the results in this fullpage JSFiddle: http://jsfiddle.net/bdd9U/231/embedded/result/

    Source: http://jsfiddle.net/bdd9U/229/

    0 讨论(0)
  • 2020-11-22 08:54

    V4 of BootStrap is adding Center (justify-content-center) and Right Alignment (justify-content-end) as per: https://v4-alpha.getbootstrap.com/components/navs/#horizontal-alignment

    <ul class="nav justify-content-center">
      <li class="nav-item">
        <a class="nav-link active" href="#">Active</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    
    0 讨论(0)
  • 2020-11-22 08:56

    from the bootstrap official site (and, almost, like Eric S. Bullington said.

    https://getbootstrap.com/components/#navbar-default

    the example navbar looks like:

    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
            ...etc
    

    to center the nav, that what i've done:

    <div class="container-fluid" id="nav_center">
    

    css:

    @media (min-width:768px) { /* don't break navbar on small screen */
        #nav_center {
            width: 700px /* need to found your width according to your entry*/
        }
    }
    

    work with class="container" and navbar-right or left, and of course you can replace id by class. :D

    0 讨论(0)
提交回复
热议问题