Center content in responsive bootstrap navbar

后端 未结 11 2011
自闭症患者
自闭症患者 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:59

    Update 2020...

    Bootstrap 4

    Centering Navbar content is easier is Bootstrap 4, and you can see many centering scenarios explained here: https://stackoverflow.com/a/20362024/171456

    Bootstrap 3

    Another scenario that doesn't seem to have been answered yet is centering both the brand and navbar links. Here's a solution..

    .navbar .navbar-header,
    .navbar-collapse {
        float:none;
        display:inline-block;
        vertical-align: top;
    }
    
    @media (max-width: 768px) {
        .navbar-collapse  {
            display: block;
        }
    }
    

    http://codeply.com/go/1lrdvNH9GI

    Also see: Bootstrap NavBar with left, center or right aligned items

    0 讨论(0)
  • 2020-11-22 09:00

    I think this is what you are looking for. You need to remove the float: left from the inner nav to center it and make it a inline-block.

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

    http://jsfiddle.net/bdd9U/2/

    Edit: if you only want this effect to happen when the nav isn't collapsed surround it in the appropriate media query.

    @media (min-width: 768px) {
        .navbar .navbar-nav {
            display: inline-block;
            float: none;
            vertical-align: top;
        }
    
        .navbar .navbar-collapse {
            text-align: center;
        }
    }
    

    http://jsfiddle.net/bdd9U/3/

    0 讨论(0)
  • 2020-11-22 09:00

    For Bootstrap 4:

    Just use

    <ul class="navbar-nav mx-auto">
    

    mx-auto will do the job

    0 讨论(0)
  • 2020-11-22 09:04

    The original post was asking how to center the collapsed navbar. To center elements on the normal navbar, try this:

    .navbar-nav {
        float:none;
        margin:0 auto;
        display: block;
        text-align: center;
    }
    
    .navbar-nav > li {
        display: inline-block;
        float:none;
    }
    
    0 讨论(0)
  • 2020-11-22 09:14

    This code worked for me

    .navbar .navbar-nav {
        display: inline-block;
        float: none;
    }
    .navbar .navbar-collapse {
        text-align: center;
    }
    
    0 讨论(0)
提交回复
热议问题