Twitter bootstrap dropdown goes outside the screen
I want to implement twitter bootstrap dropdown menu, here's my code:
<span class="dropdown">
<a href="#menu1" class="dropdown-toggle" data-toggle="dropdown" ><img class="left" src="/static/img/topmenu_preferences.png" /><b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">a</a></li>
<li><a href="#">b</a></li>
<li><a href="#">c</a></li>
<li class="divider"></li>
<li><a href="#">d</a></li>
</ul>
Dropdown works good, my dropdown is placed next to right edge of the screen and when I click my dropdown-toggle, the list goes outside the screen. It looks like on the screen:
How can i fix it?
Solution 1:
adding .pull-right
to ul.dropdown-menu
should do it
Deprecation Notice: As of Bootstrap v3.1.0,
.pull-right
on dropdown menus is deprecated. To right-align a menu, use.dropdown-menu-right
.
Solution 2:
Since Bootstrap v3.1.0 adding .pull-right
is deprecated on dropdown menus. A .dropdown-menu-right
should be added to ul.dropdown-menu
instead. Docs
Solution 3:
For Bootstrap 4, adding dropdown-menu-right
works. Here's a working example..
http://codeply.com/go/w2MJngNkDQ
Solution 4:
a solution that does not need modifying the HTML only the CSS is
li.dropdown:last-child .dropdown-menu {
right: 0;
left: auto;
}
Its particularly usefull for dynamically generated menus where its not always possible to add the recommended class dropdown-menu-right
to the last element
Solution 5:
You can use class .dropdown-pull-right
with following css:
.dropdown-pull-right {
float: right !important;
right: 0;
left: auto;
}
.dropdown-pull-right>.dropdown-menu {
right: 0;
left: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<div class="dropdown dropdown-pull-right btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
Open Dropdown
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>