text-decoration

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

* 此特性的某些部分可能存在不同级别的支持。

text-decoration 简写 CSS 属性设置文本上装饰线的显示样式。它是 text-decoration-linetext-decoration-colortext-decoration-style 以及较新的 text-decoration-thickness 属性的简写。

试一试

text-decoration: underline;
text-decoration: underline dotted;
text-decoration: underline dotted red;
text-decoration: green wavy underline;
text-decoration: underline overline #ff3028;
<section id="default-example">
  <p>
    I'd far rather be
    <span class="transition-all" id="example-element">happy than right</span>
    any day.
  </p>
</section>
p {
  font: 1.5em sans-serif;
}

文本装饰会绘制在后代文本元素上。这意味着如果一个元素指定了文本装饰,那么子元素不能移除该装饰。例如,在标记 <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 属性的简写:

语法

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

/* Global values */
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

设置用于装饰的线条样式,例如 solid(实线)、wavy(波浪线)或 dashed(虚线)。

text-decoration-thickness

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

正式定义

初始值作为简写中的每个属性
应用于所有元素。也适用于 ::first-letter::first-line
继承性
计算值作为简写中的每个属性
动画类型作为简写中的每个属性

正式语法

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

<text-decoration-line> =
none |
[ underline || overline || line-through || blink ] |
spelling-error |
grammar-error

<text-decoration-thickness> =
auto |
from-font |
<length-percentage>

<text-decoration-style> =
solid |
double |
dotted |
dashed |
wavy

<text-decoration-color> =
<color>

<length-percentage> =
<length> |
<percentage>

示例

text-decoration 值的演示

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">This text has a line underneath it.</p>
<p class="over">This text has a line over it.</p>
<p class="line">This text has a line going through it.</p>
<p>
  This <a class="plain" href="#">link will not be underlined</a>, as links
  generally are by default. Be careful when removing the text decoration on
  anchors since users often depend on the underline to denote hyperlinks.
</p>
<p class="underover">This text has lines above <em>and</em> below it.</p>
<p class="thick">
  This text has a really thick purple underline in supporting browsers.
</p>
<p class="blink">
  This text might blink for you, depending on the browser you use.
</p>

结果

规范

规范
CSS 文本装饰模块级别 3
# text-decoration-property
Scalable Vector Graphics (SVG) 2
# TextDecorationProperties

浏览器兼容性

另见