Q:

How to draw a line using pseudo element right below text and ignore the padding?

I want to draw a line below a link and apply animation on it, so I use pseudo element. It produces the line as expected, but if there is a large padding around the link, the line appears far away. Is there a way to ignore the padding and draw the line right below text?

a {
  position: absolute;
  padding: 20px 0;
  top: 50%;
  left: 50%;
  margin-top: -30px;
  margin-left: -30px;
  line-height: 20px;
}

a:after {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  content: '';
  transition: width .3s;
  display: block;
}

a:hover:after {
  width: 100%;
  border-top: 1px solid #333;
}
<a>Link Text</a>

A:

You can just remove the absolute position since the pseudo is set on :after so that it's placed right after the text.

a {
  position: absolute;
  padding: 20px 0;
  top: 50%;
  left: 50%;
  margin-top: -30px;
  margin-left: -30px;
  line-height: 20px;
  border: 1px solid aqua;
}

a:after {
  content: "";
  display: block;
  border-top: 1px solid #333;
  width: 0;
  transition: width .3s;
}

a:hover:after {
  width: 100%;
}
<a>Link Text</a>

Side note, you might encounter the double tap behavior for the kind of hover effects on touch devices such as phones, tablets. Add this to fix that:
@media (hover: none) {
  a:hover:after {
    display: none;
  }
}

In addition, the effects can also be done with linear-gradient(), example:

a {
  display: inline-block;
  text-decoration: none;
  border: 1px solid aqua;
  font-size: 16px;
  padding: 20px 0;
  background-image: linear-gradient(to bottom, blue, blue);
  background-position: 0 38px; /*adjust this based on font-size and padding*/
  background-size: 0 1px;
  background-repeat: no-repeat;
  transition: background-size .3s;
}

a:hover {
  background-size: 100% 1px;
}
<a href="#">Link text</a>

