Based on your question, I assume that you are not looking for a natural typewriter effect but more on how to extend what you have already created to the 2nd, 3rd lines and also hide the first line when the second is animating and so on. If this is indeed the case, please read further.
Since you have to animate multiple elements but not at the same time, you need to add a delay. The delay for each element should be calculated such that all the prior elements would have finished their animation by the time the animation starts for the current one. So, for example, the first element would need no delay but the second would need 5s delay, the third would need 10s delay and so on. These delays can be applied to the elements by using the nth-child
selectors like in the below snippet.
Finally, since you are looking for the previous lines to disappear when the next element is animating, the default/original width of the element should be 0 and should animate to 10em.
.css-typing {
width: 0;
white-space: nowrap;
overflow: hidden;
}
@keyframes type {
to {
width: 10em;
}
}
@-webkit-keyframes type {
to {
width: 10em;
}
}
p{
-webkit-animation: type 5s steps(50, end);
animation: type 5s steps(50, end);
}
p:nth-child(2){
-webkit-animation-delay: 5s;
animation-delay: 5s;
}
p:nth-child(3){
-webkit-animation-delay: 10s;
animation-delay: 10s;
}
<p class='css-typing'>To save their world</p>
<p class='css-typing'>They must come to ours</p>
<p class='css-typing'>Spongebob out of water</p>