ChatGPT解决这个技术问题 Extra ChatGPT

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:

https://i.stack.imgur.com/I7cor.png

How can i fix it?

The correct answer (for bootstrap >= v 3.2.0) to this question is resolved here: stackoverflow.com/questions/23654962/… by @cvrebert.
I believe your code is missing an end span tag:

s
sigurdo

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.

Deprecation Notice 2: As of Bootstrap v5, .dropdown-menu-right is deprecated. For Bootstrap v5, use .dropdown-menu-end.


That is a completely separate question
This answer does not actually fix the problem if user is using some smaller screen device -- it is going out of the left side in stead of right side (in fact that you just change the pull-left to the pull-right).
See krzychu answer as Bootstrap.pull-right is deprecated
@HristoEnev me either. Did your dropdowns go way off the button?
With Boostrap v5.0, it's .dropdown-menu-end instead of .dropdown-menu-right getbootstrap.com/docs/5.0/components/dropdowns/…
I
Ilia

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


Even with the latest bootstrap .pull-right works for me, and .dropdown-menu-right does nothing.
This does indeed work in Bootstrap 4, except you add the class .dropdown-menu-right to div.dropdown-menu (not ul)
<ul class="dropdown-menu dropdown-menu-right"> worked for me on 4.1.1
p
praj

For Bootstrap 4, adding dropdown-menu-right works. Here's a working example..

http://codeply.com/go/w2MJngNkDQ


I tried all the other answers, - and this one was the only one that worked for me as well. I'm using Bootstrap v. 4.1.0.
G
GiorgosK

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


this is the only solution that works for bootstrap 4.0.0
M
Mohan Dere

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; }


D
Dedy Setiawan

in Bootstrap 4 you can use .dropleft

just change to :

<span class="dropleft">

or

add .dropdown-menu-{lg,med,sm,xs}-right to your dropdown-menu

<div class="dropdown-menu dropdown-menu-sm-right" aria-labelledby="dropdown03">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
</div>

J
Jeremy Lynch

float-*-right for Bootstrap 4

* = xs, sm, md, lg


h
hackdotslashdotkill

I can't leave a comment because my SO level is too low, but I just made sure that the parent container is set to "overflow:hidden" like so (also make sure position is set).

<div style="overflow:hidden;position:relative">
    <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>
</div>

L
Leonardo Hermoso

If you are using Bootstrap 5, according to documentation:

Dropright: Trigger dropdown menus at the right of the elements by adding .dropend to the parent element.

<div class="btn-group dropend">
 <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  Dropright
 </button>
 <ul class="dropdown-menu">
  <!-- Dropdown menu links -->
 </ul>
</div>

J
JavaProScript

I encountered this issue recently, and I solved it by addind display="dynamic" to my ngbDropdown (using Angular 12, ngBootstrap 10 and Bootstrap 4):