Hover effects on inline links add polish to a page, but each of the six techniques below leans on a different CSS mechanism — box shadows, pseudo-elements, gradients, clipping. The examples target mouse users, though it can be worth pairing :hover with :focus, since not every interaction comes from a pointer.

Sliding highlight

This one paints a box shadow onto the inline link and shifts the text color along with it. Padding is added on all sides, then canceled out with a negative margin of the same value so the padding doesn't disturb text flow. The background is drawn with box-shadow rather than the background property specifically because the former can be transitioned.

a {
  box-shadow: inset 0 0 0 0 #54b3d6;
  color: #54b3d6;
  margin: 0 -.25rem;
  padding: 0 .25rem;
  transition: color .3s ease-in-out, box-shadow .3s ease-in-out;
}
a:hover {
  box-shadow: inset 100px 0 0 0 #54b3d6;
  color: white;
}

Text swap

Hovering slides the original link text out while replacement text slides in — a fair amount of trickery, with the key piece being a data attribute that supplies the incoming text through the content property on the link's ::after pseudo-element.

The markup is a paragraph holding a link and a span:

<p>Hover <a href="#"><span>get a link</span></a></p>

The link itself gets relative positioning so the absolutely positioned pseudo-elements stay anchored to it, inline-block display so box styling applies, and hidden overflow to contain the pseudo-elements:

a {
  overflow: hidden;
  position: relative;
  display: inline-block;
}

Both pseudo-elements are absolutely positioned so they stack with the real link, then set to the link's full width with a zero left offset — the setup for the sliding motion:

a::before,
a::after {
 content: '';
  position: absolute;
  width: 100%;
  left: 0;
}

The ::after content comes from the data attribute:

a::after {
  content: attr(data-replace);
}

By default it is pushed right by transform: translate3d() at 200% and returns to place on :hover. Its top offset is set to zero, which matters for the next step:

a::after {
  content: attr(data-replace);
  top: 0;
  transform-origin: 100% 50%;
  transform: translate3d(200%, 0, 0);
}

a:hover::after,
a:focus::after {
  transform: translate3d(0, 0, 0);
}

The ::before element is scaled down to act as a hidden underline, then scaled back up on hover. At roughly 2px tall and pinned to the bottom, it reads as an underline on the swapped-in text:

a::before {
  background-color: #54b3d6;
  height: 2px;
  bottom: 0;
  transform-origin: 100% 50%;
  transform: scaleX(0);
}

a:hover::before,
a:focus::before {
  transform-origin: 0% 50%;
  transform: scaleX(1);
}

What remains — transitions on the transforms, colors, and so on — is entirely a matter of preference.

View full CSS
a {
  overflow: hidden;
  position: relative;
  display: inline-block;
}

a::before,
a::after {
 content: '';
  position: absolute;
  width: 100%;
  left: 0;
}
a::before {
  background-color: #54b3d6;
  height: 2px;
  bottom: 0;
  transform-origin: 100% 50%;
  transform: scaleX(0);
  transition: transform .3s cubic-bezier(0.76, 0, 0.24, 1);
}
a::after {
  content: attr(data-replace);
  height: 100%;
  top: 0;
  transform-origin: 100% 50%;
  transform: translate3d(200%, 0, 0);
  transition: transform .3s cubic-bezier(0.76, 0, 0.24, 1);
  color: #54b3d6;
}

a:hover::before {
  transform-origin: 0% 50%;
  transform: scaleX(1);
}
a:hover::after {
  transform: translate3d(0, 0, 0);
}

a span {
  display: inline-block;
  transition: transform .3s cubic-bezier(0.76, 0, 0.24, 1);
}

a:hover span {
  transform: translate3d(-200%, 0, 0);
}

Growing background

A widely seen pattern: the link's ::before pseudo-element serves as a thick underline tucked slightly behind the text, and on hover it expands to fill the whole link. Base styles remove text-decoration (the pseudo-element replaces it) and add relative positioning to hold the absolutely positioned ::before:

a {
  text-decoration: none;
  position: relative;
}

The pseudo-element starts around 8px tall so it resembles a chunky underline. Absolute positioning lets it span the link's full width, offset to left and nudged just off the bottom so it looks like a subtle highlight. z-index: -1 keeps it behind the link:

a::before {
  content: '';
  background-color: hsla(196, 61%, 58%, .75);
  position: absolute;
  left: 0;
  bottom: 3px;
  width: 100%;
  height: 8px;
  z-index: -1;
}

The growth comes from changing the height from 3px to 100%, with the bottom offset dropping to zero so the background covers more area as it expands:

a:hover::before {
  bottom: 0;
  height: 100%;
}

A short transition smooths the change:

a::before {
  content: '';
  background-color: hsla(196, 61%, 58%, .75);
  position: absolute;
  left: 0;
  bottom: 3px;
  width: 100%;
  height: 8px;
  z-index: -1;
  transition: all .3s ease-in-out;
}
View full CSS
a {
  text-decoration: none;
  color: #18272F;
  font-weight: 700;
  position: relative;
}

a::before {
  content: '';
  background-color: hsla(196, 61%, 58%, .75);
  position: absolute;
  left: 0;
  bottom: 3px;
  width: 100%;
  height: 8px;
  z-index: -1;
  transition: all .3s ease-in-out;
}

a:hover::before {
  bottom: 0;
  height: 100%;
}

Right-to-left color swap

A good fit for navigation links. The link begins in one color with no underline; on hover, a second color slides in from the right while an underline arrives from the left. Because there's a lot of motion, the accessibility angle is worth considering — wrapping the effect in a prefers-reduced-motion query and substituting something calmer for users with motion sensitivities.

