Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap Navigation Bar - Right Align Dropdown Menu

I'm using the Twitter Bootstrap (v2.1.1) with a PHP site. I'm generating the navigation bar dynamically in a php script as the navigation bar will have different content if the user is logged in or out of the site.

I would like to align the last dropdown menu to the right of the screen but haven't been able to so far. Here's a jsFiddle showing a simplified version:

http://jsfiddle.net/fmdataweb/AUgEA/

I would like the Menu 2 drop down to be right aligned. The code the for last dropdown is the same as for other dropdowns:

<li class="dropdown pull-right"> <a href="properties.php?type=showall" class="dropdown-toggle" data-toggle="dropdown">Menu 2<b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="propertiesSearch.php">Logout</a></li> </ul> </li>           

I've tried chaging it to:

<li class="dropdown pull-right"> 

but that makes no difference. Anyone know how to pull the dropdown menu to the right like you can with forms and <p> text?

like image 873
user982124 Avatar asked Sep 17 '12 01:09

user982124


People also ask

How do I align a drop down menu to the right?

Use the w3-right class to float the dropdown to the right, and use CSS to position the dropdown content (right:0 will make the dropdown menu go from right to left).

How do I align nav bar content to the right?

ml-auto class in Bootstrap can be used to align navbar items to the right. The . ml-auto class automatically aligns elements to the right.

How do I change the position of a drop down menu in Bootstrap?

Use data-offset or data-reference to change the location of the dropdown.

How do I add a drop down menu to the Bootstrap 4 navigation bar?

Example Explained. Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.


2 Answers

You need to change the .pull-right class to ul element instead of li.

HTML

<ul class="nav pull-right">   <li class="dropdown">     <a href="properties.php?type=showall" class="dropdown-toggle" data-toggle="dropdown">       Menu 2       <b class="caret"></b>     </a>     <ul class="dropdown-menu">       <li><a href="propertiesSearch.php">Logout</a></li>     </ul>   </li> </ul> 

Version 2.1.1

I rewrote your code including only necessary JavaScript plugins (bootstrap-dropdown.js), the latest version of Twitter Bootstrap (2.1.1) and support for responsive design.

http://jsfiddle.net/caio/gvw7j/

If you see the responsive menu in the above link, you can see the "wide result" in this link:

http://jsfiddle.net/caio/gvw7j/embedded/result/

.pull-right is defined by:

.pull-right {   float: right !important; } 

Version 3.1.1

There were no changes in this class. You might see the helpers classes section.

However, the documentation recommends using the class .navbar-right because it has some specific optimizations, unlike the pull-right class.

like image 76
Caio Tarifa Avatar answered Sep 28 '22 05:09

Caio Tarifa


If you came here trying to right-align a regular bootstrap .dropdown-menu and NOT a .nav, and are using Bootstrap 3, the correct class to add is .dropdown-menu-right

Here's the bootstrap documentation:

<ul class="dropdown-menu dropdown-menu-right">   ... </ul> 
like image 42
jbmilgrom Avatar answered Sep 28 '22 04:09

jbmilgrom