How to justify navbar-nav in Bootstrap 3

前端 未结 7 466
栀梦
栀梦 2020-12-01 07:16

I\'m attempting to justify a navbar (make the navbar contents stretch) in Bootstrap 3. I\'ve added margin: 0 auto; max-width: 1000px; to the nav* c

相关标签:
7条回答
  • 2020-12-01 07:44
    <div class="navbar navbar-default navbar-fixed-top" role="navigation">
     <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li><a href="#">Home</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#contact">Contact</a></li>  
      </ul>
    </div>
    

    and for the css

    @media ( min-width: 768px ) { 
        .navbar > .container {
            text-align: center;
        }
        .navbar-header,.navbar-brand,.navbar .navbar-nav,.navbar .navbar-nav > li {
            float: none;
            display: inline-block;
        }
        .collapse.navbar-collapse {
            width: auto;
            clear: none;
        }
    }
    

    see it live http://www.bootply.com/103172

    0 讨论(0)
  • 2020-12-01 07:45

    I know this is an old post but I would like share my solution. I spent several hours trying to make a justified navigation menu. You do not really need to modify anything in bootstrap css. Just need to add the correct class in the html.

       <nav class="nav navbar-default navbar-fixed-top">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#collapsable-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#top">Brand Name</a>
            </div>
            <div class="collapse navbar-collapse" id="collapsable-1">
                <ul class="nav nav-justified">
                    <li><a href="#about-me">About Me</a></li>
                    <li><a href="#skills">Skills</a></li>
                    <li><a href="#projects">Projects</a></li>
                    <li><a href="#contact-me">Contact Me</a></li>
                </ul>
            </div>
        </nav>
    

    This CSS code will simply remove the navbar-brand class when the screen reaches 768px.

    media@(min-width: 768px){
       .navbar-brand{
            display: none;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 07:56

    To justify the bootstrap 3 navbar-nav justify menu to 100% width you can use this code:

    @media (min-width: 768px){
        .navbar-nav {
            margin: 0 auto;
            display: table;
            table-layout: auto;
            float: none;
            width: 100%;
        }
        .navbar-nav>li {
            display: table-cell;
            float: none;
            text-align: center;
        }
    } 
    
    0 讨论(0)
  • 2020-12-01 07:58

    It turns out that there is a float: left property by default on all navbar-nav>li elements, which is why they were all scrunching up to the left. Once I added the code below, it made the navbar both centered and not scrunched up.

    .navbar-nav>li {
            float: none;
    }
    

    Hope this helps someone else who's looking to center a navbar.

    0 讨论(0)
  • 2020-12-01 07:58

    Here is a more easy solution. just remove the "navbar-nav" class and add "nav-justified".

    0 讨论(0)
  • 2020-12-01 08:00

    Hi you can use this below code for working justified nav

    <div class="navbar navbar-inverse">
        <ul class="navbar-nav nav nav-justified">
            <li class="active"><a href="#">Inicio</a></li>
            <li><a href="#">Item 1</a></li>
            <li><a href="#">Item 2</a></li>
            <li><a href="#">Item 3</a></li>
            <li><a href="#">Item 4</a></li>
        </ul>
     </div>
    
    0 讨论(0)
提交回复
热议问题