简体   繁体   中英

Twitter Bootstrap navbar right allign link

Helo,

New to bootstrap and trying to use it with rails. I am trying to create a navbar and trying to display a link to the extreme right of the navbar which is not working. Here is my code:

   <div class="navbar navbar-fixed-top">
   <div class="navbar-inner">  
   <div class="container">  

    <ul class="nav">  
       <li class="active">  
         <%= link_to "Rubyblog", root_path, :class => 'brand' %>
       </li>  

       <li>
         <a href="#">Edit Account</a>
       </li>

       <li>
         <a href="#">Categories</a>
       </li>

        <ul class = "nav pull-right">
          <a href="#">Current User</a>
        </ul>

    </ul> 

</div>  


I am trying to display the last link "Current User" to the extreme right of the navbar. Using div class"nav pull-right" doesn't seem to work: http://i.imgur.com/BEBcaIc.png

Please let me know what I am missing/doing wrong. Appreciate your inputs.

Thanks! Mike

EDIT: Answering my question:

Pulled the "nav pull-right" out of the main "nav" class tag and it worked. Updated code:

 <div class="navbar navbar-fixed-top">
 <div class="navbar-inner">  
 <div class="container">  

<ul class="nav">  
   <li class="active">  
     <%= link_to "Rubyblog", root_path, :class => 'brand' %>
   </li> 

   <% if user_signed_in? %>

      <li>
        <%= link_to 'Edit Account', edit_user_registration_path %>
      </li>

      <li>
        <%= link_to 'Categories', categories_path %>
      </li>

      <% if current_user.has_role? :admin %>
         <li>
           <%= link_to 'Users', users_path %>
         </li>
      <%end%>
   <%else%>
      <li>
        <%= link_to 'Sign up', new_user_registration_path %>
      </li>
   <% end %>
</ul> 

   <ul class = "nav pull-right">
      <li><%= link_to 'Welcome, '+current_user.name, edit_user_registration_path %></li>
   </ul>

EDIT: Answering my question:

Pulled the "nav pull-right" out of the main "nav" class tag and it worked. Updated code in the EDIT section of my question

Have you tried making your current user like the following:

<li class="pull-right">
    <a href="#">Current User</a>
</li>

I'm pretty sure the reason it's losing it's styling is because you are nesting nav inside a nav .

I think you want to divide the navbar into navbar-left and navbar-right :

<nav class="navbar navbar-fixed-top" role="navigation">
  <!-- Brand and toggle get grouped for better mobile display -->
  <div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
      <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="#">Brand</a>
  </div>

  <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <div class="navbar-inner">  

      <ul class="nav navbar-nav">  
        <li class="active">  
        <%= link_to "Rubyblog", root_path, :class => 'brand' %>
        </li>

        <li>
        <a href="#">Edit Account</a>
        </li>

        <li>
        <a href="#">Categories</a>
        </li>
      </ul>

      <ul class = "nav navbar-nav navbar-right">
        <a href="#">Current User</a>
      </ul>

    </div> 
  </div>
</nav>

The bootstrap docs talk about this and say they're a bit different than pull-right - this might explain why you're seeing issues. I haven't tested the above, but it's likely a good start.

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