I'm currently designing a kind of CSS 'mega dropdown' menu - basically a normal CSS-only dropdown menu, but one that contains different types of content.
At the moment, it appears that CSS3 Transitions don't apply to the 'display' property, i.e. you can't do any sort of transition from display: none
to display: block
(or any combination).
Can anyone think of a way for the second-tier menu from the above example to 'fade in' when someone hovers over one of the top level menu items?
I'm aware that you can use transitions on the visibility:
property, but I can't think of a way to utilise that effectively.
I've also tried using height but that just failed miserably.
I'm also aware that it's trivial to achieve this using JavaScript, but I wanted to challenge myself to use just CSS and I think I'm coming up a little short.
All and any suggestions most welcome.
I finally found a solution for me, by combining
opacity
withposition absolute
(not to occupy space when hidden).I feel almost bad answering to a question with that many answers, but this solution has excellent compatibility and I haven't seen it yet:
Explanation: it uses the
visibility: hidden
trick (which is compatible with “show-and-animate” in one step) but uses the combinationposition: absolute; z-index: -1; pointer-events: none;
to make sure that the hidden container does not take space and does not answer to user interactions.Instead of callbacks, which don't exist in CSS, we can use
transition-delay
property.So, what's going on here?
When
visible
class is added, bothheight
andopacity
start animation without delay (0ms), thoughheight
takes 0ms to complete animation (equivalent ofdisplay: block
) andopacity
takes 600ms.When
visible
class is removed,opacity
starts animation (0ms delay, 400ms duration), and height waits 400ms and only then instantly (0ms) restores initial value (equivalent ofdisplay: none
in the animation callback).Note, this approach is better, than ones using
visibility
. In such case the element still occupies the space on the page, and it's not always suitable.For more examples please refer this article.
Taking from a few of these answers and some suggestions elsewhere, the following works great for hover menus (I'm using this with bootstrap 3, specifically):
You could also use
height
in place ofmax-height
if you specify both values sinceheight:auto
is not allowed withtransition
s. The hover value ofmax-height
needs to be greater than theheight
of the menu can possibly be.I think SalmanPK has the closest answer, it does fade an item in or out, with the following CSS animations. However the display property does not animate smoothly, only the opacity.
If you want to animate the element moving from display block to display none, I can't see that it is currently possible just with CSS, you have to get the height and use a CSS animation to decrease the height. This is possible with CSS as shown in the example below, but it would be tricky to know the exact height values you need to animate for an element.
jsFiddle example
CSS
JavaScript