Link Underlines That Animate Into Block Backgrounds
Publikováno: 23.9.2019
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:
A journey:
- The Superfriendly site does it with
box-shadow
. Turningbox-shadow: inset 0 -0.07em 0 #0078d6;
intobox-shadow: inset 0 -0.85em 0
The post Link Underlines That Animate Into Block Backgrounds appeared first on CSS-Tricks.
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:
A journey:
- The Superfriendly site does it with
box-shadow
. Turningbox-shadow: inset 0 -0.07em 0 #0078d6;
intobox-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
scaleY
or something. Here's that kind of idea. Your enemy here is going to be links that break onto new lines, whichbox-shadow
seems to handle more elegantly. - Another idea would be using
linear-gradient
with 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 itsbackground-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-thickness
/ text-decoration-offset
/ text-decoration-color
with a transition is a better option for aesthetics, control, and being able to understand the code at first glance.
The post Link Underlines That Animate Into Block Backgrounds appeared first on CSS-Tricks.