<meta> 标签
<meta> 标签提供了关于 HTML 文档的元数据(metadata)。元数据是关于数据的数据,不会显示在页面上,但会被浏览器和搜索引擎解析。
概述
<meta> 标签定义了文档级别的元数据,这些数据不会显示在页面内容中,但对浏览器、搜索引擎和其他网络服务非常重要。
语法
html
<meta 属性名="属性值" content="内容">常用属性
charset 属性
定义文档的字符编码:
html
<meta charset="UTF-8">name 和 content 属性
定义文档级元数据的名称和内容:
html
<meta name="description" content="页面描述">
<meta name="keywords" content="关键词1, 关键词2">
<meta name="author" content="作者名称">property 和 content 属性
用于 Open Graph 协议:
html
<meta property="og:title" content="页面标题">
<meta property="og:description" content="页面描述">http-equiv 和 content 属性
提供关于文档指令的信息:
html
<meta http-equiv="refresh" content="30">基本用法示例
字符编码
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>页面标题</title>
</head>
<body>
<h1>页面内容</h1>
</body>
</html>页面描述
html
<head>
<meta charset="UTF-8">
<meta name="description" content="这是一个关于网页开发的教程网站,提供 HTML、CSS 和 JavaScript 教程">
<title>网页开发教程</title>
</head>关键词
html
<head>
<meta charset="UTF-8">
<meta name="keywords" content="HTML, CSS, JavaScript, 教程, 网页开发">
<title>网页开发教程</title>
</head>重要元数据类型
视口设置(响应式设计)
html
<meta name="viewport" content="width=device-width, initial-scale=1.0">这个元标签对响应式网页设计至关重要,它告诉浏览器如何控制页面尺寸和缩放。
作者信息
html
<meta name="author" content="张三">页面刷新
html
<!-- 30秒后刷新页面 -->
<meta http-equiv="refresh" content="30">
<!-- 5秒后跳转到另一个页面 -->
<meta http-equiv="refresh" content="5;url=https://example.com">兼容性设置
html
<!-- IE 浏览器兼容性设置 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">SEO 相关元数据
页面描述
html
<meta name="description" content="学习 HTML、CSS 和 JavaScript 的最佳资源,从基础到高级教程">机器人指令
html
<!-- 控制搜索引擎爬虫行为 -->
<meta name="robots" content="index, follow">
<meta name="robots" content="noindex, nofollow">
<meta name="robots" content="index, nofollow">
<meta name="robots" content="noindex, follow">生成器信息
html
<meta name="generator" content="WordPress 5.8">社交媒体元数据
Open Graph 协议(Facebook)
html
<meta property="og:title" content="页面标题">
<meta property="og:description" content="页面描述">
<meta property="og:image" content="https://example.com/image.jpg">
<meta property="og:url" content="https://example.com/page">
<meta property="og:type" content="website">Twitter 卡片
html
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="页面标题">
<meta name="twitter:description" content="页面描述">
<meta name="twitter:image" content="https://example.com/image.jpg">
<meta name="twitter:site" content="@username">移动设备相关
禁止自动检测电话号码
html
<meta name="format-detection" content="telephone=no">全屏模式(iOS)
html
<meta name="apple-mobile-web-app-capable" content="yes">状态栏样式(iOS)
html
<meta name="apple-mobile-web-app-status-bar-style" content="black">完整示例
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<!-- 字符编码 -->
<meta charset="UTF-8">
<!-- IE 兼容性 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- 响应式设计 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 页面标题 -->
<title>网页开发教程</title>
<!-- 页面描述 -->
<meta name="description" content="全面的 HTML、CSS、JavaScript 教程,帮助你从零开始学习网页开发">
<!-- 关键词 -->
<meta name="keywords" content="HTML, CSS, JavaScript, 教程, 网页开发, 前端开发">
<!-- 作者 -->
<meta name="author" content="张三">
<!-- 搜索引擎机器人指令 -->
<meta name="robots" content="index, follow">
<!-- Open Graph 数据(Facebook) -->
<meta property="og:title" content="网页开发教程">
<meta property="og:description" content="全面的 HTML、CSS、JavaScript 教程">
<meta property="og:image" content="https://example.com/og-image.jpg">
<meta property="og:url" content="https://example.com">
<meta property="og:type" content="website">
<!-- Twitter 卡片 -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="网页开发教程">
<meta name="twitter:description" content="全面的 HTML、CSS、JavaScript 教程">
<meta name="twitter:image" content="https://example.com/twitter-image.jpg">
<meta name="twitter:site" content="@example">
<!-- 移动设备设置 -->
<meta name="format-detection" content="telephone=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
</head>
<body>
<h1>网页开发教程</h1>
<p>欢迎学习网页开发!</p>
</body>
</html>最佳实践
- 始终包含
charset元标签 - 使用
viewport元标签实现响应式设计 - 提供有意义的页面描述
- 合理使用关键词元标签
- 为社交媒体分享添加 Open Graph 和 Twitter 卡片数据
- 避免关键词堆砌
- 保持元数据的准确性和时效性
浏览器兼容性
<meta> 标签在所有浏览器中都得到支持:
- Chrome
- Firefox
- Safari
- Edge
- Internet Explorer
不同属性的支持情况略有差异,但基本功能在所有现代浏览器中都能正常工作。
注意事项
<meta>标签必须放在<head>部分<meta>标签没有结束标签(在 HTML5 中)- 不同的元数据类型有不同的属性组合
- 过多的元数据可能影响页面加载速度
- 某些元数据可能被搜索引擎忽略