Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate a link underline with border-bottom, so that there is space between the link text and the underline?

Tags:

css

How could I animate the link underline with using border-bottom, so that there is space between the text and the underline?

I know how to do it in the following way, so that the default text-decoration element is animated. But I would like to have space between the link and the underline, that is why I think I need to use border-bottom. But I can't get the border-bottom work with the transition animation. How could I do this? I tried looking for other solutions, but couldn't find any. Thanks!

h2 > a {
  position: relative;
  color: #000;
  text-decoration: none;
}

h2 > a:before {
  content: "";
  position: absolute;
  width: 100%;
  height: 2px;
  bottom: 0;
  left: 0;
  background-color: #000;
  visibility: hidden;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transition: all 0.3s ease-in-out 0s;
  transition: all 0.3s ease-in-out 0s;
}

h2 > a:hover:before {
  visibility: visible;
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}
like image 249
userden Avatar asked Nov 29 '22 09:11

userden


2 Answers

you can fake an animated border via background and background-size:

a {
  padding-bottom: 5px;
  /* set here size + gap size from text */
  background: linear-gradient(0deg, currentcolor, currentcolor) bottom center no-repeat;
  background-size: 0px 3px;
  transition: 0.5s;
  text-decoration: none;
}

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

a[class] {
  color: gray;
}

a.tst {
  color: purple;
  background: linear-gradient(0deg, currentcolor, currentcolor) bottom center no-repeat, linear-gradient(0deg, turquoise, turquoise) center calc(100% - 2px) no-repeat;
  background-size: 0px 2px;
}

a.tst:hover {
  background-size: 100% 2px;
}
<a href>kake animated border</a>
<a href class> why currentcolor ?</a>
<a href class="tst">mix of colors ?</a>
like image 149
G-Cyrillus Avatar answered Jan 02 '23 22:01

G-Cyrillus


The code you've presented uses a pseudo-element not the default text-decoration. Since the pseudo element is positioned absolutely, you can change the distance easily. Change the a:before bottom to -5px or whatever negative value fits the distance that you want:

a {
  position: relative;
  color: #000;
  text-decoration: none;
}

a:before {
  content: "";
  position: absolute;
  width: 100%;
  height: 2px;
  bottom: -5px;
  left: 0;
  background-color: #000;
  visibility: hidden;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transition: all 0.3s ease-in-out 0s;
  transition: all 0.3s ease-in-out 0s;
}

a:hover:before {
  visibility: visible;
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}
<a href="#">Long long text</a>
like image 44
Ori Drori Avatar answered Jan 02 '23 23:01

Ori Drori