Menu
CoddyTech

文本装饰

本课属于 Coddy HTML 学习旅程的“使用 CSS 添加样式”单元。第 22 课,共 76 课。

在 CSS 中,text-decoration 属性用于为 HTML 元素中的文本添加或移除装饰。你可以使用此属性为文本添加下划线、上划线或删除线,以突出显示文本或表示特定状态。

text-decoration 的常见值包括:

  • none:移除所有文本装饰。这是默认值。
  • underline:为文本添加下划线。
  • overline:在文本上方添加上划线。
  • line-through:在文本中间添加一条横线(删除线)。

例如:

a {
    text-decoration: none;
}

.underline {
    text-decoration: underline;
}

.strike {
    text-decoration: line-through;
}
challenge icon

挑战

简单

给定一个 HTML 文档,其中包含一个带有锚点(<a>)元素的段落(<p>),以及一个分区(<div>)。你的任务是使用 text-decoration 属性为这些元素设置样式。按照以下步骤操作:

  1. 编写一条 CSS 规则,选择所有 <a> 元素。将 text-decoration 设置为 none,以移除默认的下划线。
  2. 为 <p> 元素添加一个 class 属性,其值为 "underline"。
  3. 编写一条 CSS 规则,选择所有 class 为 underline 的元素。将 text-decoration 设置为 underline。
  4. 为 <div> 元素添加一个 class 属性,其值为 "strike"。
  5. 编写一条 CSS 规则,选择所有 class 为 strike 的元素。将 text-decoration 设置为 line-through。

亲手试试

<html>
<head>
    <title>Text Decoration</title>
    <style>
        /* Write CSS rules here */
    </style>
</head>
<body>
    <p>This is a paragraph with a <a href="#">link</a>.</p>
    <div>This is a division.</div>
</body>
</html>
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

使用 CSS 添加样式中的所有课

自己练习: Web 在线编辑器