Search code examples
htmlcsseffect

How to get the cursor to blink upon the completion the second line - Typewriting effect using CSS


I am trying to implement the typewriting effect on two lines for a webpage (black background), where I am not able to execute a cursor blinking once the second sentence has been typed out.

I have put out the HTML and CSS code. Any help in rectifying where I've gone wrong will be helpful.

HTML:

      <div class="typewriter">
        <h1>Hi,</h1>
        <h2>This is <span style="color: orange">ABC.</span></h2></div> 

CSS:

.typewriter h1 {
    color: #fff;
    font-family: 'DM Mono', monospace;
    overflow: hidden;
    font-size: 80px;
    border-right: .15em solid black;
    white-space: nowrap;
    letter-spacing: .15em;
    width: 4ch;
    animation: typing 2.5s steps(4, end), blink-caret .75s step-end 4;
  }
.typewriter h2 {
    color: #fff;
    font-family: 'DM Mono', monospace;
    font-size: 40px;
    white-space: nowrap;
    overflow: hidden;
    border-right: .15em solid orange;
    width: 20ch;
    animation: typing2 2s steps(20, end), blink-caret 1s step-end;
    animation-delay: 3s;
    animation-fill-mode: both;
  }
@keyframes typing {
    from { 
        width: 0 
    }
    to { 
      width: 3em;
    }
}
@keyframes typing2 {
    from { 
        width: 0 
    }
    to { 
      width: 20em;
    }
}
@keyframes blink-caret {
    from, to {
      border-color: transparent
    }
    50% {
      border-color: white;
    }
} 

Solution

  • I don't know why this works, but it works. Let me know if it gives the desired effect.

        .typewriter h2 {
            color: #fff;
            font-family: 'DM Mono', monospace;
            font-size: 40px;
            white-space: nowrap;
            overflow: hidden;
            border-right: .15em solid orange;
            //this is what i changed
            width: 12ch;
            animation: typing2 2s steps(12, end), blink-caret 1s step-end infinite;
            ////
            animation-delay: 3s;
            animation-fill-mode: both;
        }
        @keyframes typing2 {
            from { 
                width: 0 
            }
            to {
                //this is what i changed
                width: 12ch;
                ////
            }
        }