html - 使用文本装饰下划线为文本添加下划线 - CSS
问题描述
以下是我的 HTML/CSS 代码,我在其中尝试在不使用本机text-decoration: underline
属性的情况下在文本下方添加下划线。但不知何故,这段代码不起作用。任何指针我在这里做错了什么
HTML 代码 -
<span>
<a class="test">Add Underline To Text</a>
</span>
CSS 代码 -
.test {
position: relative;
text-decoration: none;
}
.test:hover {
width: 100%:
}
.test:after {
content: '';
position: absolute;
left: 0;
bottom: -7px;
height: 4px;
background-color: green;
width: 0;
transition: width .25s;
}
Codepen 链接 - https://codepen.io/anon/pen/VEBNyx
解决方案
您想在悬停链接width
时添加吗?然后你需要写. 不只是。:after
.test
test:hover:after { }
test:hover
即使:after
是伪元素,如果要更改其样式,也必须选择它
见下文
.test {
position: relative;
text-decoration: none;
}
.test:hover:after {
width: 100%;
}
.test:after {
content: '';
position: absolute;
left: 0;
bottom: -7px;
height: 4px;
background-color: green;
width: 0;
transition: width .25s;
}
<span>
<a class="test">Add Underline To Text</a>
</span>
推荐阅读
- vagrant - Xdebug 2.7.0 不适用于带有 Laravel Homestead 的 PhpStorm
- highcharts - “viewFullscreen”菜单项未显示在 highcharts 6.2.0 的上下文菜单中
- javascript - 如何从跨域 iframe 中捕获(初始化)错误?
- regex - 需要使用正则表达式匹配变量中存在的类似标题的文件名
- tensorflow - 当我从修剪中获得稀疏矩阵时,如何在 TensorFlow 中加快推理速度?
- ios - ID 为 xxxxxxxx 的应用在 ID 为 yyyy 的应用上没有正确的 view_structure
- python - 如何将对象转换为数字
- php - 电子邮件附件在 Vtiger 电子邮件自定义代码中不起作用
- c# - Asp.net 中的时间计算
- docker - Hyperledger Composer Playground 本地文件