Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4: Dropdown menu is not right-aligned when inside nav.navbar

Latest Boostrap 4 and popper.js as of time of writing are loaded from CDN.

Codepen here.

Problem.

My dropdown code:

<div class="btn-group">
    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">This dropdown's menu is right-aligned</button>
    <div class="dropdown-menu dropdown-menu-right">
        <button class="dropdown-item" type="button">Action</button>
        <button class="dropdown-item" type="button">Another action</button>
        <button class="dropdown-item" type="button">Something else here</button>
    </div>
</div>

I am positioning it inside <nav class="navbar"> and it is flushed right:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <div class="btn-group ml-auto">
        <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Welcome</button>
        <div class="dropdown-menu dropdown-menu-right">
            <button class="dropdown-item" type="button">Action</button>
            <button class="dropdown-item" type="button">Another action</button>
            <button class="dropdown-item" type="button">Something else here</button>
        </div>
    </div>
  </div>
</nav>

The problem is that when you click "Welcome", the drop-down goes outside the right edge of the screen, despite dropdown-menu-right class on it.

Solution proposed here does not work ({margin:0} does not help).

Question.

How do I make sure that drop down does not go outside the right edge of the screen?

Thanks!

like image 680
temuri Avatar asked Oct 09 '17 16:10

temuri


People also ask

How do I move a dropdown to the right in bootstrap?

By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. Add . dropdown-menu-right to a . dropdown-menu to right align the dropdown menu.

How do I align navbar items to the right in bootstrap 4?

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 Align drop down menu in bootstrap Center?

Dropdown button can be positioned in the center of the page by setting the “text-align” property of dropdown div to center. The following example contains a simple Bootstrap dropdown menu with an added class “my-menu”. The property “text-align: center” is added to the class.

How do I move a drop down menu to the left in bootstrap?

To override it, use . dropdown-menu-left.


1 Answers

I have not worked that one either. However, a workaround that I'm using and so far fixes the problem everywhere is:

.dropdown-menu-right {
    right:0;
    left:auto;
}
like image 174
StrayObject Avatar answered Oct 21 '22 17:10

StrayObject