CSS animations do not work with display
. If you want the paragraph to be hidden after 1 second, you could even use a combination of visibility: hidden
with Javascript, where Javascript would detect the end of the animation to apply a display: none
in the element:
document.addEventListener("DOMContentLoaded", function(){
const eventos = ["animationend", "webkitAnimationEnd", "oAnimationEnd", "MSAnimationEnd"];
eventos.forEach(e=>{
document.getElementById("text").addEventListener(e, function(){
this.style.display = "none";
});
});
});
#text {
animation: anima1 1s linear forwards;
}
@keyframes anima1 {
100% {
visibility: hidden;
}
}
<p id="text">Text</p>
Qualquer coisa aqui
The visibility: hidden
makes the element invisible, but it still takes up space on the page. Javascript detects when the CSS animation ends and applies the display: none
.
However, if you are going to use Javascript, you do not need to use CSS and Javascript animation at the same time. Just use simpler Javascript with setTimeout
:
document.addEventListener("DOMContentLoaded", function(){
setTimeout(function(){
document.getElementById("text").style.display = "none";
}, 1000);
});
<p id="text">Text</p>
Qualquer coisa aqui
Where has the value 1000
is equal to 1 second. The time value of the setTimeout
is in milliseconds. Then you just multiply the seconds you want by 1000 (e.g.: 5 seconds = 5000).
Now, if you want a transition effect, you could use opacity
in place of visibility
:
document.addEventListener("DOMContentLoaded", function(){
const eventos = ["animationend", "webkitAnimationEnd", "oAnimationEnd", "MSAnimationEnd"];
eventos.forEach(e=>{
document.getElementById("text").addEventListener(e, function(){
this.style.display = "none";
});
});
});
#text {
animation: anima1 1s linear forwards;
}
@keyframes anima1 {
100% {
opacity: 0;
}
}
<p id="text">Text</p>
Qualquer coisa aqui