Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display none breaks animation

i want a certain div to be shown in certain circumstances, and i want it to be animated, at the start it should be invisible so...

.fixedNav{
    background-color: rgba(28,20,13, 0.75);
    position:fixed;
    color:white;
    width:60vw;
    margin-top:-50px !important;
    z-index: 1;
    display:none;
    opacity:0;
    transition:opacity .3s ease-in;
}

and then i animate the opacity with css by adding this class, with jquery, to it:

.fixedNavActive{
    display: block;
    opacity:1;
}

and it all works fine if i remove the display: none; from the main class, if i leave it there then the opacity isn't animated, it just appears, why does it break it and how can i make it animate the opacity instead of toggling it to 1?

like image 549
user2209644 Avatar asked May 20 '14 14:05

user2209644


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.

Can display be animated in CSS?

You can't use the display property in CSS animations either.

What is opposite of display none?

display: none doesn't have a literal opposite like visibility:hidden does. The visibility property decides whether an element is visible or not. It therefore has two states ( visible and hidden ), which are opposite to each other.

How do you make a div appear slowly CSS?

$("div"). animate({ opacity:0 },"slow"); This is useful if you also want to animate other properties of the element at the same time.


1 Answers

Basically, you cannot animate display properties, and even though you're only trying to animate the opacity, that transition fails because when the transition begins, the element doesn't exist. It has been removed from the flow due to the display: none property.

You can achieve the effect you're looking for by using visibility: hidden and visibility: visible, which is an animatable property, and if you need to effectively remove the element from the flow when it lacks visibility, then you can apply a max-height of 1px to the element and a margin-top of -1px.

like image 77
Gray Spectrum Avatar answered Oct 02 '22 02:10

Gray Spectrum