I have been trying the typewriter animation, and it went well!
.typewriter{
display: flex;
justify-content: center;
}
.typewriter .p1,.p2{
font-family: monospace;
font-size: 1.5rem;
/* margin-inline: auto; */
overflow: hidden;
/* keeps on a single line */
white-space: nowrap;
/* cursor */
border-right: 3px solid;
/* steps = number of characters */
animation: typing 3s steps(22) forwards, blink 1s step-end infinite;
}
.typewriter .p2{
font-size: 3rem;
animation-delay: after previos;
}
@keyframes typing{
from{
width: 0;
}
to{
width: 100%;
}
}
@keyframes blink{
50%{
border-color: transparent;
}
}
<div class = "typewriter">
<div>
<p class = "p1">Hello, Welcome to CodePen!</p>
<p class = "p2">Let's get started!</p>
</div>
</div>
But I am confused, about how to play the second line animation after the first one.
I am trying to create a typewriter animation for two specific paragraphs with different lengths and font sizes.
I want to customize the animation delay for each line so that the second paragraph starts typing only after the first finishes.
Solutions that use animation-delay
or other modern techniques are welcome, but the existing duplicates do not address the unique requirements of this typewriter effect.
2
Answers
After going through the w3schools documentation and taking @AHaworth, @Yogi, and @alvinalvord's solutions, I have ended up with an answer myself. I would like to thank you all for your involvement.
The typewriter effect is a bit challenging when it comes to multiple elements with the conditions set by me:
By combining CSS properties like
visibility
,nth-child(n)
,animation-fill-mode
I was able to achieve what I desired:The
visibility:hidden
property ensures subsequent lines remain hidden until their animations start.The
nth-child(n)
selector synchronizes the animations for each line.The
animation-fill-mode:forwards
keeps the lines visible after typing.Even though I have achieved what I wanted, there are still some issues with the spacing. If you run the snippet you can see that the cursor extends to the full-width of the element even though I have set
width: fit-content
to all<p>
elements.If anyone can solve this problem, it would be a great help.
I modified one of your css class and added another keyframe and if my understanding is correct, you are looking for something like this?
A brief explanation is basically to hide .p2 for 3 seconds (same duration as .p1 animation) then play the typing animation before the full 3 seconds completes (2.9s delay).