🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Creating a typing animation effect in CSS can add a dynamic and engaging touch to your web page. This effect mimics the appearance of text being typed out in real-time, often used for headers or intros.
In this tutorial, you'll learn how to create a typing animation effect using CSS by combining keyframes and the border-right property to simulate a blinking cursor.
Problem Statement
Create a CSS code that:
- Animates text to appear as if it's being typed out.
- Adds a blinking cursor effect for a realistic typing experience.
Example:
- Input: A paragraph element with the text "Typing Animation Example".
- Output: The text appears letter by letter, followed by a blinking cursor.
Solution Steps
- Use
@keyframesfor Typing Animation: Create keyframes to gradually reveal the text. - Simulate Blinking Cursor: Use the
border-rightproperty to create a blinking cursor effect. - Control Animation Timing: Use
animationproperty to adjust the typing speed and cursor blink rate.
HTML Structure
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Typing Animation Example</title>
<style>
/* Step 1: Create typing effect */
.typing-text {
font-family: monospace;
white-space: nowrap;
overflow: hidden;
border-right: 3px solid black; /* Simulate cursor */
width: 0; /* Initial width is 0 */
animation: typing 4s steps(30, end), blink-caret 0.75s step-end infinite;
}
/* Step 2: Define keyframes for typing */
@keyframes typing {
from { width: 0; }
to { width: 100%; }
}
/* Step 3: Blinking cursor effect */
@keyframes blink-caret {
from { border-right-color: black; }
to { border-right-color: transparent; }
}
/* Additional styling */
.container {
width: 400px;
margin: 50px auto;
font-size: 24px;
}
</style>
</head>
<body>
<div class="container">
<p class="typing-text">This is a typing animation example with CSS!</p>
<p class="typing-text">This is a typing animation example with CSS!</p>
<p class="typing-text">This is a typing animation example with CSS!</p>
</div>
</body>
</html>
Output
Explanation
Step 1: Use @keyframes to Simulate Typing
- To animate the text being typed out, use the following CSS:
.typing-text { white-space: nowrap; overflow: hidden; border-right: 3px solid black; width: 0; animation: typing 4s steps(30, end), blink-caret 0.75s step-end infinite; }
Step 2: Define Keyframes for Typing
- The
@keyframes typingrule gradually increases the width of the text element from0to100%:@keyframes typing { from { width: 0; } to { width: 100%; } }
Step 3: Simulate the Blinking Cursor
- The
@keyframes blink-caretcreates a blinking cursor effect by alternating theborder-right-colorbetween black and transparent:@keyframes blink-caret { from { border-right-color: black; } to { border-right-color: transparent; } }
Conclusion
Creating a typing animation effect with CSS is easy using @keyframes for the typing effect and a blinking cursor. This engaging effect can be used in headers, intros, or anywhere you want to grab your audience's attention.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment