Home >Web Front-end >CSS Tutorial >How to Create a CSS Typewriter Effect for Your Website

How to Create a CSS Typewriter Effect for Your Website

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2025-02-08 10:20:13566browse

Pure CSS creates engaging typewriter text effects

Core points:

  • CSS typewriter effect makes the website content more dynamic and attractive by gradually displaying text, and can be used for login pages, personal websites and code demonstrations.
  • The
  • Typewriter effect can be created by using the CSS steps() function to change the width of the text element from 0% to 100%, and to simulate the cursor of "Putout" the text through animation.
  • The typing effect can be adjusted by increasing or decreasing the number of steps and duration of the typing animation to accommodate longer or shorter text.
  • The
  • Typewriter effect can be used in conjunction with the flashing cursor animation to enhance the effect, and the cursor can be customized by adjusting its border-right properties, color, flicker frequency, etc.

This article will guide you how to create dynamic, more attractive website text typewriter effects using pure CSS.

The typewriter effect simulation text is displayed word by word, just like typing in real time in front of you.

How to Create a CSS Typewriter Effect for Your Website

Adding typewriter effects in text snippets helps attract website visitors and keep them interested in continuing to read. Typewriter effects can be used for a variety of purposes, such as creating engaging login pages, call-to-action elements, personal websites, and code presentations.

Easy to create typewriter effects

Creating a typewriter effect is very simple, you only need to have the basic knowledge of CSS and CSS animations to understand this tutorial.

The working principle of typewriter effect is as follows:

  • Typewriter animation will gradually change the width of the text element from 0% to 100% by using the CSS steps() function, thus displaying our text element.
  • The flashing animation will simulate the cursor of "Putout" the text.

Create a typewriter effect webpage

First, let's create a web page for the typewriter demonstration. It will contain a <div> container for typewriter text, with class name <code>typed-out:

<code class="language-html"><!doctype html>

  
    <title>Typewriter effect</title>
    <style>
      body{
        background: navajowhite;
        background-size: cover;
        font-family: 'Trebuchet MS', sans-serif; 
      }
    </style>
  
  
    <div class="container">
      <div class="typed-out">Web Developer</div>
    </div>
  
</code>

Style style for typewriter text container

