27
loading...
This website collects cookies to deliver better user experience
@keyframes
CSS rule and set the CSS property called visibility
to value hidden
for that particular element in CSS.<div id="hideMeAfter5Seconds">Hello World!</div>
#hideMeAfter5Seconds {
animation: hideAnimation 0s ease-in 5s;
animation-fill-mode: forwards;
}
@keyframes hideAnimation {
to {
visibility: hidden;
width: 0;
height: 0;
}
}
div
with some content Hello world!
inside it which we need to hide after 5 seconds.div
and also set an id
attribute to it so we can reference it later in the CSS. It can be done like this,<div id="hideMeAfter5Seconds">Hello World!</div>
hideMeAfter5Seconds
div id like this,#hideMeAfter5Seconds {
// css magic here!
}
animation
css property and define 4 properties like this,#hideMeAfter5Seconds {
animation: hideAnimation 0s ease-in 5s;
}
hideAnimation
, 0s
, ease-in
, and 5s
.The first one, hideAnimation
is the name of the animation which we need to declare using the @keyframes
rule. This is where we will define the auto-hiding logic for the div
HTML element.
The second one is the animation duration which we have set to 0s
because we don't need the animation to run for any amount of seconds. We just need to execute some properties immediately in the hideAnimation
animation.
The third one is the animation timing function, which defines the speed curve of the animation. In our case, we have set it to be ease-in
.
The fourth one is the delay of the animation which we have set to 5s
so that animation waits for 5s
then executes it.
animation-fill-mode
property and set its value to forwards
like this,#hideMeAfter5Seconds {
animation: hideAnimation 0s ease-in 5s;
animation-fill-mode: forwards;
}
hideAnimation
animation itself using the @keyframes
css rule.#hideMeAfter5Seconds {
animation: hideAnimation 0s ease-in 5s;
animation-fill-mode: forwards;
}
@keyframes hideAnimation {
// animation behavior
}
hideAnimation
, we can use the the to
value or block and set the css visibiltiy
property to hidden
like this,#hideMeAfter5Seconds {
animation: hideAnimation 0s ease-in 5s;
animation-fill-mode: forwards;
}
@keyframes hideAnimation {
to {
visibility: hidden;
}
}
animation
property and waits for 5s to execute the hideAnimation
animation.0s
, the to
block is immediately executed after waiting for 5 seconds, and the visibility:hidden
CSS property gets applied to the div
element which hides the div
.visibility:hidden
only sets the visible state of the div
element and doesn't remove the entire div
from the visual flow. To remove it we can also set its CSS width
and the height
property to 0
like this,#hideMeAfter5Seconds {
animation: hideAnimation 0s ease-in 5s;
animation-fill-mode: forwards;
}
@keyframes hideAnimation {
to {
visibility: hidden;
width: 0;
height: 0;
}
}
display
CSS property and set its value to none
. This is because we cannot animate the display
property and thus it will not work inside the to
block.