Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underline animation on click

I'm trying to recreate the animation on description and review here. The underline animates from right to left when the link is unclicked the underline animates from left to right before it disappears.

a {
  color:#00f;
  text-decoration:none;
  display:inline-block;
}
a:after {
  width: 0;
  display:block;
  background:#00f;
  height:3px;
  transition: all .5s ease-in-out;
  content:"";
}
a:hover {
  color:#00f;
}
a:hover:after {
  width:100%;
}
<a href="#">Click first</a>
<a href="#">Click second</a>
like image 681
sonia maklouf Avatar asked Oct 23 '17 10:10

sonia maklouf


People also ask

How do you underline an animation in CSS?

Use display: inline-block to make the underline span just the width of the text content. Use the :after pseudo-element with width: 100% and position: absolute to place it below the content. Use transform: scaleX(0) to initially hide the pseudo-element.

How do you animate an underline in after effects?

Let's say, “Underline my text.” And place the text at your desired position. As you want to animate your text, put it in the center. Now go to layers, select new, and select solid fill. Besides the color option on the pop-out screen, select the eyedropper button to match it with the color of your text and hit okay.

How do you style an underline?

How to Underline a Title in CSS. To underline a title, you can use text-decoration: underline; but you can make it prettier if you use the border-bottom property. In the latter case, however, you need to add display: inline; so that the underline wouldn't be longer than the word itself.


3 Answers

  1. Use position on the pseudoelement. Add position: relative to a, and position: absolute to :after.

  2. Position the pseudoelement using bottom and right. This means the line originates from the right side.

  3. On hover/click, add the left property. This makes the line span the full width. The width grows from 0 to 100% from left to right.

When you 'unhover' or click again, left is removed, and the line originates from the right again, so the width decreases in that direction.

$('a').on('click', function() {
  $('a').not(this).removeClass('underline');
  $(this).toggleClass('underline');
});
a {
  color: #00f;
  text-decoration: none;
  display: inline-block;
  position: relative;
}

a:after {
  width: 0;
  background: #00f;
  height: 3px;
  transition: all .5s ease-in-out;
  content: "";
  position: absolute;
  bottom: -5px;
  right: 0;
}

a.underline:after {
  width: 100%;
  left: 0;
}

a:hover:after {
  width: 100%;
  left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#">Click first</a>
<a href="#">Click second</a>
like image 198
sol Avatar answered Oct 12 '22 09:10

sol


Maybe you want a HTML/CSS only solution:

a {
  color: #00f;
  text-decoration: none;
  display: inline-block;
}

a span:after {
  width: 0;
  display: block;
  background: #00f;
  height: 3px;
  transition: all .5s ease-in-out;
  content: "";
  float: right;
}

a span:focus {
  color: #00f;
}

label:hover {
  cursor: pointer;
}

label input {
  display: none;
}

label input:checked + span:after, label input + span:hover:after  {
  width: 100%;
  float: left;
}

label input:checked + span {
  color: #00f;
}
<a href="#">
  <label>
    <input type="radio" name="link" />
    <span>
      Click first
    </span>
  </label>
</a>
<a href="#">
  <label>
    <input type="radio" name="link" />
    <span>
      Click second
    </span>
  </label>
</a>
like image 32
Huelfe Avatar answered Oct 12 '22 10:10

Huelfe


You can use the following. Just use a class to set style.

$('a').click(function(e){
  $(this).siblings().removeClass('start');
  $(this).toggleClass('start');
  e.preventDefault();
})
a {
  color:#00f;
  text-decoration:none;
  display:inline-block;
}
a:after {
  width: 0;
  display:block;
  background:#00f;
  height:3px;
  transition: all .5s ease-in-out;
  content:"";
  float:right;
}
a:hover {
  color:#00f;
}
a.start:after {
  width: 100%;
}
a:hover:after {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#">Click first</a>
<a href="#">Click second</a>
like image 4
rrk Avatar answered Oct 12 '22 09:10

rrk