Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

Left and Right nav items

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 last two elements are still on the right

The code snippet is here.

like image 381
loretoparisi Avatar asked Jul 08 '15 21:07

loretoparisi


People also ask

How do I put two navbar collapse content in the same line?

Wrap both #nabar-mid-collapse and #navbar-rt-collapse in a div with class row-fluid , and apply class col-xs-4 (*or any respective . col class according to the width you need for each item *) to both of them. There is no row-fluid in Bootstrap 3.

How do I make bootstrap navbar left to right?

ml-auto class of Bootstrap 4 to align navbar items to the right. The . ml-auto class automatically gives a left margin and shifts navbar items to the right.

How do you collapse a collapsed navbar?

To create a collapsible navigation bar, use a button with class="navbar-toggler", data-toggle="collapse" and data-target="#thetarget" . Then wrap the navbar content (links, etc) inside a div element with class="collapse navbar-collapse" , followed by an id that matches the data-target of the button: "thetarget".


2 Answers

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.

like image 68
Daniel Waghorn Avatar answered Oct 07 '22 23:10

Daniel Waghorn


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:

Left aligned right text

Bootply

like image 45
m4n0 Avatar answered Oct 07 '22 21:10

m4n0