问题描述
我是 CSS 的初学者,学习了大约一个星期。 这是我关于堆栈溢出的第一个问题。 希望我以正确的方式提问。我很高兴收到任何更正。
问题
我使用伪类来制作一个在悬停时变粗的下划线。 我想让它从下到上滑动,同时从上到下滑动。 我怎样才能做到?我确实搜索了类似的问题,但最终得到了块元素(渐变等)。 非常感谢!
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>