.alert {
position: relative;
padding: 1.2rem 2rem;
background-color: rgba(0, 0, 0, 0.01);
border: 1px solid rgba(0, 0, 0, 0.1);
border-radius: var(--border-radius);
}
.alert.hidden {
animation:1s fadeMe forwards;
}
@keyframes fadeMe {
0% { opacity:1; }
99% { padding: 1.2rem 2rem; height:auto; }
100% { opacity:0; height:0px; padding:0; }
}
<div class="alert">
This is an alert box.
<button onclick="this.parentNode.classList.add('hidden')">Close</button>
</div>
<div class="alert">
Another one
<button onclick="this.parentNode.classList.add('hidden')">Close</button>
</div>
至于页面加载时的淡入,使用css进行淡入是很棘手的,因为一旦加载css,动画就会播放页面是否已完全加载。您可以设置动画延迟,但在开始制作动画之前,您确实需要javacscript来确保一切就绪。