置顶公告:

欢迎访问龙卷风自助链–留言板,在线咨询QQ:307021929
冰封的心
2026-3-17 9:28:41

HTML字体样式设置全攻略:颜色、大小、类型及效果实现

(置顶)[回复]
在网页开发中,字体样式设置是提升页面视觉效果的关键环节。本文将系统梳理HTML中常用的字体样式设置方法,包括颜色、大小、类型、粗细、斜体、下划线等效果,并提供完整的代码示例和实现细节。

基础字体样式设置
字体颜色设置
HTML中可通过color属性设置字体颜色,支持颜色名称、十六进制值和RGB值。

<p style="color: red;">红色文字</p>
<p style="color: #0099ff;">蓝色文字</p>
<p style="color: rgb(0, 200, 0);">绿色文字</p>
字体大小设置
使用font-size属性可调整字体大小,支持像素(px)、em等单位。


<p style="font-size: 12px;">12px 文字</p>
<p style="font-size: 1.5em;">1.5em 文字</p>
字体类型设置
通过font-family属性指定字体,可设置多个备选字体。


<p style="font-family: 'Times New Roman', serif;">Times New Roman 字体</p>
<p style="font-family: '楷体', 'Microsoft YaHei', sans-serif;">楷体字体</p>
高级字体效果实现
字体粗细与样式
使用font-weight和font-style属性可设置字体粗细和样式。


<p style="font-weight: bold;">加粗文字</p>
<p style="font-style: italic;">斜体文字</p>
文本装饰效果
text-decoration属性可实现下划线、删除线等效果。


<p style="text-decoration: underline;">下划线</p>
<p style="text-decoration: line-through;">删除线</p>
文本阴影效果
text-shadow属性可创建阴影、发光和立体效果。


<p style="text-shadow: 2px 2px 4px rgba(0,0,0,0.5);">阴影文字</p>
<p style="text-shadow: 0 0 5px #ff0000;">发光效果</p>
综合字体样式应用
综合样式设置 font属性可一次性设置字体样式、粗细、大小和类型。


<p style="font: italic bold 20px/1.5 'Times New Roman', serif;">综合样式</p>
完整代码示例

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>HTML字体样式大全</title>
<style>
body { font-family: 'Arial', sans-serif; }
.example { margin-bottom: 20px; }
.code { background-color: #f8f9fa; padding: 10px; }
</style>
</head>
<body>
<div class="example">
<p style="color: red;">红色文字</p>
<div class="code">&lt;p style="color: red;"&gt;红色文字&lt;/p&gt;</div>
</div>
</body>
</html>