Hmmm, Let's do it by some steps. These are CSS steps to run your underline from left to right. This is so simple, you just need to add your @keyframes in the stylesheet.
A CSS underline animation can create a left-to-right reveal for links, headings, or short labels. The example below uses a repeating gradient that moves across the text; for a single solid line, the pseudo-element approach later in this guide gives you a cleaner reveal and more predictable timing.
@keyframes border-dance {
from {
background-position: 0% bottom;
}
to {
background-position: 100% bottom;
}
}
In the above code, we are using @keyframes with my class border-dance. In this class, we can define animation from and to. Now you need to add your class which will use for the underline.
.animated_underline {
background-image: linear-gradient(
to right,
silver 50%,
transparent 0%
);
background-size: 6px 4px;
background-repeat: repeat-x;
background-position: 0% bottom;
animation-name: border-dance;
animation-duration: 24s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
In the animated_underline class, we are defining colour, line size, position etc. You can modify as per your requirement.
Understand what the original gradient does
The repeating-gradient sample above animates background position, so its marks travel along the text rather than a single solid line growing from one edge. Keep that effect if the moving dash pattern is what you want. For a conventional left-to-right link underline, a pseudo-element scaled from zero to full width is generally easier to control and avoids changing the link’s layout.
Use the technique that matches the visual goal: native text decoration for links within paragraphs, a pseudo-element for a short menu or heading link, and a repeating background only when a patterned line is intentional. Test wrapped text because a positioned pseudo-element often follows the link’s box, while native decoration follows individual lines.
Build a Solid Underline with a Pseudo-Element
Attach the line to the link
A pseudo-element gives you more control than a repeating gradient when you want one clean underline that reveals on interaction. Make the link an inline-block or another positioned box, set its text decoration as desired, and create an absolutely positioned ::after line at the bottom. Give the pseudo-element an empty content value, a height for the stroke, and a background color that remains visible against the page.
Reveal the line from the left
Set the line’s transform origin to the left edge and start it at scaleX(0). On hover and keyboard focus, change the scale to 1 and transition the transform. Because the element scales around its origin, it appears to grow from left to right without changing the link’s layout width. This is usually easier to tune than moving a repeated background image across the text.

Keep the clickable anchor itself as the interactive element; the pseudo-element is only decoration. If the link wraps onto several lines, test the effect carefully: a single positioned underline may follow the element box rather than each line of text. For multi-line links, consider native text-decoration or a background-based technique that repeats the underline on each line.
Make Hover Effects Work for Keyboard Focus
Share the visual state
Mouse users can discover a hover effect, but keyboard users move through links with focus. Apply the same underline state to :focus-visible as to :hover so the cue is available without a mouse. Keep a separate, clear focus outline or another unmistakable focus indicator; an animated underline alone may be too subtle to show which control currently has keyboard focus.
Do not hide the normal link affordance
If a link has no underline until hover, make sure its color, context, and focus treatment still identify it as interactive. On touch screens there may be no hover state at all. A restrained always-visible underline or another persistent cue can make links easier to recognize and tap, especially in paragraphs with nearby plain text.
Choose Timing and Direction Intentionally
Use a short transition for a small detail
Underline motion is a small interface cue, so it should finish promptly and should not distract from reading. Start with a modest duration and an easing curve that feels consistent with the rest of the site. Test both entering and leaving the link: a long reverse animation can make the underline appear to lag behind the user’s action.
Reverse the origin for a right-to-left reveal
To make the stroke grow from the other side, set its transform origin to the right edge. For a two-sided reveal, use a different technique, such as scaling from the center. Choose direction deliberately for your writing direction and design; if the site supports right-to-left languages, test that layout rather than assuming the left edge is always the correct starting point.
Style the Underline Without Hurting Readability
Match color and thickness to the text
Use a line color that contrasts with its background and remains distinguishable from the link text. A very thin, faint line may disappear on high-density displays or against a textured background. Check the underline on light and dark themes and avoid relying on color alone to communicate focus or selection.
Leave a little space below the letters
Position the underline so it does not collide with descenders such as g, p, and y. Use a small offset below the baseline or a bottom positioning value that fits the font and line height. Make sure the line does not overlap adjacent content, especially when links appear close together in navigation menus.
Respect Reduced-Motion Preferences
Keep the link clear when motion is reduced
Some visitors ask their device to minimize non-essential movement. Use a prefers-reduced-motion media query to remove or shorten the transition for those users. The link should remain recognizable and the underline should still appear in the appropriate state; reduced motion should simplify movement, not remove the interaction cue.
Check the effect with the preference enabled
Turn on the operating system’s reduced-motion setting and inspect both pointer and keyboard interaction. Confirm that the line does not keep running indefinitely and that the focus indicator remains visible. A static underline or a very short, subtle transition is often enough for this decorative effect.
Keep reduced motion simple and predictable
A reduced-motion rule should remove the movement while retaining the link cue. One practical approach is to make the underline visible in the reduced-motion state and disable its transition, rather than leaving a line hidden until hover. This is especially useful for a decorative underline that otherwise depends on a scale transform.
Check the rule in the browser’s accessibility or emulation settings, not only by reading the stylesheet. Confirm that the keyboard focus outline is still visible, the link remains identifiable on touch devices, and the change does not cause surrounding text to shift.
Troubleshoot Common Underline Problems
The line appears behind or outside the link
Check the containing element’s positioning context and the pseudo-element’s width, bottom offset, and stacking order. Ensure the link has enough space for the underline and that an ancestor is not clipping it with overflow. If another decoration competes with the line, simplify the styles and test one effect at a time.
The animation shifts text or does not run
A width animation can alter layout and make text appear to jump. Prefer transform when the goal is a visual reveal without resizing the link. Also check that the pseudo-element has a content declaration, that the transition property includes transform, and that the interactive selector matches the actual anchor element.
The old dashed effect looks like a moving border
The original sample animates a repeating gradient across a long duration, so it produces a traveling dashed or dotted pattern rather than a single underline that grows into view. Keep it when that moving pattern is intentional. For a conventional link underline, use a solid pseudo-element or native text decoration and select the approach that matches the visual result you want.
CSS Underline Animation: Pick the Right Technique
Use native text decoration for reading text
For links inside a paragraph, a native underline is often the most robust choice because it follows wrapped lines and remains visible without hover. If you want a decorative reveal for a short navigation link, a pseudo-element offers more control over thickness, placement, and direction. Choose the technique based on the content: a title or menu item can support a bespoke effect, while a long text link should remain easy to read and identify.
Check the real rendered layout
Preview the effect with the actual font, font size, and line height used by the site. A style that looks aligned in a short demo may collide with descenders or adjacent links in a dense navigation row. Test at narrow viewport widths and with browser zoom, too. If text wraps or scales, make sure the line still sits with the intended link and does not become a confusing underline beneath the next line of text.
Keep the effect decorative and lightweight
An underline is a visual cue, not extra content. Use pseudo-elements for the line rather than placing important words or instructions in generated content. Avoid animating an infinite moving stripe for a routine link; a brief transition on hover and keyboard focus is easier to follow and less distracting. If a decorative style fails to load, the link should still work and remain recognizable.
For a final CSS underline animation check, verify four states: the default link, pointer hover, keyboard focus, and reduced-motion preference. The same link should be understandable in each state, and the line should not shift surrounding text. This small review catches most issues before the style is reused across a site.

Ok, now the animated underline code is completed.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Animated Underline</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<style>
.animated_underline {
background-image: linear-gradient(
to right,
silver 50%,
transparent 0%
);
background-size: 6px 4px;
background-repeat: repeat-x;
background-position: 0% bottom;
animation-name: border-dance;
animation-duration: 24s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
@keyframes border-dance {
from {
background-position: 0% bottom;
}
to {
background-position: 100% bottom;
}
}
</style>
</head>
<body>
<div>
<h2>What is Dummy Text?</h2>
<p><strong class="animated_underline">Dummy Text</strong> is simply dummy text of the printing and typesetting industry. Dummy Text has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Dummy Text passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Dummy Text.</p>
</div>
</body>
</html>
You can download the above example from here:




Write a comment