text-decoration:underline 是用 CSS 為文字加上底線的標準寫法。兩個名詞拆开看就很清楚:text-decoration 是「文字裝飾」,underline 是「底線」。
text-decoration: underline;
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>text-decoration underline 範例</title>
<style>
.ab { text-decoration: underline; }
</style>
</head>
<body>
測試文字,範例內容 <span class="ab">這段有底線</span>!
</body>
</html>
只有 span 包住的片段帶底線,其餘維持原樣。
overline:上標線。
line-through:刪除線(即橫線劃過文字中央)。
none:移除所有裝飾線,常用來拿掉超連結的預設底線。
它可以複寫多個值,以空格分隔:text-decoration: underline overline;
CSS 也提供了更細緻的底線樣式屬性,可獨立調整線條外觀:
.link {
text-decoration-line: underline;
text-decoration-style: wavy;
text-decoration-color: #F00;
text-decoration-thickness: 2px;
text-underline-offset: 4px; /* 底線與文字的距離 */
}
其中 text-underline-offset 特別有用——它能拉開底線與文字之間的距離,避免底線壓到 p、g、y 這類有下伸筆畫的字母。
若想要「文字黑色、底線紅色」的效果,用上述的 text-decoration-color 即可,不必再繞路用 border-bottom。