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 ran into this today, with a
position: fixed
modal that I was reusing. I couldn't keep itdisplay: none
and then animate it, as it just jumped into appearance, and andz-index
(negative values, etc) did weird things as well.I was also using a
height: 0
toheight: 100%
, but it only worked when the modal appeared. This is the same as if you usedleft: -100%
or something.Then it struck me that there was a simple answer. Et voila:
First, your hidden modal. Notice the
height
is0
, and check out theheight
declaration in transitions... it has a500ms
, which is longer than myopacity
transition. Remember, this affects the out-going fade-out transition: returning the modal to its default state.Second, your visible modal. Say you're setting a
.modal-active
to thebody
. Now theheight
is100%
, and my transition has also changed. I want theheight
to be instantly changed, and theopacity
to take300ms
.That's it, it works like a charm.
I suspect anyone just starting CSS transitions quickly discovers that they don't work if you're modifying the display property (block/none) at the same time. One work-around that hasn't yet been mentioned is that you can continue to use display:block/none to hide/show the element, but set its opacity to 0 so that even when it's display:block, it's still invisible. Then to fade it in, add another CSS class such as "on" which sets the opacity to 1 and defines the transition for opacity. As you may have imagined, you'll have to use JavaScript to add that "on" class to the element, but at least you're still using CSS for the actual transition.
P.S. If you find yourself in a situation where you need to do both display:block, and add class "on", at the same time, defer the latter using setTimeout. Otherwise the browser just sees both things as happening at once and disables the transition.
you can also use this:
Change
overflow:hidden
tooverflow:visible
. It works better. I use like this:visible
is better becauseoverflow:hidden
act exactly like adisplay:none
.It can be handle by using transition timing functions
step-end
andstep-start
For example: https://jsfiddle.net/y72h8Lky/
I started an open source skeleton project called Toggle Display Animate
https://marcnewton.github.io/Toggle-Display-Animate/
This skeleton helper will allow you to easily mimic jQuery show/hide but with in/out CSS3 transition animations.
It uses class toggles so you can use any css methods you want on elements besides display:none|block|table|inline etc as well as other alternate uses that can be thought up.
Its main design purpose is for element toggle states, it supports a revert state where hiding the object allows you to run your keyframe in reverse or play an alternate animation for hiding the element.
Most of the markup for the concept I am working on is CSS, there is very little javascript actually used.
There is a demo here: http://marcnewton.co.uk/projects/toggle-display-animate/