Search code examples
cssanimationtyping

How to stop the cursor after the text is finished


I have a typewriting effect on my webpage but it has a little problem. After the text finished typing the blinking cursor keeps moving to the end of the line. How do I make it stay after the last letter?

It's basic but maybe I need to add something more or modify it?

.typewriter h1{
  position: relative;
  top: 7em;
  width: fit-content;
  overflow: hidden;
  border-right: .15em solid white;
  white-space: nowrap;
  margin: 0 auto;
  letter-spacing: .15em;
  animation: typing 3.5s steps(100, end),
            blink-caret .75s step-end infinite;
}

@keyframes typing{
  from {width: 0}
  to {width: 100%}
}

@keyframes blink-caret{
  from, to {border-color: transparent}
  50% {border-color: white}
}
<div class="typewriter">
    <h1>Welcome to my website</h1>
</div>


Solution

  • It's because the width of your .typewriter h1 is set to 100%.

    Use max-width and set the width to auto and display to inline-block.

    .typewriter h1{
      width: auto;
      display: inline-block;
      overflow: hidden;
      border-right: .15em solid white;
      white-space: nowrap;
      margin: 0 auto;
      letter-spacing: .15em;
      animation: typing 3.5s steps(100, end),
    			 blink-caret .75s step-end infinite;
    }
    
    @keyframes typing{
      from {max-width: 0}
      to {max-width: 100%}
    }
    
    @keyframes blink-caret{
      from, to {border-color: transparent}
      50% {border-color: black}
    }
    <div class="typewriter">
        <h1>Welcome to my website</h1>
    </div>