Skip to content
Related Articles

Related Articles

Improve Article

How to Create Typewriter Animation using HTML and CSS ?

  • Last Updated : 22 Jul, 2021

Typewriter animation as the name suggests is an effect that looks like a typing animation, as being typed by a typewriter. We are going to create this animation without JavaScript and just using HTML and CSS.

Approach: To achieve the typewriter effect we will use the following CSS properties.

  • animation-timing-function: We need to specify the time of the animation in seconds or milliseconds so that the animation is faster and looks perfect. We also need to specify steps so that each letter appears not in a single flow but one by one as seen in typewriters. In the below code we are using the example GeeksforGeeks as the total number of letters in GeeksforGeeks is 13, so the number of steps used is 13.
  • overflow: So that each text appears according to the animation and is not revealed all at once, overflow: hidden is used in the below code.
  • white-space: To get the animation on a single line white-space: nowrap is used.
  • border-color: As seen in the below code the border color is animated from black too transparent to give the animation a realistic touch of a blinking cursor any color other than black can also be used.
  • width: To get a typing like animation width is animated from 0% to 50% (this can vary as per the text height/width or the given text spacing).
     

Example: In this example, we will be going to use above-defined properties to create typewriter animation.

index.htm




<!DOCTYPE html>
<html>
  <head>
    <title>Typewriter Effect</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <style type="text/css">
       {
        box-sizing: border-box;
        padding: 0;
        margin: 0;
        outline: none;
      }
      body {
        background-color: #2ca32c;
        justify-content: center;
        align-items: center;
        height: 100vh;
        display: flex;
      }
  
      h1 {
        font-size: 90px;
        white-space: nowrap;
        overflow: hidden;
        animation: typewriter 2s steps(13) infinite alternate,
          blink 800ms steps(13) infinite normal;
        border-right: 5px solid black;
      }
      @keyframes typewriter {
        from {
          width: 0%;
        }
        to {
          width: 50%;
        }
      }
      @keyframes blink {
        from {
          border-color: black;
        }
        to {
          border-color: transparent;
        }
      }
    </style>
  </head>
  <body>
    <h1>GeeksforGeeks</h1>
  </body>
</html>

Output:

Note: For other longer or shorter text, required changes should be done in font size, steps. The time of the animation of text and the cursor should also be changed accordingly.

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :