Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap navbar float right moves div to new line

Hello I'm trying to create a section on the right of the navbar. I used the div class navbar-right which should float the div to the right, but I am getting a new line instead.

See below for code & thanks!

 <div class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <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>
          <a class="navbar-brand" href="index.php">xxx</a>
        </div>
        <div class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="index.php">Home</a></li>
            <li><a href="about.php">About</a></li>
            <li><a href="contact.php">Contact</a></li>
          </ul>
        </div>
      <div class="navbar-right">
      <p>
        <b> '.$username.' </b> | Access Level: '.$level.' | <a href=logout.php>Logout</a>
      </p> 


      </div>
    </div>
  </div>
like image 779
alchuang Avatar asked Sep 19 '13 16:09

alchuang


2 Answers

In order to be placed at the top level of nav-bar, the contents must be placed inside of a div with the class: navbar-header

So instead of declaring the logout link inside of the div:

<div class="navbar-right">

Use the following classes instead:

<div class="navbar-header pull-right">

Since you'll want the Brand to pull-left, you can place two navbar-header divs right next to each other at the very top and have one pull to each direction. Like this:

<div class="navbar-header pull-left">     
    <a class="navbar-brand" href="index.php">xxx</a>
</div>
<div class="navbar-header pull-right">
    <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>

    <p class="navbar-text">
        <b> '.$username.' </b>
        <a href="logout.php" class="navbar-link">Logout</a>&nbsp;
    </p>  
</div>

jsFiddle

Expanded

expanded

Collapsed

collapsed

Update: As per Ram's comment, in order to not have the pull down menu not overlap on a small screen, you'll have to clear the left float like this:

.navbar-collapse.in,
.navbar-collapse.collapsing {
    clear: left;
}
like image 115
KyleMit Avatar answered Oct 31 '22 14:10

KyleMit


Instead of,

<div class="navbar-right">
  <p>
    <b> '.$username.' </b> | Access Level: '.$level.' | <a href=logout.php>Logout</a>
  </p> 
</div>

Use,

<p class="pull-right">
    <b> '.$username.' </b> | Access Level: '.$level.' | <a href=logout.php>Logout</a>
</p> 
like image 1
Anshad Vattapoyil Avatar answered Oct 31 '22 13:10

Anshad Vattapoyil