示例工程 04-head-metadata
04-head-metadata/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>head 与元数据 · HTML 笔记</title>
<meta name="description" content="charset、viewport、title、description、favicon、OG 与 robots 的最小示例。" />
<meta name="theme-color" content="#2563eb" />
<meta name="robots" content="index, follow" />
<meta property="og:title" content="head 与元数据" />
<meta property="og:description" content="head 里的标签如何影响搜索、分享与移动端。" />
<meta property="og:type" content="article" />
<meta name="twitter:card" content="summary" />
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='8' fill='%232563eb'/%3E%3Cpath d='M9 10h14v3H9zm0 6h10v3H9z' fill='white'/%3E%3C/svg%3E" />
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
padding: 32px 20px;
background: #f8fafc;
color: #0f172a;
font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
line-height: 1.75;
}
.page { width: min(100%, 780px); margin: 0 auto; }
.site-header {
display: flex;
justify-content: space-between;
gap: 16px;
padding-bottom: 14px;
border-bottom: 1px solid #e2e8f0;
}
.brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
nav a + a { margin-left: 14px; }
main { padding: 32px 0; }
.eyebrow { color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
h1 { margin: 6px 0 12px; font-size: clamp(30px, 6vw, 46px); line-height: 1.1; }
.lead { margin: 0 0 20px; color: #334155; font-size: 17px; }
section { margin-top: 24px; padding: 20px; border: 1px solid #e2e8f0; border-radius: 14px; background: #fff; }
h2 { margin: 0 0 10px; font-size: 20px; }
h3 { margin: 14px 0 6px; font-size: 15px; }
p { margin: 0 0 10px; color: #334155; }
ul, ol { margin: 8px 0 0; padding-left: 22px; color: #334155; }
li + li { margin-top: 4px; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
pre {
margin: 10px 0 0;
padding: 14px;
overflow-x: auto;
border-radius: 10px;
background: #0f172a;
color: #e2e8f0;
font-size: 12px;
line-height: 1.7;
}
pre code { padding: 0; background: none; color: inherit; }
table { width: 100%; margin-top: 10px; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; border-bottom: 1px solid #e2e8f0; text-align: left; vertical-align: top; }
th { background: #f1f5f9; font-weight: 600; }
.preview { max-width: 420px; overflow: hidden; border: 1px solid #e2e8f0; border-radius: 12px; background: #f8fafc; }
.preview__media { height: 120px; background: linear-gradient(135deg, #2563eb, #7c3aed); }
.preview__body { padding: 12px 14px; }
.preview__site { color: #64748b; font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; }
.preview__title { margin: 4px 0; font-size: 16px; font-weight: 700; }
.preview__desc { margin: 0; color: #64748b; font-size: 12px; }
footer { padding-top: 18px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
@media (max-width: 560px) {
.site-header { flex-direction: column; }
table { display: block; overflow-x: auto; }
}
</style>
</head>
<body>
<div class="page">
<header class="site-header">
<span class="brand">HTML NOTES</span>
<nav aria-label="页面导航">
<a href="#tags">标签</a>
<a href="#social">分享</a>
<a href="#checklist">检查</a>
</nav>
</header>
<main>
<p class="eyebrow">LESSON / 04</p>
<h1>head 与元数据</h1>
<p class="lead">
head 不直接显示,却决定编码、移动端宽度、标签页标题、搜索摘要与分享卡片。
</p>
<section id="tags">
<h2>1. 常用 head 标签</h2>
<table>
<thead>
<tr><th>标签</th><th>作用</th></tr>
</thead>
<tbody>
<tr><td><code>meta charset</code></td><td>文档编码</td></tr>
<tr><td><code>meta viewport</code></td><td>移动端视口</td></tr>
<tr><td><code>title</code></td><td>标签页与搜索结果标题</td></tr>
<tr><td><code>meta description</code></td><td>搜索摘要</td></tr>
<tr><td><code>meta theme-color</code></td><td>移动浏览器主题色</td></tr>
<tr><td><code>link rel="icon"</code></td><td>标签页图标</td></tr>
<tr><td><code>meta robots</code></td><td>是否索引</td></tr>
</tbody>
</table>
<pre><code><meta name="description" content="一句话描述。" />
<meta name="theme-color" content="#2563eb" />
<link rel="icon" href="/favicon.ico" /></code></pre>
</section>
<section id="social">
<h2>2. 分享元数据</h2>
<p>OG 标签决定社交平台预览卡的标题、描述与图片。</p>
<pre><code><meta property="og:title" content="页面标题" />
<meta property="og:description" content="一句话描述。" />
<meta property="og:type" content="article" /></code></pre>
<div class="preview" aria-label="分享卡片预览">
<div class="preview__media" aria-hidden="true"></div>
<div class="preview__body">
<span class="preview__site">example.com</span>
<p class="preview__title">head 与元数据</p>
<p class="preview__desc">head 里的标签如何影响搜索、分享与移动端。</p>
</div>
</div>
</section>
<section id="checklist">
<h2>3. 检查清单</h2>
<ul>
<li>charset 在 head 前部。</li>
<li>viewport 存在。</li>
<li>title 每页唯一。</li>
<li>description 不重复。</li>
<li>favicon 路径正确。</li>
<li>草稿页 noindex。</li>
</ul>
</section>
</main>
<footer>
<p>示例数据,仅用于教程演示。</p>
</footer>
</div>
</body>
</html>