Basically, I have two drop down buttons in a nav bar. Currently, when I hover the left button, the right button moves instantly to the right to let space for the drop down to appear. What I want is to let it slides smoothly to the right, not instantaneously.
I tried to apply "transition: 1s" but it requires a start and end value, and since the width property don’t change, the only thing changing is the display property (that hides the menu). How should I do it? I don’t want to use transform property… Have a nice day 🙂
nav {
display: flex;
gap: 20px;
}
.nav_item>button {
border: none;
}
.dropdown_item {
text-decoration: none;
color: black;
}
.dropdown_content {
display: flex;
flex-direction: column;
}
.hidden {
display: none;
}
.nav_item:hover .hidden {
display: block;
}
<nav>
<div class="nav_item">
<button>File</button>
<div class="hidden">
<div class="dropdown_content">
<a href="#" class="dropdown_item">Save map</a>
<a href="#" class="dropdown_item">Load new map</a>
<a href="#" class="dropdown_item">Another</a>
</div>
</div>
</div>
<div class="nav_item">
<button>File</button>
<div class="hidden">
<div class="dropdown_content">
<a href="#" class="dropdown_item">Save map</a>
<a href="#" class="dropdown_item">Load new map</a>
<a href="#" class="dropdown_item">Another</a>
</div>
</div>
</div>
</nav>
2
Answers
It’s not advised to try and animate the display property, but that being said, there are some clever people out there and some hacks you can do:
The above was inspired by CSS Tricks post about animating display properties.
I believe this achieves the desired effect from your initial question.
That being said, I’d suggest taking a far simpler (and cleaner) route by simply adding
position: absolute
into the.dropdown_content
class:This removes all width dependancy from the equation and makes the user experience more predictable.
I hope this helps.