上一篇

在HTML中设置字体主要通过CSS的
font-family属性实现,
文本,可定义多个字体备选(如
"Microsoft YaHei", sans-serif),用逗号分隔,浏览器将优先使用首个可用字体,还可用`
标签或外部CSS文件统一管理样式,同时支持font-size
、color`等属性调整字号和颜色。
HTML字体设置指南:打造精美网页文本
下面是一份详细讲解HTML中字体设置方法的完整指南:


<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">HTML字体设置完全指南</title>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;700&family=Noto+Serif+SC:wght@400;700&display=swap" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Noto Sans SC', sans-serif;
line-height: 1.8;
color: #333;
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
background-attachment: fixed;
padding: 20px;
max-width: 1200px;
margin: 0 auto;
}
header {
text-align: center;
padding: 50px 20px;
background: linear-gradient(120deg, #4b6cb7, #182848);
color: white;
border-radius: 12px;
margin: 20px 0;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}
h1 {
font-family: 'Noto Serif SC', serif;
font-size: 3.2rem;
font-weight: 700;
margin-bottom: 20px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
letter-spacing: 1px;
}
.subtitle {
font-size: 1.4rem;
max-width: 800px;
margin: 0 auto;
opacity: 0.9;
}
.container {
display: grid;
grid-template-columns: 1fr 350px;
gap: 30px;
margin: 30px 0;
}
.main-content {
background: white;
border-radius: 12px;
padding: 40px;
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
}
.sidebar {
background: white;
border-radius: 12px;
padding: 30px;
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
}
h2 {
font-family: 'Noto Serif SC', serif;
color: #2c3e50;
margin: 30px 0 20px;
padding-bottom: 10px;
border-bottom: 2px solid #3498db;
}
h3 {
font-family: 'Noto Serif SC', serif;
color: #2980b9;
margin: 25px 0 15px;
}
p {
margin-bottom: 20px;
font-size: 1.1rem;
}
.code-block {
background: #2d3748;
color: #e2e8f0;
padding: 20px;
border-radius: 8px;
margin: 20px 0;
font-family: 'Courier New', monospace;
font-size: 1rem;
overflow-x: auto;
line-height: 1.6;
}
.example-box {
background: #f8f9fa;
border-left: 4px solid #3498db;
padding: 20px;
margin: 25px 0;
border-radius: 0 8px 8px 0;
}
.example-text {
padding: 10px;
margin: 15px 0;
border-radius: 4px;
}
.font-showcase {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 30px 0;
}
.font-card {
padding: 25px;
border-radius: 8px;
background: white;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
border: 1px solid #e9ecef;
transition: transform 0.3s ease;
}
.font-card:hover {
transform: translateY(-5px);
box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
}
.font-name {
font-weight: bold;
font-size: 1.3rem;
margin-bottom: 15px;
color: #2c3e50;
}
.font-sample {
font-size: 1.4rem;
line-height: 1.7;
min-height: 80px;
}
.tip {
background: #e3f2fd;
border-left: 4px solid #2196f3;
padding: 20px;
margin: 25px 0;
border-radius: 0 8px 8px 0;
}
.tip h4 {
font-size: 1.2rem;
color: #0d47a1;
margin-bottom: 10px;
}
.browser-support {
background: #fff8e1;
padding: 25px;
border-radius: 8px;
margin: 30px 0;
border: 1px solid #ffecb3;
}
.browser-support h3 {
color: #ff8f00;
}
.browser-list {
display: flex;
justify-content: space-around;
margin-top: 20px;
flex-wrap: wrap;
}
.browser {
text-align: center;
padding: 15px;
}
.browser i {
font-size: 3rem;
margin-bottom: 10px;
color: #4b6cb7;
}
.browser span {
font-weight: bold;
}
footer {
text-align: center;
padding: 30px;
margin-top: 40px;
background: #2c3e50;
color: white;
border-radius: 12px;
font-size: 0.9rem;
}
.references {
margin-top: 40px;
padding-top: 30px;
border-top: 1px solid #ddd;
}
.references h2 {
text-align: center;
border-bottom: none;
}
.ref-list {
columns: 2;
column-gap: 40px;
}
.ref-list li {
margin-bottom: 15px;
break-inside: avoid;
}
@media (max-width: 900px) {
.container {
grid-template-columns: 1fr;
}
.font-showcase {
grid-template-columns: 1fr;
}
.ref-list {
columns: 1;
}
}
.color-demo {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin: 25px 0;
}
.color-box {
width: 120px;
height: 120px;
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
text-align: center;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
/* 字体样式示例 */
.font-bold { font-weight: 700; }
.font-italic { font-style: italic; }
.font-underline { text-decoration: underline; }
.font-strike { text-decoration: line-through; }
.font-uppercase { text-transform: uppercase; }
.font-spacing { letter-spacing: 3px; }
.font-shadow { text-shadow: 2px 2px 4px rgba(0,0,0,0.3); }
</style>
</head>
<body>
<header>
<h1>HTML字体设置完全指南</h1>
<p class="subtitle">掌握HTML字体设置的现代方法与最佳实践,打造专业美观的网页排版</p>
</header>
<div class="container">
<div class="main-content">
<h2>字体设置基础</h2>
<p>在HTML中设置字体主要使用CSS(层叠样式表),虽然过去使用<font>标签,但它在HTML5中已被废弃,CSS是当前的标准方法。</p>
<h3>设置字体系列</h3>
<p>使用<code>font-family</code>属性指定文本的字体系列:</p>
<div class="code-block">
p {<br>
font-family: "Microsoft YaHei", "Segoe UI", sans-serif;<br>
}
</div>
<div class="tip">
<h4>专业提示:</h4>
<p>始终指定备用字体(fallback fonts),最后以一个通用字体族(如serif或sans-serif)结束,确保在所有设备上都有可接受的显示效果。</p>
</div>
<h3>设置字体大小</h3>
<p>推荐使用相对单位设置字体大小,使页面具有响应式特性:</p>
<div class="code-block">
body {<br>
font-size: 100%; /* 基准大小通常为16px */<br>
}<br>
<br>
h1 {<br>
font-size: 2.5rem; /* 相当于40px */<br>
}<br>
<br>
p {<br>
font-size: 1.125rem; /* 相当于18px */<br>
}
</div>
<div class="example-box">
<h3>尺寸示例:</h3>
<p style="font-size: 0.875rem;">较小文本 - 0.875rem (14px)</p>
<p style="font-size: 1rem;">标准文本 - 1rem (16px)</p>
<p style="font-size: 1.125rem;">较大文本 - 1.125rem (18px)</p>
<p style="font-size: 1.5rem;">标题文本 - 1.5rem (24px)</p>
</div>
<h3>设置字体颜色</h3>
<p>使用<code>color</code>属性设置文本颜色:</p>
<div class="code-block">
/* 使用颜色名称 */<br>
.red-text { color: red; }<br><br>
/* 使用十六进制值 */<br>
.blue-text { color: #1e88e5; }<br><br>
/* 使用RGB/RGBA值 */<br>
.green-text { color: rgb(46, 204, 113); }<br>
.transparent-text { color: rgba(0, 0, 0, 0.7); }
</div>
<div class="color-demo">
<div class="color-box" style="background: #e74c3c;">#e74c3c</div>
<div class="color-box" style="background: #3498db;">#3498db</div>
<div class="color-box" style="background: #2ecc71;">#2ecc71</div>
<div class="color-box" style="background: #f39c12;">#f39c12</div>
<div class="color-box" style="background: #9b59b6;">#9b59b6</div>
<div class="color-box" style="background: #34495e; color: #ecf0f1;">#34495e</div>
</div>
<h2>高级字体样式</h2>
<p>CSS提供了多种属性来微调文本外观:</p>
<div class="code-block">
/* 字体粗细 */<br>
.bold { font-weight: 700; }<br>
.light { font-weight: 300; }<br><br>
/* 字体样式 */<br>
.italic { font-style: italic; }<br><br>
/* 文本装饰 */<br>
.underline { text-decoration: underline; }<br>
.strike { text-decoration: line-through; }<br><br>
/* 文本变换 */<br>
.uppercase { text-transform: uppercase; }<br>
.capitalize { text-transform: capitalize; }<br><br>
/* 字符间距 */<br>
.spaced { letter-spacing: 2px; }<br>
.condensed { letter-spacing: -1px; }<br><br>
/* 行高 */<br>
.line-height { line-height: 1.8; }
</div>
<div class="example-box">
<h3>样式示例:</h3>
<p class="font-bold">加粗文本 - font-weight: bold</p>
<p class="font-italic">斜体文本 - font-style: italic</p>
<p class="font-underline">下划线文本 - text-decoration: underline</p>
<p class="font-strike">删除线文本 - text-decoration: line-through</p>
<p class="font-uppercase">大写转换 - text-transform: uppercase</p>
<p class="font-spacing">字符间距 - letter-spacing: 3px</p>
<p class="font-shadow">文本阴影 - text-shadow: 2px 2px 4px rgba(0,0,0,0.3)</p>
</div>
<h2>使用Web字体</h2>
<p>现代网页设计常常使用网络字体服务,如Google Fonts:</p>
<div class="code-block">
<!-- 在head中添加字体链接 --><br>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet"><br><br>
/* 在CSS中使用 */<br>
body {<br>
font-family: 'Roboto', sans-serif;<br>
}
</div>
<h3>常用中文字体推荐</h3>
<div class="font-showcase">
<div class="font-card">
<div class="font-name">思源黑体 (Noto Sans SC)</div>
<div class="font-sample" style="font-family: 'Noto Sans SC', sans-serif;">
思源黑体是一款开源字体,提供多种字重,适合现代网页设计。
</div>
</div>
<div class="font-card">
<div class="font-name">思源宋体 (Noto Serif SC)</div>
<div class="font-sample" style="font-family: 'Noto Serif SC', serif;">
思源宋体是衬线字体,适用于正文阅读,提供优雅的排版效果。
</div>
</div>
<div class="font-card">
<div class="font-name">阿里巴巴普惠体</div>
<div class="font-sample" style="font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', sans-serif;">
阿里巴巴推出的免费商用字体,简洁现代,适合多种场景。
</div>
</div>
<div class="font-card">
<div class="font-name">方正仿宋</div>
<div class="font-sample" style="font-family: 'FangSong', '仿宋', serif;">
仿宋字体具有传统印刷风格,适合正式文档和文化类网站。
</div>
</div>
</div>
<div class="browser-support">
<h3>浏览器兼容性注意事项</h3>
<p>为了确保在所有现代浏览器中字体显示一致,请遵循以下实践:</p>
<ul>
<li>使用WOFF2格式作为首选,WOFF作为备选</li>
<li>为旧版浏览器提供备用字体</li>
<li>使用@font-face规则时包含所有必要的字重</li>
<li>测试在IE11、Edge、Chrome、Firefox和Safari中的显示效果</li>
</ul>
<div class="browser-list">
<div class="browser">
<span>Chrome</span>
</div>
<div class="browser">
<span>Firefox</span>
</div>
<div class="browser">
<span>Safari</span>
</div>
<div class="browser">
<span>Edge</span>
</div>
<div class="browser">
<span>IE 11</span>
</div>
</div>
</div>
<div class="tip">
<h4>性能优化建议:</h4>
<p>使用<code>font-display: swap;</code>属性确保文本在字体加载期间保持可见,避免布局偏移,同时限制使用2-3种字体变体以优化页面加载速度。</p>
</div>
</div>
<div class="sidebar">
<h2>最佳实践总结</h2
