Creating visually engaging web interfaces is no longer just about layout and responsiveness—it’s about micro-interactions and visual storytelling. One such technique that adds flair to your typography is text background animation using CSS.
In this tutorial, we’ll walk through how to create a bold, animated text effect using pure HTML and CSS. This effect is perfect for hero sections, landing pages, or any UI element that needs to grab attention.
HTML Structure
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@900&display=swap" rel="stylesheet"> <div class="box-container"> <h1 class="text-bg">A</h1> </div>
We’re importing the Roboto font with a heavy weight (900) to give our text a bold presence. The <div> acts as a wrapper, and the <h1> is the main element where the animated background will be applied.
CSS Styling
body {
font-family: 'Roboto', sans-serif;
}
.box-container {
max-width: 100%;
width: 400px;
margin: 0 auto;
box-shadow: 0px 0px 10px #0000000f;
border-radius: 10px;
text-align: center;
padding: 20px;
background: #ffc5bf;
}
This sets the stage for the animated text to shine. The container is centered, padded, and styled with a soft background and subtle shadow.
Applying the Background Image to Text
h1.text-bg {
background-image: url(https://code.bydev24.com/uploads/code-snippts/text-bg-background.jpg);
}
This line applies a background image directly to the text element. The image will later be clipped to the text using background-clip.
Creating the Animated Text Effect
h1.text-bg {
margin: 0;
font-size: 300px;
-webkit-text-fill-color: transparent;
background: -o-linear-gradient(transparent, transparent);
-webkit-background-clip: text;
background-position: center;
-webkit-animation: moving 21s linear infinite;
-moz-animation: moving 21s linear infinite;
-o-animation: moving 21s linear infinite;
}
This is the core of the text background animation using CSS. It hides the text color, clips the background to the text, and animates the background image.
Keyframes for Animation
@-webkit-keyframes moving {
0% { background-position: right bottom; }
50% { background-position: left top; }
100% { background-position: right bottom; }
}
@-moz-keyframes moving {
0% { background-position: right bottom; }
50% { background-position: left top; }
100% { background-position: right bottom; }
}
These keyframes animate the background image’s position, creating a smooth loop. Multiple vendor prefixes ensure cross-browser compatibility.
Why Use Text Background Animation?
- Visual Impact: It grabs attention instantly.
- Branding: You can use branded textures or gradients.
- Minimal Code: No JavaScript required.
- Performance: Lightweight and fast-loading.
Browser Compatibility Tips
Use vendor prefixes to ensure support across Chrome, Firefox, Safari, and Opera. Test on mobile devices—some older browsers may not support background-clip: text.
Live link – https://code.bydev24.com/snippets/text-background-animation

0 comments