Modern CSS Link Hover Effects: Animated Underlines, Strikethroughs & Magnetic Micro-Interactions
To create the popular left-to-right animated underline in modern CSS, set background: linear-gradient(currentColor, currentColor) no-repeat 0 100% / 0% 2px on the anchor tag and transition background-size 0.3s cubic-bezier(0.16, 1, 0.3, 1) to 100% 2px on :hover and :focus-visible. This outperforms older border-bottom hacks by preserving sub-pixel anti-aliasing and allowing precise vertical gap control with text-underline-offset.
Step 1: The Left-to-Right Expanding Underline via CSS Backgrounds
Traditional CSS underlines defined via text-decoration: underline cannot be animated smoothly in older browsers. The modern front-end standard uses a CSS linear gradient as the background image, positioned at the bottom left of the anchor tag with a width of 0%. On hover or keyboard focus, the background width scales to 100%, creating a smooth horizontal wipe effect without layout recalculations.
.nav-link {
color: var(--text-primary);
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.35s cubic-bezier(0.16, 1, 0.3, 1);
padding-bottom: 2px;
}
.nav-link:hover,
.nav-link:focus-visible {
background-size: 100% 2px;
}
Step 2: Modern Native Properties: text-decoration-thickness and text-underline-offset
Modern CSS now provides first-class properties for native text decoration. Rather than hacking borders or pseudo-elements, you can use text-underline-offset to pull the line away from descending characters like 'g', 'y', and 'p', and text-decoration-thickness to set fractional sub-pixel widths.
.article-link {
color: #38bdf8;
text-decoration: underline;
text-decoration-color: rgba(56, 189, 248, 0.4);
text-decoration-thickness: 1.5px;
text-underline-offset: 4px;
transition: text-decoration-color 0.2s ease, text-underline-offset 0.2s ease;
}
.article-link:hover {
text-decoration-color: #38bdf8;
text-underline-offset: 7px;
}
Step 3: Interactive Pill Expansion & Ghost Highlights
For navigation links and sidebar menus, a full background fill creates a punchy, tactile interface. By combining a subtle translucent background with an offset box-shadow or pill border, the link signals clickable depth instantaneously.
.tab-link {
position: relative;
display: inline-flex;
align-items: center;
padding: 6px 14px;
border-radius: 9999px;
color: var(--text-secondary);
text-decoration: none;
background: transparent;
transition: background 0.25s cubic-bezier(0.16, 1, 0.3, 1),
color 0.25s ease,
transform 0.25s ease;
}
.tab-link:hover {
color: #fff;
background: rgba(108, 99, 255, 0.15);
transform: translateY(-1px);
}
Step 4: External Link Indicators & Keyboard Accessibility
Good UX demands that external links warn users that they will navigate away from the current domain. Using an inline SVG mask or CSS pseudo-element with :not([href^='#']) automatically appends an external arrow icon that animates upward and to the right on hover, while :focus-visible guarantees keyboard users never lose their place.
.external-link {
display: inline-flex;
align-items: center;
gap: 4px;
color: var(--accent);
}
.external-link::after {
content: "↗";
display: inline-block;
font-size: 0.9em;
transition: transform 0.2s ease;
}
.external-link:hover::after,
.external-link:focus-visible::after {
transform: translate(2px, -2px);
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
How do you animate an underline from left to right in CSS?
Set 'background-image: linear-gradient(currentColor, currentColor)' with 'background-position: 0 100%', 'background-repeat: no-repeat', and 'background-size: 0% 2px'. On :hover or :focus-visible, transition 'background-size' to '100% 2px' using a cubic-bezier timing function.
Can you animate text-decoration directly in CSS?
Modern browsers support transitions on 'text-underline-offset' and 'text-decoration-color'. However, smooth continuous scaling of underline width from 0% to 100% still requires using the CSS background-size trick or pseudo-elements.
How do you prevent link underlines from crossing descender characters (p, y, g)?
Declare 'text-decoration-skip-ink: auto;' on the anchor tag. This is enabled by default in modern browsers and automatically leaves clean optical gaps around letter tails.
What is the difference between text-underline-offset and border-bottom for links?
'text-underline-offset' is rendered natively by the text layout engine, naturally respecting multi-line text wraps and font descenders. 'border-bottom' treats the link as an inline box, which can collide with line-height and trigger unintended baseline shifts.
How do you ensure custom link hover effects are accessible to keyboard users?
Always pair every ':hover' pseudo-class selector with ':focus-visible' (e.g. '.link:hover, .link:focus-visible'). Never set 'outline: none' without providing an equivalent high-contrast visual focus indicator.