CSS Portal

CSS Animated Underline Links

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

What we're building

Links are usually shown with a plain underline. With a little CSS animation, you can make that underline grow, slide or thicken when someone hovers over the link, a small touch that makes a site feel polished and responsive.

In this tutorial we'll build the classic animated underline step by step, then look at smoother and more flexible versions. Hover over the link below, or press Tab to reach it with the keyboard:

Step 1: removing the default underline

First, we remove the browser's built-in underline with the text-decoration property, and set position to relative so we can place our own line exactly where we want it:

.fancy-link {
  position: relative;
  color: #1d4ed8;
  text-decoration: none;
}

We're using a class, .fancy-link, rather than styling every a on the page. As we'll see at the end, removing underlines from all links can make them hard to spot.

Step 2: drawing the new line

Next, we draw a new underline with the ::after pseudo-element, an extra box that CSS adds inside the link, after its text:

.fancy-link::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: -2px;
  width: 0;
  height: 2px;
  background: currentColor;
  transition: width 0.4s ease;
}
LineWhat it does
content: "";Creates the pseudo-element; it's empty, because we only want a shape
position: absolute;Places the line relative to the link, without affecting the text
left: 0; and bottom: -2px;Starts the line at the left edge, just below the text
width: 0;Starts with no width, so the line is invisible
height: 2px; and backgroundA 2px tall bar filled with currentColor, the link's own text colour
transitionAnimates any change in width over 0.4 seconds

What changed since the old version? The original drew the line with border-bottom and a fixed colour, #3366FF. Using a background with currentColor means the line automatically matches the link, even if you change the link colour on hover or in dark mode.

Step 3: animating it on hover

Finally, when someone hovers over the link, the line grows to full width. Because of the transition, it grows smoothly instead of appearing instantly, and shrinks back when the pointer moves away:

.fancy-link:hover::after,
.fancy-link:focus-visible::after {
  width: 100%;
}

Adding :focus-visible means keyboard users see the same effect when they Tab to the link, not just mouse users.

A smoother version, in any direction

Animating width works, but it makes the browser recalculate the layout on every frame of the animation. A smoother approach keeps the line at full width and squashes it with transform: scaleX(), which the browser can animate very efficiently:

.fancy-link::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: -2px;
  width: 100%;
  height: 2px;
  background: currentColor;
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.3s ease;
}

.fancy-link:hover::after,
.fancy-link:focus-visible::after {
  transform: scaleX(1);
}

scaleX(0) squashes the line to nothing; scaleX(1) returns it to full size. The transform-origin decides which point it grows from, so changing one value changes the direction:

transform-originEffect
leftGrows from left to right
rightGrows from right to left
centerGrows outwards from the middle

The slide-through effect

The last link above has a neat trick: the line enters from the left and leaves to the right, as if it's passing through. It works by swapping the transform-origin between the normal and hover states:

.fancy-link::after {
  transform-origin: right;  /* shrinks away towards the right */
}
.fancy-link:hover::after,
.fancy-link:focus-visible::after {
  transform-origin: left;   /* grows in from the left */
}

Underlines that wrap across lines

The pseudo-element method has one weakness: if a long link wraps onto two lines, the line only appears under one part of it. For links inside paragraphs, a background gradient works better, because backgrounds follow the text across line breaks:

.fancy-link {
  text-decoration: none;
  background: linear-gradient(currentColor, currentColor) no-repeat left bottom / 0% 2px;
  transition: background-size 0.5s ease;
}

.fancy-link:hover,
.fancy-link:focus-visible {
  background-size: 100% 2px;
}
  • linear-gradient(currentColor, currentColor) is a solid block of the link colour that can be used as a background image.
  • left bottom / 0% 2px places it at the bottom left, zero width and 2px tall.
  • On hover, the width grows to 100%, drawing the line across each line of text in turn.

Tip: no position: relative or pseudo-element is needed with this method, so it's also the simplest to add to existing links.

Animating a real underline

Modern CSS also lets you style the browser's own underline, and even animate it. Instead of removing the underline and drawing a new one, you can keep it and change its colour, thickness and distance from the text:

.fancy-link {
  text-decoration: underline;
  text-decoration-color: rgb(29 78 216 / 0.35);
  text-decoration-thickness: 1px;
  text-underline-offset: 0.2em;
  transition: text-decoration-color 0.25s ease,
              text-decoration-thickness 0.25s ease,
              text-underline-offset 0.25s ease;
}

.fancy-link:hover,
.fancy-link:focus-visible {
  text-decoration-color: currentColor;
  text-decoration-thickness: 3px;
  text-underline-offset: 0.3em;
}
PropertyWhat it controls
text-decoration-colorThe underline's colour, separately from the text
text-decoration-thicknessHow thick the underline is
text-underline-offsetHow far below the text it sits

A real underline also skips neatly around letters that dip below the line, such as g, p and y, which looks tidy and is easy to read.

Accessibility and the complete code

Keep links easy to spot

When the underline only appears on hover, links in a paragraph can look just like normal text, especially for people who can't easily tell colours apart. Accessibility guidelines say links shouldn't be identified by colour alone.

  • Inside paragraphs: keep a visible underline, and animate it to become bolder on hover, as in the last example.
  • In menus and navigation bars: where it's obvious everything is a link, the grow-on-hover effects work well.

Respect reduced motion

@media (prefers-reduced-motion: reduce) {
  .fancy-link,
  .fancy-link::after {
    transition: none;
  }
}

The complete code

Here's the smooth, transform-based version, ready to copy:

.fancy-link {
  position: relative;
  color: #1d4ed8;
  text-decoration: none;
}

.fancy-link::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: -2px;
  width: 100%;
  height: 2px;
  background: currentColor;
  transform: scaleX(0);
  transform-origin: left;  /* try right or center */
  transition: transform 0.3s ease;
}

.fancy-link:hover::after,
.fancy-link:focus-visible::after {
  transform: scaleX(1);
}

.fancy-link:focus-visible {
  outline: 3px solid #f59e0b;
  outline-offset: 4px;
}

@media (prefers-reduced-motion: reduce) {
  .fancy-link::after { transition: none; }
}

Wrapping up

An animated underline takes just a few lines of CSS. Draw the line with ::after and animate it with transform: scaleX(), change transform-origin to set the direction, use a background gradient for links that wrap, or animate the real underline for links in body text. Whichever you choose, include :focus-visible so keyboard users enjoy the effect too.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee