I've been trying to add animation to a right-aligned Bootstrap dropdown using CSS, but I'm having trouble getting it to work. I attempted setting the transition
property to target the element's width
, but it doesn't seem to be effective.
Upon inspecting the element in my browser, I noticed that the width is initially set to 0 and changes to "auto" once the menu becomes visible. Manually adjusting the width shows that the transition works, but it doesn't seem to happen when the menu is displayed. I even tried using specific pixel values instead of "auto", but nothing changed.
My goal is to have the menu smoothly slide out to the right while the menu button rotates. Is this achievable?
div.dropdown > .btn {
transition: transform 0.8s cubic-bezier(0,-0.14,.27,1.55);
transform: rotate(90deg);
}
div.dropdown > .btn.show {
transform: rotate(270deg);
}
div.dropdown > .dropdown-menu {
transition: width 0.8s cubic-bezier(0,-0.14,.27,1.55);
width: 0;
}
div.dropdown > .btn.show + .dropdown-menu {
width: auto;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="45272a2a31363137243505706b755c6f647e667649">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea8885859e999e988b9aaadfc4dac4dbd3fab7cb">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<div class="dropdown dropend p-4">
<button class="btn btn-secondary" type="button" data-bs-toggle="dropdown">
++
</button>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
</ul>
</div>