要实现带间距的多行文本下划线效果,有几种CSS方法可以选择。下面我将介绍最常用的几种解决方案:
.underlined {
background-image: linear-gradient(to right, currentColor 0%, currentColor 100%);
background-repeat: repeat-x;
background-position: 0 100%;
background-size: 100% 1px;
padding-bottom: 3px; /* 控制下划线与文本的间距 */
}
.underlined {
position: relative;
display: inline;
}
.underlined::after {
content: "";
position: absolute;
left: 0;
right: 0;
bottom: -3px; /* 控制下划线与文本的间距 */
height: 1px;
background-color: currentColor;
}
.underlined {
text-decoration: underline;
text-decoration-skip-ink: none; /* 确保下划线连贯 */
text-underline-offset: 3px; /* 控制下划线与文本的间距 */
text-decoration-thickness: 1px;
}
对于多行文本,上述方法可能需要额外调整:
.multiline-underlined {
display: inline;
background-image: linear-gradient(to right, currentColor 0%, currentColor 100%);
background-repeat: repeat-x;
background-position: 0 100%;
background-size: 100% 1px;
padding-bottom: 3px;
box-shadow: 0px 1px 0px 0px currentColor; /* 为换行处添加下划线 */
}
background-image
方法通常兼容性最好currentColor
可以让下划线颜色自动匹配文本颜色padding-bottom
或bottom
值可以控制下划线与文本的间距box-shadow
来确保每行都有下划线选择哪种方法取决于你的具体需求和浏览器兼容性要求。现代浏览器中,方法3(text-decoration)是最语义化的选择。