As you see in the picture below, when I click on the bell icon, a dropdown menu appears at the bottom-right of the icon. I want this dropdown menu to appear at bottom-left instead of bottom-right. What should I do?
If you want to see my code, it's written with php
:
function navigation(){
$output = "";
$icons = ["user","bell","envelope","commenting"];
$rows = [2,5,5,5];
for ($i=0; $i < count($icons) ; $i++) {
$output .= '<div class="dropdown">';
$output .= '<a class="nav-item nav-link" data-toggle="dropdown">';
$output .= '<i class="fa fa-'.$icons[$i].'"></i></a>';
$output .= '<div class="dropdown-menu text-right">';
for ($j=0; $j < $rows[$i] ; $j++) {
$output .= '<a href="#" class="dropdown-item">'.($j+1).'</a>';
}
$output .= '</div>';
$output .= '</div>';
}
return $output;
}
And then, this output will be echoed in an html file:
<nav class="navbar">
<div class="container">
<div class="navbar-nav d-flex flex-row">
<?= navigation() ?>
</div>
</div>
</nav>
Use the
dropdown-menu-right
class to align the items inside the menu right..http://codeply.com/go/6Mf9aK0P8G
Update for Bootstrap4-Beta:
Because there is a bug in bootstrap4 beta i had to add
to make it work.
I encountered the same problem with an additional difficulty because my menu is created in PHP - the number of elements and dropdown content is not fixed.
Here is a solution that centers dropdowns when possible, otherwise align them on the left or right to prevent them from exceeding the viewport:
https://codepen.io/migli/pen/RELPPj
.dropdown-menu-right
class has a different behaviour if it's inside a.navbar
. You can read the "Heads up" in the docs here:https://getbootstrap.com/docs/4.0/components/dropdowns/#menu-alignment
Actually to solve I use this class: