
A short underline that extends when a link receives attention can give navigation a little personality. Use a pseudo-element so you can control the line’s thickness, position, and direction separately from the text. This pattern is designed for short standalone links, not every link on a page.
Change the CSS below. The preview updates as you type. Your edits stay in this browser tab.
Hover or Tab to the links. Try another transform-origin and underline color.
HTML for this example
<nav class="underline-demo" aria-label="Example navigation"><a href="#underline-note">Explore our work</a><a href="#underline-note">Meet the studio</a></nav><p id="underline-note" class="example-note">Demo links. Keep ordinary underlines on links inside paragraphs.</p>
Scale the line from one side
The ::after layer spans the link’s width. It starts at scaleX(.18), leaving a small visible cue, and grows to full width on hover or keyboard focus. transform-origin: left anchors that growth. Try center for a line that expands in both directions, or right for the opposite emphasis.
Keep the focus indicator distinct
An animated line can be subtle, so keyboard focus also gets a clear outline. Touch devices show the complete underline without waiting for a hover state. Reduced-motion preferences keep the state change but remove its transition. Change --line only after checking that the indicator remains visible against the background.
Choose the right place to use it
This single pseudo-element draws one line beneath the link box. It does not follow every line of a wrapped paragraph link. Keep native text underlines for body copy, and test longer navigation labels at phone widths and increased text sizes. If a label wraps awkwardly, simplify the treatment rather than forcing the text into a fixed-width box.
Reference: MDN on transform-origin.
Have a client website that needs this kind of attention? See our WordPress development for agencies and case studies.


Leave a Reply