Text Animation with changing the color of the text using HTML & CSS

Text animation is the creation of beautiful and colourful letters, words and paragraphs with a decorative movable effect. The movement can be seen in some fashion within the area or across the screen following some regular pattern.

HTML Code: In this section, the basic structure of the code is designed by using HTML. Text animation is created with changing the color of the text by using some CSS properties .

html

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>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
    <div class="row">
        <span class="text1">Hello!</span>
        <span class="text2">GeeksforGeeks</span>
    </div>
    </div>
</body>
</html>

chevron_right


CSS Code: In this section, CSS properties are used to create Text Animation.
@keyframes are used which defines the code for animation. The animation is created by gradually changing from one set of CSS styles to another. The change of styles or transformations are taking place in percentages, or by using keywords “from” and “to”, which is actually 0% and 100%. The set of CSS styles can be changed many times.

Syntax for @keyframes:



@keyframes animationname {keyframes-selector {css-styles;}} 

 

css

filter_none

edit
close

play_arrow

link
brightness_4
code

*{
    padding: 0;
    margin: 0;
    font-family: sans-serif;
}
body{
    background: yellowgreen;
}
.container{
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
}
.container span{
    display: block;
}
.text1{
    color: white;
    font-size: 70px;
    font-weight: 700;
    letter-spacing: 8px;
    margin-bottom: 20px;
    background: yellowgreen;
    position: relative;
    animation: text 3s 1;
}
.text2{
    font-size: 30px;
    color: darkgreen;
    font-family: Georgia, serif;
}
@keyframes text{
    0%{
        color: black;
        margin-bottom: -40px;
    }
    30%{
        letter-spacing: 25px;
        margin-bottom: -40px;
        }
    85%{
        letter-spacing: 8px;
        margin-bottom: -40px;
    }
}

chevron_right


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

html

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>
        * {
            padding: 0;
            margin: 0;
            font-family: sans-serif;
        }
  
        body {
            background: yellowgreen;
        }
  
        .container {
            text-align: center;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 100%;
        }
  
        .container span {
            display: block;
        }
  
        .text1 {
            color: white;
            font-size: 70px;
            font-weight: 700;
            letter-spacing: 8px;
            margin-bottom: 20px;
            background: yellowgreen;
            position: relative;
            animation: text 3s 1;
        }
  
        .text2 {
            font-size: 30px;
            color: darkgreen;
            font-family: Georgia, serif;
        }
  
        @keyframes text {
            0% {
                color: black;
                margin-bottom: -40px;
            }
  
            30% {
                letter-spacing: 25px;
                margin-bottom: -40px;
            }
  
            85% {
                letter-spacing: 8px;
                margin-bottom: -40px;
            }
        }
    </style>
</head>
  
<body>
    <div class="container">
        <div class="row">
            <span class="text1">Hello!</span>
            <span class="text2">GeeksforGeeks</span>
        </div>
    </div>
</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.