Now that we have the layout of the web page, let's style for <div> with the "typed-out" class: <pre class="brush:php;toolbar:false">&lt;code class=&quot;language-css&quot;&gt;.typed-out { overflow: hidden; border-right: .15em solid orange; font-size: 1.6rem; width: 0; }&lt;/code&gt;</pre> <p>Please note that in order to make the typewriter effect effective, we have added the following: </p> <ul> <li> <code>"overflow: hidden;" and "width: 0;" make sure that the text content is not displayed until the typing effect begins.

  • "border-right: .15em solid orange;", create the typewriter cursor.
  • Before making a typing effect, in order to stop the cursor after the last letter of the typed-out element is fully typing (like a typewriter or word processor), we will create a container for the typed-out element and add display: inline-block; :

    <code class="language-css">.container {
      display: inline-block;
    }</code>

    Create display text animation

    Typewriter animation will create a verbatim effect for text within the typed-out element to be displayed. We will use @keyframes CSS animation rules:

    <code class="language-html"><!doctype html>
    
      
        <title>Typewriter effect</title>
        <style>
          body{
            background: navajowhite;
            background-size: cover;
            font-family: 'Trebuchet MS', sans-serif; 
          }
        </style>
      
      
        <div class="container">
          <div class="typed-out">Web Developer</div>
        </div>
      
    </code>

    As you can see, all this animation does is change the width of the element from 0% to 100%.

    Now, we include this animation in our typed-out class and set its animation orientation to forwards to ensure that the text element does not return to width: 0 after the animation is over:

    <code class="language-css">.typed-out {
      overflow: hidden;
      border-right: .15em solid orange;
      font-size: 1.6rem;
      width: 0;
    }</code>

    Our text elements will be displayed from left to right in a smooth manner:

    Add steps to achieve typewriter effect

    So far, our text has been displayed, but it is a smooth way to not display the text verbatim. This is a start, but obviously it doesn't look like the typewriter effect.

    In order for this animation to display our text elements verbatim or step by step (just like a typewriter), we need to split the typing animation contained in the typed-out class into steps so that it looks like it is typing out. This is where the CSS steps() function comes into play:

    <code class="language-css">.container {
      display: inline-block;
    }</code>

    As you can see, we use the CSS steps() function to divide the typing animation into 20 steps.

    Adjust the steps for longer typing

    To adjust longer text, you need to increase the steps and duration of the typing animation.

    Adjust the steps for shorter typing

    To adjust shorter text, you need to reduce the steps and duration of typing animations.

    Create and set flashing cursor animation

    Apparently, the original mechanical typewriters did not have a flashing cursor, but adding it in to mimic the flashing cursor effect of more modern computer/word processors has become tradition. The flashing cursor animation helps make the imprinted text stand out from the static text elements.

    To add a flashing cursor animation to our typewriter animation, we will first create a flashing animation:

    <code class="language-css">@keyframes typing {
      from { width: 0 }
      to { width: 100% }
    }</code>

    In our webpage, this animation will change the border color of the typed-out element border (used as the cursor for typewriter effects) from transparent to orange.

    We include this animation in the rules of the typed-out class and set its animation direction property to infinite to make the cursor disappear and reappear indefinitely every 0.8 seconds:

    <code class="language-css">.typed-out{
        overflow: hidden;
        border-right: .15em solid orange;
        white-space: nowrap;
        font-size: 1.6rem;
        width: 0;
        animation: typing 1s forwards;
    }</code>

    Code to adjust the flashing typing effect

    We can make the cursor thinner or thicker by adjusting its border-right: .15em solid orange; attribute, or you can make the cursor color differently, give it border-radius, adjust its flicker frequency, etc.

    Elements of Combining Typewriter Text Animation

    Now that you know how to make a typewriter effect in CSS, it's time to demonstrate some practical and relevant use cases for this typewriter effect.

    Conclusion

    In this article, we understand how easy it is to create animated "typewriter" text using CSS. This typing effect can definitely add fun and fun to your page.

    However, it may be worth a gentle warning at the end. This technique is best used for a small amount of non-critical text, just to add a little extra fun. But be careful not to over-rely rely on it, as there are some limitations to using CSS animations like this. Make sure to test your typewriter text on various devices and viewport sizes, as the results may vary by platform. Also consider end users who rely on assistive technology, ideally, some usability tests can be performed to ensure you don't cause inconvenience to users. Because you can do something with pure CSS doesn't necessarily mean you should. If typewriter effects are important to you and you want to use it for more critical content, maybe at least consider a JavaScript solution, too.

    Anyway, I hope you enjoyed this post and it gets you to think about what other cool things you can do with CSS animations to add fun and fun to your page.

    Frequently Asked Questions about Creating CSS Typewriter Effects

    Finally, let's answer some of the most common questions about how to create typewriter effects in CSS.

    • What is the typewriter effect?

    "Typewriter Effect" is an animation technique that makes a string of text appear word by word on the screen as if it is being typed in real time by the typewriter. This effect is usually created with JavaScript, but it can also be implemented using CSS only, as shown in this article.

    • What is typewriter animation?

    The typewriter prints text one letter at a time. Typewriter animation is an animation that imitates typewriter typing, presenting text with one letter at a time. It is a popular animation effect on many web pages.

    • How to make animated text typing in CSS?

    Modern CSS provides a variety of tools for creating animations, including animation, @keyframes, steps(). These tools are used to gradually display text that is first hidden through the overflow attribute.

    • How to use CSS to create customizable typewriter animations?

    Creating customizable typewriter animations with CSS involves using keyframes and CSS properties to control how text looks and behaves when typing on the screen. You can make it customizable by exposing some animation parameters as CSS variables (custom properties) so that you can easily change them in your stylesheet. For example:

    In this CSS code, we define custom properties (
    <code class="language-html"><!doctype html>
    
      
        <title>Typewriter effect</title>
        <style>
          body{
            background: navajowhite;
            background-size: cover;
            font-family: 'Trebuchet MS', sans-serif; 
          }
        </style>
      
      
        <div class="container">
          <div class="typed-out">Web Developer</div>
        </div>
      
    </code>
    and

    ) to make the animation customizable. You can change the default values ​​by modifying these properties. --typewriter-text --typewriter-duration

      How to stop the cursor after the last letter of the
    • element is fully printed? typed-out
    • To stop the cursor of the last letter of the
    element in the CSS typewriter animation, you can use the CSS animation and the

    attribute: typed-out

    <code class="language-html"><!doctype html>
    
      
        <title>Typewriter effect</title>
        <style>
          body{
            background: navajowhite;
            background-size: cover;
            font-family: 'Trebuchet MS', sans-serif; 
          }
        </style>
      
      
        <div class="container">
          <div class="typed-out">Web Developer</div>
        </div>
      
    </code>

    In the above CSS, the typewriter animation gradually increases the width of the elements in the container, effectively typing out text. The .typewriter property is set to animation-fill-mode to ensure that the animation remains final (completely played out) after completion. With this setting, the cursor will flash at the forwards element after it is fully printed. typed-out

    • What are some website examples that effectively use typewriter effects?
    Typewriter animations are often used on sites such as portfolio sites, especially on designers and developers’ websites, which are used to highlight key skills and add creative sense to the page, thereby attracting the reader’s attention. Typewriter effects are sometimes used in blog websites and login pages as well as product demonstrations.

    The above is the detailed content of How to Create a CSS Typewriter Effect for Your Website. For more information, please follow other related articles on the PHP Chinese website!

    JavaScript css for 样式表 display overflow border animation
    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Previous article:Quick Tip: How to Animate Text Gradients and Patterns in CSSNext article:Quick Tip: How to Animate Text Gradients and Patterns in CSS

    Related articles

    See more