It’s a cool little effect. The default link style has an underline (which is a good idea) and then on
:hover you see the underline essentially thicken up turning into almost what it would have looked liked if you used a
background-color on the link instead.
Here’s an example of the effect on the Superfriendly site:
- The Superfriendly site does it with
box-shadow: inset 0 -0.07em 0 #0078d6;into
box-shadow: inset 0 -0.85em 0 #a2d5fe;with a transition. Andres Cuervo ported that idea to a Pen. (I forked it to fix the “start offset” idea that was broken-seeming to me on the original).
- You might be tempted to draw the line with a pseudo-element that’s, say, absolutely positioned within the relatively positioned link. Then you animate its height or
scaleYor something. Here’s that kind of idea. Your enemy here is going to be links that break onto new lines, which
box-shadowseems to handle more elegantly.
- Another idea would be using
linear-gradientwith hard color stops to kinda “fake” the drawing of a line that’s positioned to look like an underline. Then the gradient can be animated to cover the element on hover, probably by moving its
background-position. Here’s that kind of idea and another example we wrote up a little while back. This handles line breaks nicer than the previous method.
- The default
text-decoration: underline;has a distinct advantage these days:
text-decoration-skip-ink! It has become the default behavior for links to have the underlines deftly skip around the decenders in text, making for much nicer looking underlines than any of these techniques (also: borders) can pull off. There are properties that are somewhat new that you may not be aware of that give you more control over the underline that we have traditionally had, like text-decoration-color. But there is more, like thickness and offset, that make this effect possible! Miriam Suzanne has a demo of exactly this, which only works in Firefox Nightly at the moment, but should be making the rounds soon enough.
Summary: If you need to do this effect right now in the most browsers you can, the
box-shadow technique is probably best. If it’s just an enhancement that can wait a bit, using
text-decoration-color with a transition is a better option for aesthetics, control, and being able to understand the code at first glance.
Amazing will diffenetly use this
Hackernoon does this as well, I think. I had been meaning to look at how they did this – now I don’t have to! Thanks for the write up and examples.
Super nice effect.. wouldn’t it be better to use em’s in this case? To make the height of the box relative to the actual text element instead of the root font-size.