David Fenk David Fenk - 2 years ago 53
CSS Question

How can I make this dropdown menu appear on the same line as its parent?

I'm trying to build a vertical menu with bootstrap4beta and

dropdown-menu
. I want the dropdown to appear on the right. But with
dropdown-menu-right
I can only make it appear in the next line and not on the same. How to do that?



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>

<nav class="nav flex-column col-4 col-sm-4 col-md-4 col-lg-4 col-xl-4">
<a class="nav-link active backgroundnav" href="#">HTML</a>
<a class="nav-link backgroundnav" href="#">CSS</a>
<a class="nav-link backgroundnav" href="#">JavaScript</a>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle backgroundnav" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
Preview
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="Preview">
<a class="dropdown-item" href="#">Mobile Phone</a>
<a class="dropdown-item" href="#">Tablet</a>
<a class="dropdown-item" href="#">Laptop</a>
<a class="dropdown-item" href="#">Desktop</a>
</div>
</li>
</nav>





Thanks.

Answer Source

Bootstrap 4 popper.js is positioning the dropdown so it's not easy to customize the positioning. You could override with some CSS like this, but the tricky part is figuring the "65%" value which is based on the width of the dropdown link.

.dropdown-menu-side {
    transform: translateX(65%) !important;
}

https://www.codeply.com/go/BI2Gs7viID

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download