Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Perform Animation on CSS font-size property
The CSS font-size property can be animated to create text that grows or shrinks smoothly over time. This creates engaging visual effects for headings, hover states, or attention-grabbing elements.
Syntax
@keyframes animation-name {
keyframe-selector {
font-size: value;
}
}
selector {
animation: animation-name duration timing-function iteration-count;
}
Example: Basic Font Size Animation
The following example demonstrates animating the font-size property from its default size to 30px −
<!DOCTYPE html>
<html>
<head>
<style>
.animated-text {
border: 2px solid #333;
padding: 20px;
width: 400px;
height: 100px;
display: flex;
align-items: center;
justify-content: center;
font-size: 16px;
animation: fontGrow 3s ease-in-out infinite;
}
@keyframes fontGrow {
0% {
font-size: 16px;
}
50% {
font-size: 30px;
}
100% {
font-size: 16px;
}
}
</style>
</head>
<body>
<div class="animated-text">Growing Text Animation</div>
</body>
</html>
A bordered box containing text that smoothly grows from 16px to 30px and back to 16px in a 3-second cycle, repeating infinitely.
Example: Hover-Triggered Font Size Animation
You can also trigger font size animations on user interactions like hover −
<!DOCTYPE html>
<html>
<head>
<style>
.hover-text {
font-size: 18px;
color: #2196F3;
cursor: pointer;
transition: font-size 0.3s ease;
padding: 10px;
display: inline-block;
}
.hover-text:hover {
font-size: 28px;
}
</style>
</head>
<body>
<span class="hover-text">Hover over me to see font size change</span>
</body>
</html>
Blue text that smoothly increases from 18px to 28px when hovered over, creating a smooth scaling effect.
Conclusion
Animating the font-size property adds dynamic visual interest to text elements. Use keyframes for continuous animations or transitions for user-triggered effects like hover states.
Advertisements
