If you’ve ever animated width or border-bottom for a hover effect, you’ve probably noticed the frame rate tanking way below 60 FPS. What’s happening under the hood is that the browser is forced into a heavy layout recalculation on every single frame. Swap that out for transform: scaleX on a pseudo-element, though, and the browser offloads the whole thing to the GPU without breaking a sweat.
Performance Target: Rock-solid 60 FPS (handled right on the GPU)
Single-Line Text: Go with
transform: scaleXMulti-Line Text: Reach for
background-sizeModern Native Method: Use
text-decoration-thicknessAccessibility Must-Have: Always support
prefers-reduced-motion
Why Transform Beats Width for Underline Animations
When you animate width, you’re essentially forcing the browser to recalculate the entire page geometry frame by frame. That layout thrashing is what causes those annoying micro-stutters, and it’s especially brutal on budget mobile chips that don’t have CPU power to burn. Changing actual physical dimensions on the fly is just inherently expensive.
Using transform dodges that bottleneck entirely by shipping the animation straight to your graphics card. The main thread stays completely untouched and responsive. The result is butter-smooth 60 FPS motion, simply because the browser is only transforming a visual layer rather than figuring out where everything sits on the page.
The ScaleX Technique for Single-Line Text
Sticking a pseudo-element on the link and scaling it horizontally is pretty much the go-to pattern for single-line hover states. All you really need is an absolutely positioned ::after element sitting directly underneath your text.
Center-Out Expanding Line
Setting transform-origin: center makes the line expand outward from the middle in both directions. It feels balanced, looks polished, and works especially well on primary navbar links.
HTML
Our ServicesCSS
.underline-center {
position: relative;
display: inline-block;
text-decoration: none;
padding-bottom: 4px;
}
.underline-center::after {
content: '';
position: absolute;
width: 100%;
height: 2px;
bottom: 0;
left: 0;
background-color: currentColor;
transform: scaleX(0);
transform-origin: center;
transition: transform 0.3s ease-out;
}
.underline-center:hover::after,
.underline-center:focus-visible::after {
transform: scaleX(1);
}Bi-Directional Wipe Effect
If you want something a bit more dynamic, tweak the transform-origin dynamically on hover. That gives you a slick directional wipe where the line glides in from the left when you hover, then continues out to the right when you leave.
CSS
.underline-wipe::after {
content: '';
position: absolute;
width: 100%;
height: 2px;
bottom: 0;
left: 0;
background-color: currentColor;
transform: scaleX(0);
transform-origin: bottom right;
transition: transform 0.3s ease-out;
}
.underline-wipe:hover::after,
.underline-wipe:focus-visible::after {
transform: scaleX(1);
transform-origin: bottom left;
}The Background-Size Technique for Multi-Line Text
The pseudo-element trick falls flat the moment your link wraps onto two lines, because a pseudo-element only ever draws one rigid rectangular box. Luckily, a basic linear gradient handles multi-line wraps without any weird layout hacks.
HTML
<a href="#" class="underline-multiline">
Using modern CSS animation techniques
is critically important for user experience.
</a>CSS
.underline-multiline {
text-decoration: none;
background-image: linear-gradient(currentColor, currentColor);
background-position: 0% 100%;
background-repeat: no-repeat;
background-size: 0% 2px;
transition: background-size 0.3s ease-in-out;
}
.underline-multiline:hover,
.underline-multiline:focus-visible {
background-size: 100% 2px;
}Because the background naturally follows the inline flow of the text, it wraps cleanly across line breaks without breaking a sweat. You get exact control over the line's thickness and color while keeping the DOM tidy. This is actually the exact pattern most mature design systems lean on for body copy links.
Modern Alternatives: text-decoration Properties
Using text-underline-offset and text-decoration-thickness
Modern native CSS text decoration gives us fine-grained styling power without needing extra markup or pseudo-elements. You can dial in the exact pixel gap between your baseline and the line with text-underline-offset, then animate the color or line thickness directly.
CSS
.underline-native {
text-decoration: underline;
text-decoration-color: transparent;
text-underline-offset: 4px;
text-decoration-thickness: 2px;
transition: text-decoration-color 0.3s ease;
}
.underline-native:hover,
.underline-native:focus-visible {
text-decoration-color: currentColor;
}Another huge plus here is how naturally the browser handles descenders. The underline automatically breaks around letters like p, g, and y so it doesn’t collide with your typography, and it does that straight out of the box.
Transform vs. Modern CSS: Which One to Choose?
If raw animation smoothness is your top priority, stick with the transform approach - it’s still the battle-tested, cross-browser champion for buttery-smooth motion. On the other hand, if you want clean markup, less CSS overhead, and natural descender skipping, native text-decoration properties are well worth using.
Both methods drop right into modern setups like custom color systems you build in Tailwind CSS v4; just hook up your custom properties directly to the color values.
Essential Accessibility Implementations
Disabling Animations with prefers-reduced-motion
Vestibular disorders and motion sensitivity are very real concerns for plenty of users. You can respect their preferences by wrapping your transitions inside a @media (prefers-reduced-motion: reduce) block to kill the scaling animation outright. The underline simply snaps into place instantly instead.
CSS
@media (prefers-reduced-motion: reduce) {
.underline-center::after,
.underline-wipe::after {
transition: none;
}
.underline-multiline {
transition: none;
}
}It’s just a few lines of CSS, requires zero JS, and immediately checks off WCAG 2.1 SC 2.3.3 compliance for anyone who has reduced motion enabled at the system level.
Keyboard Navigation: :focus-visible State
If your styles only account for :hover, anyone navigating with a keyboard is basically left guessing where their cursor is. That’s why you’ll notice our snippets always pair :focus-visible right alongside :hover - it really shouldn’t be treated as an optional nice-to-have. Tabbing through the page triggers the exact same animation, giving keyboard users immediate feedback.
Using :focus-visible instead of standard :focus is the sweet spot because it avoids flashing an outline when someone clicks with a mouse. If you're putting together interactive components, keep this exact pattern in mind when you build a search bar with Tailwind CSS or any other focusable UI piece.
Handling Hover States on Mobile Touch Devices
One super common headache on touchscreens is hover states getting permanently "stuck" after someone taps a link. You can sidestep that entirely by tucking your hover rules inside @media (hover: hover) so they only fire on devices with an actual mouse or pointing device.
CSS
@media (hover: hover) {
.underline-center:hover::after {
transform: scaleX(1);
}
}That way, mobile users get a clean, predictable tap target without the UI hanging on a weird active state afterward.
Troubleshooting Common Underline Animation Bugs
The Underline Covers the Text (z-index Fix)
If your animated underline is showing up on top of your copy instead of beneath it, you just forgot a stacking context fix. Toss a z-index: -1 on the ::after block and it’ll tuck itself neatly behind the text where it belongs.
CSS
.underline-center::after {
/* ... other properties */
z-index: -1;
}The Animation Shifts the Whole Layout (position: relative Fix)
If your pseudo-element is flying off to some random spot on the screen, check the parent link. An absolutely positioned pseudo-element will bubble all the way up to the nearest positioned ancestor (often the body) if you forget to give the anchor tag position: relative.
The Line Does Not Animate on Multi-Line Links
If your link text wraps onto multiple lines and the pseudo-element ends up looking like an awkward detached box, that’s your cue to switch over to background-size. A pseudo-element simply cannot split itself across lines, so no amount of tweaking will fix it - background-size is built for that exact job.
The Future: CSS Houdini Underlines
The CSS Houdini Paint API is gradually giving us lower-level hooks into how browsers actually render pixels. In theory, you can run lightweight JavaScript worklets that draw custom, dynamic underlines that understand every typographic nuance. That said, widespread browser support isn't quite there yet, so it’s more of an exciting tech demo than something to rely on in production today.
For now, grab transform: scaleX for navbars, buttons, and short labels. Switch over to background-size the second you're styling text that could wrap onto multiple lines. Drop in that prefers-reduced-motion media query at the end, and you’re fully set with a resilient, accessible implementation.