How can I center Twitter-Bootstrap 3 navbar links?

前端 未结 2 1462
别那么骄傲
别那么骄傲 2020-12-06 01:56

How can I center the buttons in the jsFiddle I set up so that the buttons are equally spaced and centered within and through-out the navbar?

http://jsfiddle.net/3GQy

相关标签:
2条回答
  • 2020-12-06 02:31

    Add the style

    .nav{
        text-align: center;
    }
    

    This will center the text in li.

    Check Fiddle

    These styles should be sufficient. You were trying to apply styles to to the wrong element.

    // This is being applied by the bootstrap
    // Set it to 25px instead of the default 15px
    .navbar{
        padding : 0 25px;
    }
    
    // Gave a width of 110px for each li
    // as the container is following a fixed width format
    li{
        width:110px;
    }
    
    0 讨论(0)
  • 2020-12-06 02:51

    Bootstrap 3 has a nav-justified class that can be used on nav. No extra CSS required:

    <div class="container">
      <h3 class="text-muted">Project name</h3>
      <ul class="nav nav-justified">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">Projects</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Downloads</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </div>
    

    Demo: http://bootply.com/72519


    Based on the comments, to have full-width centered links using the navbar-nav class, use flexbox...

    .navbar-center {
        width:100%;
        display: flex;
    }
    .navbar-center>li {
        flex:1 1 auto;
    }
    
    <div class="navbar navbar-default">
        <div class="container">
            <ul class="nav navbar-nav navbar-center text-center">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">Link</a></li>
                <li><a href="#">Link</a></li>
                <li><a href="#">More</a></li>
                <li><a href="#">Options</a></li>
            </ul>
        </div>
    </div>
    

    https://www.codeply.com/go/6ZE3obnpuP


    Also see
    Bootstrap NavBar with left, center or right aligned items
    Center Navbar in Bootstrap

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