Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are CSS3 transitions disabled when element is hidden?

I have noticed that (using jQuery in Chrome 43) transitions are disabled when the element has display: none. Is this standarized behavior on all browsers, a feature of jQuery, or is it something that cannot be relied on in production?

The transition is enabled when the CSS statements to be animated are changed in a deferred function. Take a look at this JSFiddle. Uncomment line 3 or 6 to see it for yourself.


SOLUTION:

This behavior cannot be relied upon in production as it seems to be a product of optimization/design choices rather than specification (as per @Andriy Horens answer). Instead you should turn on and off the animation (transition-property: none) with a class. Failing to use a class rendered it unreliable for me in Chrome 43. Chrome did also require separate frames (defer the code with a timeout of 0) to update some CSS statements. Just defer anything related to animations if you want to save development time.

like image 314
Friend of Kim Avatar asked Jun 29 '15 20:06

Friend of Kim


People also ask

Can you add transition to display none?

When we want to use transition for display:none to display:block, transition properties do not work. The reason for this is, display:none property is used for removing block and display:block property is used for displaying block. A block cannot be partly displayed.

What triggers CSS transition?

Triggering transitions You can trigger CSS transitions directly with pseudo classes like :hover (activates when the mouse goes over an element), :focus (activates when a user tabs onto an element, or when a user clicks into an input element), or :active (activates when user clicks on the element).

Is visibility Animatable CSS?

Visibility is an animatable property according to the spec, but transitions on visibility do not work gradually, as one might expect. Instead transitions on visibility delay hiding an element.

Does transition only work with hover?

But transitions are not just limited to use with :hover . You can animate CSS properties, thus use CSS transitions without hover. This is done via transitions using some other CSS techniques, a number of which I've outlined below. I've also included a demo for each example.


1 Answers

According to MDN:

Display

In addition to the many different display box types, the value none lets you turn off the display of an element; when you use none, all descendant elements also have their display turned off. The document is rendered as though the element doesn't exist in the document tree.

So i think elements with display set to none will not be rendered at all across all browsers and therefore transition or any other visual effect won't be applied.

You can also test yourself by subscribing to transitionend event:

$(element).on("transitionend", function () {
     console.log("transition ended");
});

Update:

It is also per w3c specification:

And some values (such as display:none, display: contents, and box-suppress: discard) cause the element and/or its descendants to not generate any boxes at all.

Where boxes are visual representations of element. And transition is definitely a part of visual representation as it also can affect layout e.g. when you change relative position of element with transition applied.


Here is one more example of how different are animations of elements with display : none and visibility : hidden in other words of rendered element and not-rendered one.

JSFiddle DEMO

like image 93
Andriy Horen Avatar answered Oct 29 '22 21:10

Andriy Horen