在本实验中,你将学习如何使用 HTML <span> 标签来创建内联文本样式,并增强网页内容的视觉呈现。本实验的重点是理解 span 标签的用途、创建基本的 HTML 文档结构,以及为特定文本片段应用自定义的内联样式。
通过一个逐步演进的实践示例,你将探索如何使用 span 标签来定位和样式化小部分文本,而不会破坏文档的流式布局。在本实验结束时,你将能够使用 HTML span 元素应用自定义文本颜色、背景以及其他内联样式技术,从而在你的网页中提供更具动态性和视觉吸引力的文本格式。我们将在此实验中逐步构建一个 HTML 文件,逐步添加更多功能和样式。
让我们创建一个简单的 HTML 文件来演示 <span> 标签的使用。打开 WebIDE,在 ~/project 目录中创建一个名为 styling-example.html 的新文件。这个文件将成为本实验后续内容的画布。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Span Tag Styling</title>
</head>
<body>
<p>This is a normal line of text.</p>
<p>This line contains a <span>special</span> word.</p>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Span Tag Styling</title>
</head>
<body>
<p>This is a normal line of text.</p>
<p>
This line contains a <span style="font-weight: bold;">special</span> word.
</p>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Span Tag Styling</title>
</head>
<body>
<p>This is a normal line of text.</p>
<p>
This line contains a
<span
style="font-weight: bold; font-style: italic; text-decoration: underline;"
>special</span
>
word.
</p>
</body>
</html>
现在让我们改变文本的颜色及其背景。在这一步中,你将学习如何在 <span> 标签的 style 属性中使用 color 和 background-color CSS 属性。
打开 styling-example.html 并再次修改 <span> 标签:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Span Tag Styling</title>
</head>
<body>
<p>This is a normal line of text.</p>
<p>
This line contains a
<span
style="font-weight: bold; font-style: italic; text-decoration: underline; color: blue; background-color: lightyellow;"
>special</span
>
word.
</p>
</body>
</html>