Bootstrap Center Navbar Items

后端 未结 5 1982
独厮守ぢ
独厮守ぢ 2020-12-03 05:19

I know I can push navbar items to the left and right, but how would I center them?

text-align:center;

doesn\'t work nor any of the other th

相关标签:
5条回答
  • 2020-12-03 05:27

    I had similar problem. What worked for me is adding rule 'justify-content' like this:

    CSS:

    .navbar-expand-lg .navbar-collapse {
    display: flex!important;
    justify-content: space-around;
    flex-basis: auto;
    }
    

    HTML:

    <nav id="hamburger" class="navbar sticky-top navbar-expand-lg navbar-light">
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
        <div class="navbar-nav center">
            <div><a class="nav-item nav-link active" href="#">
              Start <span class="sr-only">(current)</span></a>
            </div>
            <div><a class="nav-item nav-link" href="#">Portfolio</a></div>
            <div><a class="nav-item nav-link" href="#">Blog</a></div>
            <div><a class="nav-item nav-link" href="#">O mnie</a></div>
            <div><a class="nav-item nav-link" href="#">Kontakt</a></div>
        </div>
      </div>
    </nav>
    
    0 讨论(0)
  • 2020-12-03 05:33

    Use text-center or mx-auto or container-fluid or use flexbox technique, flex justify-center

    1. You could use this hack text-center (this does not only apply for text)

    2. Or use the exact boostrap class mx-auto

    example class="text-center" or class="mx-auto"

    in context

    <div class="btn-group text-center"> or <div class="btn-group mx-auto">
    

    TRY NOT TO USE inline styles (bad practice)

    <div class="btn-group" style="margin:0 auto">
    

    References here:

    text-center => https://getbootstrap.com/docs/4.0/utilities/text/

    mx-auto => https://getbootstrap.com/docs/4.0/utilities/spacing/#horizontal-centering

    0 讨论(0)
  • 2020-12-03 05:37

    You will need to modify some CSS rules for Navbar component. So add a class center to nav.navbar and the following rules:

    .navbar.center .navbar-inner {
        text-align: center;
    }
    
    .navbar.center .navbar-inner .nav {
        display:inline-block;
        float: none;
    }
    

    Working demo (Bootstrap 3.3.7)

    0 讨论(0)
  • 2020-12-03 05:44

    To extend upon the selected answer, adding vertical align top will prevent the space from appearing under the nav items.

    .navbar.center .navbar-inner {
        text-align: center;
    }
    
    .navbar.center .navbar-inner .nav {
        display:inline-block;
        float: none;
        vertical-align: top;
    }
    

    Demo

    0 讨论(0)
  • 2020-12-03 05:50

    You can use

    text-align:center;
    

    to a wrapper element to center its childs, but only if the childs have

    display: inline;
    /* or */
    display: inline-block;
    

    Another option, if you know the width of the element you want to center, is

    display: block;
    width: /* something */;
    margin: 0 auto;
    
    0 讨论(0)
提交回复
热议问题