Jon, I’m on the case. The first thing I’ve done is actually to simplify the HTML:
- You don’t need the first (outer) <div> as the <body> element can do that job.
- I’ve got rid of all the IDs and CLASSes by changing the “text_container” <div> to a <main> element.
- I’ve created a new CSS variable “–N” that has as its value the ordinal (1, 2, …,11) of the <div>. I do this by using the “div:nth-child(N)” css construct.
- I now have only 1 line to specify all the “animation” properties (using var(–N)).
The new HTML file is here:
<!DOCTYPE html>
<html>
<head>
<title>Subtitles Fade in and out</title>
<style>
html { width: 100%; height: 100%; }
body {
height : 100%;
width : 100%;
margin : 0;
padding : 0;
display : flex;
align-items : flex-end;
justify-content : center;
background-color: #808080;
overflow : hidden;
opacity : 1;
}
main {
display : flex;
align-items : center;
justify-content : center;
height : 200px;
width : 100%;
overflow : hidden;
background-color:transparent;
}
div {
position : absolute;
height : 90px;
width : 100%;
margin-top : 0;
text-align : center;
font-family : arial;
color : white;
font-size : 60px;
background-color: transparent;
text-shadow : 4px 4px black;
opacity :0;
--length: 4s;
--L : -0.5s;
}
div:nth-child( 1) {--N: 1;}
div:nth-child( 2) {--N: 2;}
div:nth-child( 3) {--N: 3;}
div:nth-child( 4) {--N: 4;}
div:nth-child( 5) {--N: 5;}
div:nth-child( 6) {--N: 6;}
div:nth-child( 7) {--N: 7;}
div:nth-child( 8) {--N: 8;}
div:nth-child( 9) {--N: 9;}
div:nth-child(10) {--N: 10;}
div:nth-child(11) {--N: 11;}
div {animation: down-in 1.5s ease forwards calc((var(--N) - 1) * var(--length)), fade-out 1s ease forwards calc(var(--L) + (var(--N) * var(--length))); }
@keyframes fade-in {
0% { opacity:0 }
100% { opacity:1 }
}
@keyframes fade-out {
0% { opacity:1 }
100% { opacity:0 }
}
@keyframes down-in {
0% { margin-top: -90px; opacity:0; }
100% { margin-top: 0px; opacity:1;}
}
@keyframes down-out {
0% { margin-top: 0px; opacity:1; }
100% { margin-top: 90px; opacity:0;}
}
@keyframes up {
0% { margin-top: 0px; opacity:1; }
100% { margin-top: -90px; opacity:0;}
}
@keyframes down_then_down {
0% { margin-top: -90px; opacity:0; }
10% { margin-top: 0px; opacity:0.8;}
20% { margin-top: 0px; opacity:1;}
90% { margin-top: 0px; opacity:1;}
100% {margin-top:90px; opacity:0;}
}
@keyframes up_then_up {
0% { margin-top: 90px; opacity:0; }
10% { margin-top: 0px; opacity:0.8;}
20% { margin-top: 0px; opacity:1;}
90% { margin-top: 0px; opacity:1;}
100% {margin-top:-90px; opacity:0;}
}
@keyframes down_then_up {
0% { margin-top: -90px; opacity:0; }
10% { margin-top: 0px; opacity:0.8;}
20% { margin-top: 0px; opacity:1;}
90% { margin-top: 0px; opacity:1;}
100% {margin-top:-90px; opacity:0;}
}
@keyframes up_then_down {
0% { margin-top: 90px; opacity:0; }
10% { margin-top: 0px; opacity:0.8;}
20% { margin-top: 0px; opacity:1;}
90% { margin-top: 0px; opacity:1;}
100% {margin-top:90px; opacity:0;}
}
@keyframes fadein_then_fadeout {
0% { margin-top: 0px; opacity:0; }
10% { margin-top: 0px; opacity:0.8;}
20% { margin-top: 0px; opacity:1;}
90% { margin-top: 0px; opacity:1;}
100% {margin-top:0px; opacity:0;}
}
</style>
</head>
<body >
<main>
<div>At first I was afraid, I was petrified,</div>
<div>Kept thinking I could never live without you by my side,</div>
<div>But then I spent so many nights thinking how you did me wrong -</div>
<div>And I grew strong,</div>
<div>And I learned how to get along ...</div>
<div>And so you're back - </div>
<div>- from outer space ...</div>
<div>I just walked in to find you here with that sad look upon your face</div>
<div>I should have changed that stupid lock,</div>
<div>I should have made you leave your key,</div>
<div>If I'd known for just one second you'd be back to bother me</div>
</main>
</body>
</html>
[I’ve had to include the text here, since, when I try to upload the file, it actually replaces it on the forum with the file you posted - weird.]
After dinner I’ll look into doing the animation of the characters and also move the creation of “–N” into the javascript, so it will cater for any number of lines, not just 11.