<meta>
?標簽是 HTML 文檔頭部(<head>
?部分)的重要元素,用于提供關于文檔的元數據(metadata)。這些數據不會直接顯示在頁面上,但對瀏覽器、搜索引擎和其他服務非常重要。
常用屬性
1.?name
?和?content
?屬性組合
這是最常用的?<meta>
?標簽形式,用于指定各種元數據類型:
<meta name="屬性名稱" content="屬性值">
常見?name
?值:
-
viewport
: 控制移動設備上的視口行為
<meta name="viewport" content="width=device-width, initial-scale=1.0">
keywords
: 網頁關鍵詞(搜索引擎優化)
<meta name="keywords" content="HTML, CSS, JavaScript, Web開發">
description
: 網頁描述(搜索引擎優化)
<meta name="description" content="學習HTML、CSS和JavaScript的免費教程">
author
: 作者信息
<meta name="author" content="張三">
robots
: 指示搜索引擎如何索引頁面?
<meta name="robots" content="index, follow">
# 可選值:index/noindex, follow/nofollow, noarchive, nosnippet 等
- ?
og:
?(Open Graph): 用于社交媒體分享
<meta name="og:title" content="頁面標題">
<meta name="og:description" content="頁面描述">
<meta name="og:image" content="圖片URL">
2.?http-equiv
?屬性
模擬 HTTP 響應頭字段:
<meta http-equiv="屬性名" content="屬性值">
常見?http-equiv
?值:
-
content-type
: 指定字符編碼(已過時,推薦使用?<meta charset>
)
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
X-UA-Compatible
: 指定 IE 瀏覽器使用何種引擎渲染頁面
<meta http-equiv="X-UA-Compatible" content="IE=edge">
refresh
: 頁面自動刷新或重定向
<meta http-equiv="refresh" content="30"> <!-- 每30秒刷新 -->
<meta http-equiv="refresh" content="5; url=https://example.com"> <!-- 5秒后跳轉 -->
pragma
: 防止緩存(已過時)
<meta http-equiv="pragma" content="no-cache">
3.?charset
?屬性
指定文檔的字符編碼(HTML5 新增):
<meta charset="UTF-8">
其他屬性
-
scheme
: 指定?content
?屬性的格式(很少使用)
<meta name="date" content="2023-01-01" scheme="YYYY-MM-DD">
-
lang
: 指定內容的語言(通常使用?<html lang="">
?代替)
最佳實踐
- 字符編碼:始終在?
<head>
?的最前面包含?<meta charset="UTF-8">
- 視口設置:對于響應式設計,必須包含視口 meta 標簽
- 描述:提供有意義的描述以提高 SEO
- 避免過時屬性:如?
http-equiv="content-type"
?已被?<meta charset>
?取代
完整示例
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="description" content="這是一個關于HTML meta標簽的詳細說明頁面"><meta name="keywords" content="HTML, meta標簽, 元數據, SEO"><meta name="author" content="Web開發教程"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Meta標簽詳解</title>
</head>
<body><!-- 頁面內容 -->
</body>
</html>
<meta>
?標簽雖然不直接顯示內容,但對網頁的可訪問性、SEO 和功能實現至關重要。