CSS / 参考 / text-decoration:设置下划线和删除线

CSS 的 text-decoration 是简写属性,用来一起设置下划线、上划线和删除线的位置、样式、颜色及粗细。下划线的偏移量用 text-underline-offset 单独调整。需要只给句子的一部分画线时,把属性设在该部分的 span 上。

设置线的位置、样式、颜色和粗细

underline、overline 和 line-through 决定线的位置。可以组合多个位置,例如 underline overline。线型有 solid、double、dotted、dashed 和 wavy。简写语法见 CSS text-decoration 简写规范。

.wavy {
  text-decoration: underline wavy #006c9c 2px;
  text-underline-offset: .18em;
}
声明值 外观 预览
underline wavy #006c9c 2px 蓝色波浪下划线
overline solid #006c9c 2px 蓝色实线上划线
line-through solid #006c9c 2px 穿过文字的蓝色实线
underline overline dotted #006c9c 2px 蓝色点状上划线和下划线

各项独立属性是 text-decoration-line、text-decoration-style、text-decoration-color 和 text-decoration-thickness。简写中省略的值分别重置为 none、solid、currentColor 或 auto。要保留颜色或粗细,可以只改 text-decoration-line。

在浏览器中查看装饰线和链接

将完整代码以 UTF-8 编码保存为 text-decoration-example.html,再用浏览器打开。第一组可以对比波浪下划线、上下两条点线和删除线。示例无须外部样式表。

<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>比较 CSS text-decoration</title>
  <style>
    body { margin: 1rem; color: #20242b; background: #fff;
      font: 1rem/1.65 system-ui, sans-serif; }
    main { max-width: 44rem; margin-inline: auto; }
    .sample { font-size: 1.2rem; }
    .wavy { text-decoration: underline wavy #006c9c 2px;
      text-underline-offset: .18em; }
    .two-lines { text-decoration: underline overline dotted #006c9c 2px; }
    .strike { text-decoration: line-through; }
    .origin, .only { text-decoration: underline solid #aa3377 3px;
      text-underline-offset: .16em; text-decoration-skip-ink: none; }
    .child { text-decoration: none; }
    .copy-link { color: #005879; text-decoration: underline solid currentColor;
      text-underline-offset: .16em; }
    .copy-link:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; }
  </style>
</head>
<body>
  <main>
    <h1>比较 CSS text-decoration</h1>
    <h2>线的位置和样式</h2>
    <p class="sample wavy">CSS 装饰线样例</p>
    <p class="sample two-lines">CSS 装饰线样例</p>
    <p class="sample strike">CSS 装饰线样例</p>
    <h2>父元素的装饰线与范围</h2>
    <p class="sample origin"><span>前一段</span> <span class="child">后一段</span></p>
    <p class="sample scoped"><span class="only">前一段</span> <span class="plain">后一段</span></p>
    <h2>链接和已删除的内容</h2>
    <p><a class="copy-link" href="#details">查看新的安排</a></p>
    <p id="details"><del>原定星期一</del> 新安排是星期二。</p>
  </main>
</body>
</html>

第二组的第一句话在前后两段都有紫色下划线。第二句话只有前一段有线。按 Tab 键移动到最后一组的链接,可以同时看到下划线和键盘焦点轮廓。

理解父元素的下划线为何仍然可见

各个装饰线属性并不像文字颜色那样继承。不过,父元素的下划线仍可以绘制到同一文本流中的行内子元素下方。子元素的 text-decoration-line 计算值可以是 none,父元素的线仍可见。具体条件见 CSS 装饰线传播规则。

只给 .origin 添加 text-decoration: none,第一句话的线就会全部消失。试完删除这一声明。像第二句话那样把装饰设在所需的 span 上,可以缩小画线范围,周围文字的排列方式保持原样。

区分链接并标记已删除的内容

下划线有助于读者识别正文中的链接。通过键盘导航时,要保留可见的焦点标记。下面的规则只应用到带 .copy-link 类的链接。相关说明见 WCAG 颜色使用准则 和 链接状态和键盘焦点指南。

.copy-link {
  text-decoration: underline solid currentColor;
  text-underline-offset: .16em;
}
.copy-link:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; }

从文档删除的内容可以用 <del> 标记。示例给取消的星期一加上删除线,并用文字说明新安排是星期二。line-through 提供线条外观,删除含义由 HTML del 元素规范 定义。

排查多余装饰线或下划线间距

如果线仍可见,在开发者工具中检查子元素和产生该线的元素的 text-decoration-line。下划线离字太近时,调整粗细和 text-underline-offset。需要在盒子的下边缘画线时,使用 border-bottom。

如果线条中间有缺口,检查 text-decoration-skip-ink: auto 是否避开了接触文字笔画的位置。紫色示例用 none 画连续线。请使用实际字体和行高检查可读性,相关设置见 字体和行高指南。