大家好,我是程序员柏风,AI 最全的 Vibecoding 学习地图 http://coderoadmap.cn 作者。
HTML SEO 综合
这一课你将能做到
- 写出符合 SEO 最佳实践的 title、meta description、canonical 链接,知道各字段的长度和写法规范
- 为页面添加 Open Graph 和 Twitter Card 元数据,让链接在社交平台正确预览
- 用 JSON-LD 编写结构化数据,让搜索引擎理解页面类型、作者、评分、面包屑
为什么要学
SEO(搜索引擎优化)是免费流量的最大来源。一个技术博客,搜索引擎贡献的流量通常占 60% 以上。但 SEO 不是「堆砌关键词」,而是让搜索引擎正确理解你的内容——标题是入口,描述是广告词,结构化数据是内容摘要,语义化标签是内容地图。HTML 层面的 SEO 做得好,内容质量才能被正确传递。
人话:SEO 就是给搜索引擎写「产品说明书」。你的产品再好,说明书没写清楚,客户(用户)根本找不到你。
先建立图像
SEO 的四大战场:
搜索结果显示
├── title:蓝色大标题,点击率关键
├── meta description:灰色摘要,决定用户是否点击
└── 富摘要(Rich Snippets):评分、价格、面包屑、作者
社交分享预览
├── Open Graph:微信、Facebook、Twitter 卡片
└── Twitter Card:Twitter/X 专用格式
技术层
├── canonical:解决重复内容
├── robots meta:控制索引
└── sitemap.xml:主动提交 URL
跟着做
步骤 1:标题与描述优化
新建 seo.html:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 标题:50-60 字符,核心关键词靠前,品牌靠后 -->
<title>HTML SEO 完整指南:从入门到精通 | 技术博客</title>
<!-- 描述:150-160 字符,包含关键词,有行动号召 -->
<meta name="description" content="掌握 HTML SEO 的核心技巧:标题优化、meta description、结构化数据、Open Graph。本文提供可运行的代码示例和检查清单,帮助你的网站在搜索引擎获得更好排名。">
<!-- 规范链接:防止重复内容 -->
<link rel="canonical" href="https://example.com/html-seo-guide">
<!-- robots:控制索引 -->
<meta name="robots" content="index, follow, max-image-preview:large">
</head>
<body>
<h1>HTML SEO 完整指南</h1>
<p>本文介绍……</p>
</body>
</html>
title 优化原则:
- 长度:50-60 字符(约 25-30 汉字),超长会被截断显示
... - 结构:
核心关键词 - 次要关键词 | 品牌名 - 唯一:全站每个页面 title 必须唯一,重复 title 会被搜索引擎降权
- 前置:把用户搜索意图最强的词放最前面
description 优化原则:
- 长度:150-160 字符(约 75-80 汉字)
- 包含:核心关键词、用户收益点、行动号召
- 不写:「本文介绍……」「这篇文章讲的是……」这种空话
步骤 2:Open Graph 社交卡片
<head>
<!-- Open Graph:微信、Facebook、LinkedIn、Twitter 通用 -->
<meta property="og:type" content="article">
<meta property="og:title" content="HTML SEO 完整指南:从入门到精通">
<meta property="og:description" content="掌握 HTML SEO 的核心技巧……">
<meta property="og:url" content="https://example.com/html-seo-guide">
<meta property="og:image" content="https://example.com/images/og-cover.jpg">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<meta property="og:site_name" content="技术博客">
<meta property="og:locale" content="zh_CN">
<!-- 文章专属 OG -->
<meta property="article:published_time" content="2026-10-06T08:00:00+08:00">
<meta property="article:modified_time" content="2026-10-06T10:30:00+08:00">
<meta property="article:author" content="张三">
<meta property="article:section" content="前端开发">
<meta property="article:tag" content="HTML,SEO,前端">
<!-- Twitter Card:Twitter/X 专用 -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="HTML SEO 完整指南">
<meta name="twitter:description" content="掌握 HTML SEO 的核心技巧……">
<meta name="twitter:image" content="https://example.com/images/og-cover.jpg">
</head>
og:image 规范:
- 尺寸:1200×630(1.91:1 比例)
- 格式:JPG 或 PNG,大小 < 8MB
- 绝对 URL,不要用相对路径
- 图片上不要放太多文字(社交平台可能裁切)
测试工具:
- Facebook Sharing Debugger
- Twitter Card Validator
- 微信:直接发送链接看预览
步骤 3:JSON-LD 结构化数据
<head>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "HTML SEO 完整指南:从入门到精通",
"description": "掌握 HTML SEO 的核心技巧:标题优化、meta description、结构化数据、Open Graph。",
"image": "https://example.com/images/og-cover.jpg",
"datePublished": "2026-10-06T08:00:00+08:00",
"dateModified": "2026-10-06T10:30:00+08:00",
"author": {
"@type": "Person",
"name": "张三",
"url": "https://example.com/author/zhangsan"
},
"publisher": {
"@type": "Organization",
"name": "技术博客",
"logo": {
"@type": "ImageObject",
"url": "https://example.com/logo.png"
}
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://example.com/html-seo-guide"
}
}
</script>
</head>
JSON-LD 是 Google 推荐的结构化数据格式,放在 head 或 body 里,不显示在页面上。常用 Schema 类型:
| 类型 | 用途 | 关键字段 |
|---|---|---|
| Article | 文章 | headline, author, datePublished, image |
| Product | 商品 | name, offers(price, priceCurrency), aggregateRating |
| FAQPage | 常见问题 | mainEntity(Question/Answer) |
| BreadcrumbList | 面包屑 | itemListElement(ListItem) |
| Organization | 组织 | name, logo, contactPoint |
| WebSite | 网站 | name, url, potentialAction(SearchAction) |
FAQPage 示例:
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "HTML SEO 是什么?",
"acceptedAnswer": {
"@type": "Answer",
"text": "HTML SEO 是通过优化网页的 HTML 结构和元数据……"
}
},
{
"@type": "Question",
"name": "title 标签多长合适?",
"acceptedAnswer": {
"@type": "Answer",
"text": "建议 50-60 字符,约 25-30 个汉字……"
}
}
]
}
</script>
面包屑结构化数据:
<nav aria-label="面包屑">
<ol itemscope itemtype="https://schema.org/BreadcrumbList">
<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
<a itemprop="item" href="https://example.com/">
<span itemprop="name">首页</span>
</a>
<meta itemprop="position" content="1">
</li>
<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
<a itemprop="item" href="https://example.com/html/">
<span itemprop="name">HTML</span>
</a>
<meta itemprop="position" content="2">
</li>
<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
<span itemprop="name">SEO 指南</span>
<meta itemprop="position" content="3">
</li>
</ol>
</nav>
步骤 4:canonical 与 robots
<head>
<!-- 规范链接:解决重复内容 -->
<!-- 场景:同一内容多个 URL(带参数、打印版、AMP 版) -->
<link rel="canonical" href="https://example.com/html-seo-guide">
<!-- 控制搜索引擎行为 -->
<!-- index: 收录本页 | noindex: 不收录 -->
<!-- follow: 跟踪链接 | nofollow: 不跟踪链接 -->
<meta name="robots" content="index, follow">
<!-- 特定搜索引擎 -->
<meta name="googlebot" content="index, follow, max-snippet:-1, max-image-preview:large">
<meta name="bingbot" content="index, follow">
<!-- 页面级 noindex(如搜索结果页、标签页) -->
<meta name="robots" content="noindex, follow">
</head>
canonical 使用场景:
https://example.com/page?utm_source=weibo→ canonical 指向https://example.com/pagehttps://example.com/page?sort=price→ canonical 指向https://example.com/page- 打印版、AMP 版 → canonical 指向标准版
步骤 5:语义化与内容结构
搜索引擎爬虫和屏幕阅读器一样,依赖语义化标签理解内容:
<main>
<article>
<header>
<h1>HTML SEO 完整指南</h1>
<time datetime="2026-10-06">2026年10月6日</time>
</header>
<section>
<h2>标题优化</h2>
<p>……</p>
</section>
<section>
<h2>结构化数据</h2>
<p>……</p>
</section>
<footer>
<p>作者:张三 | 分类:前端开发</p>
</footer>
</article>
</main>
h1一个页面一个,是页面主题最强信号h2-h6构成内容大纲,搜索引擎据此理解章节结构article标识独立完整内容,可能被 Google Discover 推荐time datetime提供机器可读的发布日期
讲清楚要点
- title 是 SEO 的第一战场:搜索结果中蓝色大标题,直接影响点击率。长度、关键词位置、唯一性,三条铁律。
- description 是广告文案:不直接参与排名,但影响点击率。写作用户视角的「收益点 + 行动号召」,不是内容摘要。
- Open Graph 是社交名片:没 OG 的链接分享出去是纯文本,有 OG 的链接是带图卡片,点击率差 5-10 倍。
- JSON-LD 是内容说明书:告诉搜索引擎「这是文章」「这是商品」「这是 FAQ」,从而获得富摘要展示。富摘要的点击率比普通结果高 30%。
- canonical 解决重复内容:搜索引擎惩罚「内容农场」(同一内容多 URL)。canonical 告诉它「正主在这里」。
你可能会懵的地方
-
meta keywords 还有用吗? Google 2009 年明确宣布忽略 meta keywords。不要写,写了可能被判定为关键词堆砌作弊。
-
description 不参与排名,为什么还重要? 排名是「能不能被找到」,点击率是「被找到后点不点」。description 写得好,同样排名的页面点击率高 20-50%,长期点击率好会反哺排名(用户行为信号)。
-
og:image 和 twitter:image 可以共用吗? 可以,Twitter 会 fallback 到 og:image。但建议单独写 twitter:image,因为 Twitter 对图片尺寸要求略有不同(推荐 1200×600)。
-
结构化数据错误会被惩罚吗? 不会直接惩罚,但错误的数据可能导致搜索引擎忽略你的标记,失去富摘要机会。用 Google Rich Results Test 验证。
-
noindex 和 robots.txt 的 Disallow 什么区别? robots.txt 是「别爬我」,但已收录的页面可能保留摘要;noindex 是「别收录我」,已收录的会移除。敏感页面用 noindex,后台路径用 robots.txt。
错法对照
| 错法 | 问题 | 正解 |
|---|---|---|
| 全站 title 相同 | 搜索引擎无法区分页面主题 | 每页唯一 title,关键词前置 |
| description 写「本文介绍……」 | 无信息量,点击率低 | 写收益点 + 行动号召 |
| 图片用相对路径 og:image | 社交平台解析失败 | 绝对 URL,https 完整路径 |
| 同一内容多个 URL 不加 canonical | 被判定重复内容,权重分散 | 每个变体页加 canonical 指向正主 |
| 后台管理页被收录 | 安全风险,权重浪费 | noindex + robots.txt 双重防护 |
| 关键词堆砌 title | 被判定作弊,降权 | 自然语言,1-2 个核心关键词 |
卡点急救
- 分享微信/朋友圈无图:检查 og:image 是否是绝对 URL、尺寸是否 1200×630、服务器是否允许外部爬虫访问(无防盗链)。
- 结构化数据测试不通过:用 Google Rich Results Test 验证,常见错误:JSON 语法错误、@context 拼写、必需字段缺失。
- canonical 不生效:canonical 是「建议」不是「指令」,搜索引擎可能忽略。确保所有变体 URL 内容高度相似,且 canonical 指向可访问页面。
- title 在搜索结果中被改写:Google 认为你的 title 不够好时,会抓取页面内容自动改写。解决方法:确保 h1 和 title 一致,内容质量高。
- description 显示的是页面正文:Google 认为你的 description 与搜索词不匹配时,会抓取正文片段。优化:在 description 中包含用户可能搜索的关键词。
面试常问
-
title 和 meta description 的最佳实践? title:50-60 字符,核心关键词前置,全站唯一,品牌名靠后。description:150-160 字符,包含关键词和用户收益点,有行动号召,不写空话。两者都直接影响搜索结果的点击率。
-
什么是 Open Graph? Facebook 2010 年发起的协议,通过 meta property="og:*" 标签定义社交分享的标题、描述、图片、类型。微信、Twitter、LinkedIn 都支持。没有 OG,分享链接是纯文本;有 OG,是带图卡片。
-
结构化数据(Schema.org)的作用? 用标准化词汇告诉搜索引擎页面内容的类型和属性(文章、商品、事件、FAQ)。好处:① 获得富摘要(评分、价格、面包屑),提高点击率;② 帮助搜索引擎理解内容,可能获得知识图谱展示;③ 支持语音助手直接回答。
-
canonical 标签解决什么问题? 重复内容问题。同一内容可能通过多个 URL 访问(带参数、打印版、AMP 版、HTTP/HTTPS、www/非 www),搜索引擎会分散权重甚至判定作弊。canonical 指定「规范 URL」,集中权重。
自测三问
- 写出优化后的 title 和 meta description(假设文章主题是「Vue3 组合式 API 入门」)。
- 为什么 og:image 必须用绝对 URL?
- JSON-LD 中 @type: Article 和 @type: BlogPosting 什么区别?
小结一句话
HTML SEO 是技术也是内容:title 和 description 写给人看,结构化数据和语义标签写给机器看,canonical 和 robots 写给搜索引擎看——三方都满意,流量自然来。
延伸
- Google Search Central:SEO 基础
- Schema.org 完整词汇表
- 本课程到此结束,建议继续学习:CSS 布局与响应式设计。