如何使用CSS从下到上制作伪类下划线? 问题

问题描述

我是 CSS 的初学者,学习了大约一个星期。 这是我关于堆栈溢出的第一个问题。 希望我以正确的方式提问。我很高兴收到任何更正。

问题

我使用伪类来制作一个在悬停时变粗的下划线。 我想让它从下到上滑动,同时从上到下滑动。 我怎样才能做到?

我确实搜索了类似的问题,但最终得到了块元素(渐变等)。 非常感谢!

Here is my code on CodePen.

a {
  text-decoration: none;
  color: teal;
}

a {
  display: inline-block;
  position: relative;
}

a::after {
  content: "";
  display: block;
  height: 0.1rem;
  
  transform: rotate(0.5deg);
  background-color: #20c997; /* teal */
  opacity: 0.3;
  transition: height 0.3s;
}

a:hover::after {
  height: 0.5rem;
}

解决方法

您只需要将下划线 absolute 定位到链接底部。左右偏移量应设置为 0 以增大到整个链接宽度(或者您可以只设置 width: 100%;

a {
  text-decoration: none;
  color: teal;
}

a {
  display: inline-block;
  position: relative;
}

a::after {
  content: "";
  display: block;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 0.1rem;
  background-color: #20c997; /* teal */
  opacity: 0.3;
  transition: height 0.3s;
}

a:hover::after {
  height: 0.5rem;
}
<a href="#">A link text with a animated underline</a>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...