Pure HTML+CSS to achieve typing effect

Pure HTML+CSS to achieve typing effect

This article mainly introduces the typing effect achieved by pure HTML+CSS, which has a certain reference value. If you are interested, you can learn more about it.

Rendering

analyze

Animation can be thought of as three different levels:

  • The bottom text
  • Background blocking the text in the middle
  • The top cursor

The text is static, while the background in the middle and the cursor on the top are dynamic.
Initially, the background covers all text and the cursor is at the far left.
As the animation progresses, the background and the cursor move from left to right at the same pace.
When the animation ends, the background no longer obscures the text and the cursor flashes on the far right.

Code

html

<div class="text">hello, world!</div>

CSS

:root {
    /* Number of characters */
    --steps: 12;
    /* Animation time */
    --duration: 2.5s;
    /* Font size */
    --fontSize: 50px;
    /* cursor size */
    --cursorSize: 20px;
}

.text {
    color: #333;;
    position: relative;
    display: inline-block;
    font-family: 'Courier New', Courier, monospace;
    font-size: var(--fontSize);
    line-height: 1;
}

.text::after {
    content: '';
    width: var(--cursorSize);
    height: var(--fontSize);
    background-color: black;
    z-index: 2;
    position: absolute;
    animation: blink 1s var(--duration) step-end infinite,
               moveCursor var(--duration) steps(var(--steps)) forwards;
}

.text::before {
    content: '';
    width: 100%;
    height: var(--fontSize);
    z-index: 1;
    position: absolute;
    background: linear-gradient(#fff, #fff) no-repeat top right;
    animation: showText var(--duration) steps(var(--steps)) forwards;
}

/* Cursor blinking animation*/
@keyframes blink {
    0% {
        background-color: black;
    }
    50% {
        background-color: transparent;
    }
    100% {
        background-color: black;
    }
}

/* Cursor movement animation*/
@keyframes moveCursor {
    0% {
        left: 0%;
    }
    100% {
        left: 100%;
    }
}

/* Background moving animation */
@keyframes showText {
    0% {
        background-size: 100% 100%;
    }
    100% {
        background-size: 0% 100%;
    }
}

Note that the font must be monospaced. Because the distance the cursor moves each time is determined by the number of characters/total width.

Online Demo

This is the end of this article about how to achieve typing effects with pure HTML+CSS. For more relevant HTML+CSS typing effects content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  HTML+CSS to achieve surround reflection loading effect

>>:  HTML+CSS to implement the sample code of the navigation bar drop-down menu

Recommend

JavaScript implements the detailed process of stack structure

Table of contents 1. Understanding the stack stru...

MySQL column to row conversion tips (share)

Preface: Because many business tables use design ...

Eight ways to implement communication in Vue

Table of contents 1. Component Communication 1. P...

Docker installation and configuration steps for RabbitMQ

Table of contents Single-machine deployment Onlin...

Several situations that cause MySQL to perform a full table scan

Table of contents Case 1: Case 2: Case 3: To summ...

Docker images export and import operations

What if the basic images have been configured bef...

In-depth understanding of umask in new linux file permission settings

Preface The origin is a question 1: If your umask...

Node.js sends emails based on STMP protocol and EWS protocol

Table of contents 1 Node.js method of sending ema...

XHTML Getting Started Tutorial: Form Tags

<br />Forms are an important channel for use...

vue-table implements adding and deleting

This article example shares the specific code for...

mysql5.6.zip format compressed version installation graphic tutorial

Preface: MySQL is a relational database managemen...

How to install rabbitmq-server using yum on centos

Socat needs to be installed before installing rab...

Example of using UserMap in IMG

usemap is an attribute of the <img> tag, use...