text-decoration

text-decoration 简写 CSS 属性设置文本上的装饰性线条的外观。它是 text-decoration-linetext-decoration-colortext-decoration-style 和较新的 text-decoration-thickness 属性的缩写。

尝试一下

文本装饰会覆盖子孙文本元素。这意味着如果一个元素指定了文本装饰,那么子元素无法去除这个装饰。例如,在标记 <p>This text has <em>some emphasized words</em> in it.</p> 中,样式规则 p { text-decoration: underline; } 会导致整个段落被下划线划掉。样式规则 em { text-decoration: none; } 不会有任何变化;整个段落仍然会被下划线划掉。但是,规则 em { text-decoration: overline; } 会导致“some emphasized words”上出现第二个装饰。

复合属性

语法

css
text-decoration: underline;
text-decoration: overline red;
text-decoration: none;

/* 全局值 */
text-decoration: inherit;
text-decoration: initial;
text-decoration: revert;
text-decoration: revert-layer;
text-decoration: unset;

text-decoration 属性由一个或多个用空格分隔的值组成,表示各种普通文本装饰属性。

取值

text-decoration-line

设置使用的装饰类型,例如 underline 或者 line-through

text-decoration-color

设置装饰的颜色。

text-decoration-style

设置装饰的线条的颜色,例如 solidwavy 或者 dashed

text-decoration-thickness

设置用于装饰的线条粗细。

形式定义

初始值as each of the properties of the shorthand:
适用元素all elements. It also applies to ::first-letter and ::first-line.
是否是继承属性
计算值as each of the properties of the shorthand:
Animation typeas each of the properties of the shorthand:

形式语法

text-decoration = 
<'text-decoration-line'> ||
<'text-decoration-style'> ||
<'text-decoration-color'>

示例

文本装饰值的演示

css
.under {
  text-decoration: underline red;
}

.over {
  text-decoration: wavy overline lime;
}

.line {
  text-decoration: line-through;
}

.plain {
  text-decoration: none;
}

.underover {
  text-decoration: dashed underline overline;
}

.thick {
  text-decoration: solid underline purple 4px;
}

.blink {
  text-decoration: blink;
}
html
<p class="under">此文本下方有一行线。</p>
<p class="over">此文本上面有一行线。</p>
<p class="line">此文本有一条线穿过它。</p>
<p><a class="plain" href="#">链接不会具有下划线</a
  >,因为链接默认加下划线。移除锚点上的文本装饰时要小心,因为用户通常依赖下划线来表示超链接
</p>
<p class="underover">此文本在其上方<em></em>下方都有线条。</p>
<p class="thick">在浏览器支持的情况下,此文本具有非常粗的紫色下划线。</p>
<p class="blink">此文本可能会为你闪烁,具体取决于你使用的浏览器。</p>

规范

Specification
CSS Text Decoration Module Level 3
# text-decoration-property

浏览器兼容性

BCD tables only load in the browser

参见