The link gets a linear background gradient with a hard stop between two colors at the halfway point:

a {
  background-image: linear-gradient(
    to right,
    #54b3d6,
    #54b3d6 50%,
    #000 50%
  );
}

The background is set to twice the link's width, or 200%, and positioned fully to the left so only one of the gradient's two colors shows:

a {
  background-image: linear-gradient(
    to right,
    #54b3d6,
    #54b3d6 50%,
    #000 50%
  );
  background-size: 200% 100%;
  background-position: -100%;
}

Two non-standard -webkit-prefixed properties do the heavy lifting here: one strips the text color to transparent, the other clips the background gradient to the text so the text appears to take on the background's color:

a {
  background-image: linear-gradient(
    to right,
    #54b3d6,
    #54b3d6 50%,
    #000 50%
  );
  background-size: 200% 100%;
  background-position: -100%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

The faux underline is again built from ::before, colored to match the hidden half of the gradient and placed beneath the link so it mimics text-decoration: underline:

a:before {
  content: '';
  background: #54b3d6;
  display: block;
  position: absolute;
  bottom: -3px;
  left: 0;
  width: 0;
  height: 3px;
}

On hover it slides into place from the left:

a:hover {
 background-position: 0;
}

One subtlety: the pseudo-element is made 100% of the link's width on hover. Applying that directly to the link's hover state would stretch the link itself to full width and push it around the layout:

a:hover::before {
  width: 100%;
}

A transition softens the whole thing:

a {
  background-image: linear-gradient(
    to right,
    #54b3d6,
    #54b3d6 50%,
    #000 50%
  );
  background-size: 200% 100%;
  background-position: -100%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  transition: all 0.3s ease-in-out;
}
View full CSS
a {
  background-image: linear-gradient(
    to right,
    #54b3d6,
    #54b3d6 50%,
    #000 50%
  );
  background-size: 200% 100%;
  background-position: -100%;
  display: inline-block;
  padding: 5px 0;
  position: relative;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  transition: all 0.3s ease-in-out;
}

a:before {
  content: '';
  background: #54b3d6;
  display: block;
  position: absolute;
  bottom: -3px;
  left: 0;
  width: 0;
  height: 3px;
  transition: all 0.3s ease-in-out;
}

a:hover {
 background-position: 0;
}

a:hover::before {
  width:100%;
}

Rainbow underline

There's no text-decoration-color: rainbow, but linear gradients layered on background can fake it. Start by removing the link's text-decoration:

a {
  text-decoration: none;
}

Chain two linear gradients on the same background property — the initial color, and the rainbow revealed on hover:

a {
  background:
    linear-gradient(
      to right,
      rgba(100, 200, 200, 1),
      rgba(100, 200, 200, 1)
    ),
    linear-gradient(
      to right,
      rgba(255, 0, 0, 1),
      rgba(255, 0, 180, 1),
      rgba(0, 100, 200, 1)
  );
}

Set the background size to just 3px tall so it looks like an underline, sizing both gradients together: the initial gradient at full width, the rainbow at zero width:

a {
  background:
    linear-gradient(
      to right,
      rgba(100, 200, 200, 1),
      rgba(100, 200, 200, 1)
    ),
    linear-gradient(
      to right,
      rgba(255, 0, 0, 1),
      rgba(255, 0, 180, 1),
      rgba(0, 100, 200, 1)
  );
  background-size: 100% 3px, 0 3px;
}

Then position both gradients on the same background-position property so the first sits fully in view while the rainbow is pushed out of sight. The background should not repeat:

a {
  background:
    linear-gradient(
      to right,
      rgba(100, 200, 200, 1),
      rgba(100, 200, 200, 1)
    ),
    linear-gradient(
      to right,
      rgba(255, 0, 0, 1),
      rgba(255, 0, 180, 1),
      rgba(0, 100, 200, 1)
  );
  background-size: 100% 3px, 0 3px;
  background-position: 100% 100%, 0 100%;
  background-repeat: no-repeat;
}

On hover, the gradients swap size values:

a:hover {
  background-size: 0 3px, 100% 3px;
}

And a transition carries the swap:

a {
  background:
    linear-gradient(
      to right,
      rgba(100, 200, 200, 1),
      rgba(100, 200, 200, 1)
    ),
    linear-gradient(
      to right,
      rgba(255, 0, 0, 1),
      rgba(255, 0, 180, 1),
      rgba(0, 100, 200, 1)
  );
  background-size: 100% 3px, 0 3px;
  background-position: 100% 100%, 0 100%;
  background-repeat: no-repeat;
  transition: background-size 400ms;
}

Passing underline

Geoff Graham previously broke down a similar hover effect from Adam Argyle: a background color enters from the left behind the link and exits to the right when the mouse leaves. This version trims the background down so it reads more like an underline.

a {
  position: relative;
}

a::before {
    content: '';
    position: absolute;
    width: 100%;
    height: 4px;
    border-radius: 4px;
    background-color: #18272F;
    bottom: 0;
    left: 0;
    transform-origin: right;
    transform: scaleX(0);
    transition: transform .3s ease-in-out;
  }

a:hover::before {
  transform-origin: left;
  transform: scaleX(1);
}

It isn't the only route to the effect — Justin Wong achieves it with background instead. Geoff has also assembled a roundup of CSS link hover effects ranging from neat to absurd, and for fundamentals there's Philip Zastrow's DigitalOcean tutorial on styling links and buttons with state pseudo-classes.