Text Animation using HTML & CSS @keyframes Rule
Last Updated :
09 Apr, 2025
Text animation is a powerful tool for creating visually engaging and interactive web pages. HTML and CSS offer a range of options for creating advanced text animation effects. In Particular, the CSS '@keyframes' rule comes in handy.
The CSS '@keyframes' rule is used to define animations that change CSS styles over time. It specifies a set of keyframes or frames that represent different states of an element at specific times during the animation. The styles between the keyframes are automatically interpolated, creating a smooth animation effect. By combining keyframes with other CSS properties, you can create a wide range of creative and interactive animations for your web pages.
Please note that we will use Browsersync to start a server and provide a URL to view the HTML site, and CSS Animation and to load the respective JavaScript files. Check out this article on how to install and run Browsersync on your local machine. Feel free to use any other tool of your choosing.
In this article, we'll explore several examples of advanced text animation using the HTML and CSS '@keyframes' rule.
Example 1: Animated Text Shadow: An animated text-shadow can be created by using the CSS 'text-shadow' property along with the @keyframes rule. This property is used to specify the color, blur radius, and offset of the shadow.
Here's an example of how to create an animated text-shadow:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Animated Text Shadow</title>
<style>
h2 {
font-size: 36px;
font-weight: bold;
animation: textShadow 1s ease-in-out
infinite alternate;
}
@keyframes textShadow {
from {
text-shadow: 2px 2px #333;
}
to {
text-shadow: 10px 10px #333;
}
}
</style>
</head>
<body>
<h1 style="color:green">
Geeks for Geeks
</h1>
<h2>Animated Text Shadow</h2>
</body>
</html>
In this example, the 'text-shadow' property is used to specify the color and size of the shadow. The @keyframes rule is used to specify the animation, and the animation property is used to specify the animation's name, duration, easing, iteration count, and direction. The result is an 'h2' element with a moving text-shadow that changes size over time.
Output:
Example 2: Animated Gradient Text: An animated gradient text effect can be created using CSS linear or radial gradients along with the @keyframes rule. The 'background-clip' property is used to control the background of the text, and the '-webkit-background-clip' property is used for compatibility with older browsers.
Here's an example of how to create an animated gradient text:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Animated Text Gradient</title>
<style>
h2 {
font-size: 36px;
font-weight: bold;
animation: gradientText 5s ease-in-out infinite;
}
@keyframes gradientText {
0% {
color: red;
}
25% {
color: yellow;
}
50% {
color: blue;
}
100% {
color: green;
}
}
</style>
</head>
<body>
<h1 style="color:green">
Geeks for Geeks
</h1>
<h2>Animated Gradient Text</h2>
</body>
</html>
In this example, a linear gradient is used to specify the colors of the gradient, and the 'background-clip' and '-webkit-background-clip' properties are used to control the background of the text. The @keyframes rule is used to specify the animation, and the animation property is used to specify the animation's name, duration, easing, iteration count, and direction. The result is an 'h2' element with a text background that animates over.
Output:
Similar Reads
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
HTML Interview Questions and Answers
HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers
NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Introduction of Firewall in Computer Network
A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read
What is an API (Application Programming Interface)
In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Web Development Technologies
Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.Core Concepts of Web Development TechnologiesWeb
8 min read