Differentiating a Hovered Link's ::after Pseudo Element

Here's a small piece of CSS - simple, but posting here for posterity - to add a different style to a hovered/focussed link's ::after pseudo element.

I needed to create a link with an arrow after it, something like this a link >>. But, by default when you hover a link, whatever styling is applied to it will also apply to the after item, so if there was a hover state, it might look like this: a link >>. I didn't want the arrows underlined, I wanted something like this: a link >>, so added this CSS.

1) Set up the basic style for our "styled" anchor/link. This also sets a transition of .3 of a second, so when it goes to bold on hover, there is a smooth transition.

a.styled {
  color: $c-primary;
  display: block;
  transition: 0.3s;
}

2) Add the arrows after the link.
 

a.styled::after {
  content: ">>";
  margin-left: 5px;
  letter-spacing: -2px;
  transition: 0.3s;
}

3) Set the hover state for the link
 

a.styled:focus,
a.styled:hover {
  color: $c-black;
  font-weight: bold;
  text-decoration: underline;
}

4) Set the hover state for the ::after pseudo element, including setting the margin-left to 8px so the arrows move to the right slightly when the link is hovered.

a.styled:focus::after,
a.styled:hover::after {
  content: ">>";
  display: inline-block;
  margin-left: 8px;
  text-decoration: none;
}


 

Here's a CodePen of it:

See the Pen Differentiating a Hovered Link's ::after Pseudo Element by Mark Conroy (@markconroy) on CodePen.

Filed Under:

  1. CSS