CSS 文本装饰


目录

    显示目录


文字装饰

在本章中,您将了解以下属性:

  • text-decoration-line
  • text-decoration-color
  • text-decoration-style
  • text-decoration-thickness
  • text-decoration

向文本添加装饰线

text-decoration-line 属性用于添加 文本的装饰线。

提示:您可以组合多个值,例如上划线和 下划线在文本上方和下方显示线条。

例子

h1 {
  text-decoration-line: overline;
}

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

h3 {
  text-decoration-line: underline;
}
p {
  text-decoration-line: 
  overline underline;
}

自己尝试一下 →

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  text-decoration: overline;
}

h2 {
  text-decoration: line-through;
}

h3 {
  text-decoration: underline;
}

p.ex {
  text-decoration: overline underline;
}
</style>
</head>
<body>

<h1>Overline text decoration</h1>
<h2>Line-through text decoration</h2>
<h3>Underline text decoration</h3>
<p class="ex">Overline and underline text decoration.</p>

<p><strong>Note:</strong> It is not recommended to underline text that is not a link, as this often confuses 
the reader.</p>

</body>
</html>


注意:不建议在不是链接的文本下划线,因为这常常会让读者感到困惑。


指定装饰线的颜色

text-decoration-color 属性用于 设置装饰线的颜色。

例子

h1 {
  text-decoration-line: overline;
  text-decoration-color: 
  red;
}

h2 {
  text-decoration-line: line-through;
  text-decoration-color: 
  blue;
}

h3 {
  text-decoration-line: underline;
  text-decoration-color: 
  green;
}
p {
  text-decoration-line: 
  overline underline;
  text-decoration-color: purple;
}

自己尝试一下 →

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  text-decoration-line: overline;
  text-decoration-color: red;
}

h2 {
  text-decoration-line: line-through;
  text-decoration-color: blue;
}

h3 {
  text-decoration-line: underline;
  text-decoration-color: green;  
}

p {
  text-decoration-line: overline underline;
  text-decoration-color: purple;  
}
</style>
</head>
<body>

<h1>Overline text decoration</h1>
<h2>Line-through text decoration</h2>
<h3>Underline text decoration</h3>
<p>Overline and underline text decoration.</p>

</body>
</html>




指定装饰线的样式

text-decoration-style 属性用于 设置装饰线的样式。

例子

 h1 {
  text-decoration-line: underline;
  text-decoration-style: 
  solid;
}
  
h2 {
  text-decoration-line: underline;
  
  text-decoration-style: double;
}
  
h3 {
  text-decoration-line: underline;
  
  text-decoration-style: dotted; 
}
p.ex1 {
  text-decoration-line: underline;
  
  text-decoration-style: dashed; 
}
p.ex2 {
  text-decoration-line: underline;
  
  text-decoration-style: wavy; 
}
p.ex3 {
  text-decoration-line: 
  underline;
  text-decoration-color: red; 
  
  text-decoration-style: wavy; 
}

自己尝试一下→

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  text-decoration-line: underline;
  text-decoration-style: solid; /* this is default */
}

h2 {
  text-decoration-line: underline;
  text-decoration-style: double;
}

h3 {
  text-decoration-line: underline;
  text-decoration-style: dotted;  
}

p.ex1 {
  text-decoration-line: underline;
  text-decoration-style: dashed;  
}

p.ex2 {
  text-decoration-line: underline;
  text-decoration-style: wavy;  
}

p.ex3 {
  text-decoration-line: underline;
  text-decoration-color: red;  
  text-decoration-style: wavy;  
}
</style>
</head>
<body>

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<p class="ex1">A paragraph.</p>
<p class="ex2">Another paragraph.</p>
<p class="ex3">Another paragraph.</p>

</body>
</html>



指定装饰线的粗细

text-decoration-thickness 属性用于 设置装饰线的粗细。

例子

 h1 {
  text-decoration-line: underline;
  
  text-decoration-thickness: auto;
}
h2 {
  text-decoration-line: 
  underline;
  text-decoration-thickness: 5px;
}
h3 {
  
  text-decoration-line: underline;
  text-decoration-thickness: 25%;
  }
p {
  
  text-decoration-line: underline;
  text-decoration-color: red; 
  
  text-decoration-style: double;
  text-decoration-thickness: 5px; 
}

自己尝试一下 →

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  text-decoration-line: underline;
  text-decoration-thickness: auto;  /* this is default */
}

h2 {
  text-decoration-line: underline;
  text-decoration-thickness: 5px;
}

h3 {
  text-decoration-line: underline;
  text-decoration-thickness: 25%;
}

p {
  text-decoration-line: underline;
  text-decoration-color: red;  
  text-decoration-style: double;
  text-decoration-thickness: 5px;  
}
</style>
</head>
<body>

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<p>A paragraph.</p>

</body>
</html>



速记属性

text-decoration 属性是一个简写 财产:

  • 文本装饰线(必填)

  • 文本装饰颜色(可选)

  • 文本装饰风格(可选)

  • 文本装饰厚度(可选)

例子

 h1 {
  text-decoration: underline;
}
h2 {
  
  text-decoration: underline red;
}
h3 {
  text-decoration: underline 
  red double;
}
p {
  
  text-decoration: underline red double 5px;
}

自己尝试一下→

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  text-decoration: underline;
}

h2 {
  text-decoration: underline red;
}

h3 {
  text-decoration: underline red double;
}

p {
  text-decoration: underline red double 5px;
}
</style>
</head>
<body>

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<p>A paragraph.</p>

</body>
</html>



一个小提示

默认情况下,HTML 中的所有链接都带有下划线。有时你 看到链接的样式没有下划线。 text-decoration: none; 用于删除 链接的下划线, 像这样:

例子

a {
  text-decoration: none;
}

自己尝试一下 →

<!DOCTYPE html>
<html>
<head>
<style>
a {
  text-decoration: none;
}
</style>
</head>
<body>

<h1>Using text-decoration: none</h1>

<p>A link with no underline: <a href="https://www.w3schools.com">W3Schools.com</a></p>

</body>
</html>



所有 CSS text-decoration 属性

text-decoration

在一个声明中设置所有文本修饰属性

text-decoration-color

指定文本装饰的颜色

text-decoration-line

指定要使用的文本修饰类型(下划线、上划线、 ETC。)

text-decoration-style

指定文本装饰的样式(实线、点线等)

text-decoration-thickness

指定文本装饰线的粗细