How to Create Text Changing Animation Effect using CSS ?

Almost any website that we visit today uses some kind of text animation to engage with its users. Generally, all kinds of text-animations are made using plain or vanilla JavaScript or by using some third-party JavaScript libraries. But there are some animations that can be made using only CSS. One of them is changing the word text animation. In this type of animation, a word is selected to change after a certain time interval. This is slightly older animation and is one of the earliest text animations that were used a decade ago.

Approach: The approach is to use keyframes to change word at a certain frame. The word can be set using the content property.

HTML Code: In this section, we have heading wrapped inside a <h1> tag. We also have a span which will populate later using CSS.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
    <title>Text Animation</title>
</head>
  
<body>
    <h1>I love <span> </span></h1>
</body>
  
</html>

chevron_right


CSS Code:

  • Step 1: Do some basic style like background-color, text-color, margins, padding etc.
  • Step 2: Now, use before select/or to set the content of span to an initial word.
  • Step 3: Use animation property to set the total time for the animation.
  • Step 4: Now, use keyframes to change the content property that was set in before selector for each frame.

Tip: You can set the total time according to need. But generally, it is recommended to use a time which is a multiple of the total number of words that you want to change. Like in our case, we have three words and we have used 3s as total time. You can use 6, 9, 12… so on. Setting time as multiple of the total number of words will ensure that each word gets displayed for an equal amount of time.



filter_none

edit
close

play_arrow

link
brightness_4
code

<style>
    body {
        background: green;
    }
  
    h1 {
        display: flex;
        justify-content: center;
        color: white;
    }
  
    span::before {
        content: "Geeks";
        animation: animate infinite 3s;
        padding-left: 10px;
    }
  
    @keyframes animate {
  
        0% {
            content: "Geeks";
        }
  
        50% {
            content: "for";
        }
  
        75% {
            content: "Geeks";
        }
    }
</style>

chevron_right


Complete Code: It is the combination of the above two sections of code.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
    <title>Text Animation</title>
      
    <style>
        body {
            background: green;
        }
  
        h1 {
            display: flex;
            justify-content: center;
            color: white;
        }
  
        span::before {
            content: "Geeks";
            animation: animate infinite 3s;
            padding-left: 10px;
        }
  
        @keyframes animate {
  
            0% {
                content: "Geeks";
            }
  
            50% {
                content: "for";
            }
  
            75% {
                content: "Geeks";
            }
        }
    </style>
</head>
  
<body>
    <h1>I love <span> </span></h1>
</body>
  
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.