简体   繁体   中英

Bootstrap 3 Navbar Collapse menu at Left for left and right navbar items

I have a Bootstrap 3 navbar with left nav items and right nav items (as shown below).

左右导航项目

When collapsed, I want both the navbar-toggle (AKA the 'hamburger menu') and its items to be left aligned.

My code so far:

<nav class="navbar navbar-default custom-navbar" role="navigation">
  <div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </button>    
  </div>
  <div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li><a href="#">Left</a></li>
        <li><a href="#about">Left</a></li>
    </ul>
    <ul class="nav navbar-nav pull-right">
      <li><a href="#about">Right</a></li>
      <li><a href="#contact">Right</a></li>
    </ul>
  </div>
</nav>

The CSS to have the navbar-toggle on the left is:

@media (max-width:767px) {
    .custom-navbar .navbar-right {
        float: right;
        padding-right: 15px;
    }
    .custom-navbar .nav.navbar-nav.navbar-right li {
        float: left;
    }
    .custom-navbar .nav.navbar-nav.navbar-right li > a {
        padding:8px 5px;
    }
    .custom-navbar .navbar-toggle {
        float: left;
        margin-right: 0
    }
    .custom-navbar .navbar-header {
        float: left;
        width: auto!important;
    }
    .custom-navbar .navbar-collapse {
        clear: both;
        float: none;
    }
}

I have added a "pull-right" style to have the items aligned, with no success.

The "navbar-right" works badly. In fact, with it, I will have both right-hand items on the same row. Using "pull-right" they work as single rows, but still stay on the right.

This leaves me with:

  • The hamburger menu on the left (as desired),
  • The navbar-left items on the left (as desired),
  • The navbar right items are still on the right (I want this to be fixed!).

The final result:

最后两个元素仍然在右边

The code snippet is here .

I've made a few adjustments to your CSS and markup which you can view here .

One thing I'd point out is that pull-right and the other pull and push classes are for rearranging grid columns.

You can use the !important rule to override the default behaviour of pull-right.

@media (max-width:767px) {
 .nav.navbar-nav.pull-right {
   float: left !important;
 }
} 

Output in <768px devices:

左对齐右文本

Bootply

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM