An underline draws in from the left on hover and retreats the same way. The line is a pseudo-element scaled on X, which is smoother and cheaper than animating width.
Also known as: animated underline, underline from left, link hover underline.
<a class="hfx-link-underline-left" href="#">Read the docs</a>
.hfx-link-underline-left {
--c: var(--hfx-color, #3b82f6);
font-size: var(--hfx-size, 16px);
position: relative;
display: inline-block;
color: var(--c);
font: 600 1em system-ui, sans-serif;
text-decoration: none;
}
.hfx-link-underline-left::after {
content: "";
position: absolute;
left: 0;
bottom: -0.15em;
width: 100%;
height: 2px;
background: var(--c);
transform: scaleX(0);
transform-origin: left;
transition: transform 0.3s ease;
}
.hfx-link-underline-left:hover::after { transform: scaleX(1); }
@media (prefers-reduced-motion: reduce) {
.hfx-link-underline-left, .hfx-link-underline-left *, .hfx-link-underline-left::before, .hfx-link-underline-left::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}The same effect using Tailwind utility classes. No custom CSS or config needed.
<a href="#" class="relative font-semibold text-blue-500 after:absolute after:-bottom-0.5 after:left-0 after:h-0.5 after:w-full after:origin-left after:scale-x-0 after:bg-blue-500 after:transition-transform after:duration-300 hover:after:scale-x-100"> Read the docs </a>
--hfx-color on the element or a parent.
Currently previewing #3b82f6.--hfx-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 16px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.