CSS中用于添加下划线的属性是 text-decoration,其值可以设置为 underline。
text-decoration 属性用于设置文本的装饰效果,如下划线、删除线或上划线。它是一个非继承属性,意味着子元素不会从父元素继承该属性的值。
除了 underline,text-decoration 还有其他几个值:
none:默认值,文本没有任何装饰。overline:文本上方有一条线。line-through:文本中间有一条线,通常用于表示删除。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Text Decoration Example</title>
<style>
.underline {
text-decoration: underline;
}
.overline {
text-decoration: overline;
}
.line-through {
text-decoration: line-through;
}
</style>
</head>
<body>
<p class="underline">This text has an underline.</p>
<p class="overline">This text has an overline.</p>
<p class="line-through">This text has a line-through.</p>
</body>
</html>原因:默认情况下,下划线会紧贴文本下方,可能会覆盖文本。
解决方法:可以通过设置 text-decoration-skip-ink 属性来解决这个问题。
.underline {
text-decoration: underline;
text-decoration-skip-ink: auto;
}原因:默认情况下,下划线的颜色与文本颜色相同。
解决方法:可以使用 text-decoration-color 属性来改变下划线的颜色。
.underline {
text-decoration: underline;
text-decoration-color: red;
}