bootstrap 5 的 text-decoration 工具类仅提供 text-decoration-none、text-decoration-underline 和 text-decoration-line-through 三个语义化类,直接映射原生 css 的 text-decoration-line 值,不支持响应式断点、颜色、样式等扩展,需手动处理 hover、继承、a 标签覆盖及复杂装饰效果。

Bootstrap 5 的 text-decoration 工具类怎么用
Bootstrap 5 不提供独立的 text-decoration CSS 属性封装,而是通过一组语义化工具类控制下划线、删除线等效果。核心是 text-decoration-none、text-decoration-underline 和 text-decoration-line-through 这三个类。
它们直接映射到原生 CSS 的 text-decoration 值,不带额外修饰或重置逻辑——这意味着你得自己处理 hover 状态、颜色继承、响应式切换等细节。
-
text-decoration-none会覆盖所有已有的下划线/删除线(包括父元素继承来的),但不会影响其他文本装饰(如text-shadow) -
text-decoration-underline默认使用浏览器默认颜色(通常是currentColor),不是 Bootstrap 主题色 - 这些类只作用于当前元素,不自动传播给子元素(比如
<a></a>里嵌<span></span>,span不会自动带下划线)
为什么 a 标签加了 text-decoration-underline 还没下划线
因为 Bootstrap 5 默认对 <a></a> 元素设置了 text-decoration: none(在 _reboot.scss 中),且该规则优先级高于工具类。工具类只是添加 text-decoration: underline,但被更早声明的 a 专用规则覆盖了。
- 最简解法:显式加
text-decoration-underline并用!important(不推荐) - 合理做法:改用
<span class="text-decoration-underline">文本</span>,或自定义样式覆盖a的默认行为 - 注意:Bootstrap 的
link-underline类(v5.3+)才真正支持链接下划线,旧版本没有这个类
text-decoration 在响应式场景下的坑
Bootstrap 5 的文本装饰类不支持响应式断点前缀(比如没有 text-decoration-underline-md)。想实现“移动端无下划线、桌面端有”,只能手写媒体查询或用内联 style。
- 别指望
text-decoration-none d-md-inline这类组合能恢复下划线——text-decoration-none是强制清除,d-md-inline只控制 display - 如果用 Sass,可复用 Bootstrap 的
text-decoration工具类生成逻辑,手动加断点变量 - 纯 CSS 方案示例:
@media (min-width: 768px) { .custom-underline-md { text-decoration: underline; } }
和原生 text-decoration CSS 属性的兼容性差异
Bootstrap 的工具类只设置 text-decoration-line(即 underline / line-through),不控制 text-decoration-color 或 text-decoration-style。这意味着:
- 下划线颜色永远跟随文字颜色(
currentColor),没法一键改成red或var(--primary) - 没法用工具类实现波浪线(
wavy)、双线(double)等样式——必须手写 CSS - 若项目用了
text-decoration-thickness或text-underline-offset,工具类完全不干涉,但需注意浏览器兼容性(Safari 15.4+ 才支持后者)
复杂样式需求一多,就容易掉进“一半靠工具类、一半靠自定义 CSS”的缝合状态——这时候不如直接写一行 style="text-decoration: underline wavy red" 更可控。











