<head> 是網頁的大腦,包含文檔元數據、資源引入、行為配置。掌握每個標籤的正確用法,是構建標準、高效、SEO 友好頁面的基礎。
核心標籤速查表
| 標籤 | 用途 | 關鍵屬性 |
<title> | 文檔標題,唯一,顯示於瀏覽器標籤、書籤、搜尋結果 | 無 |
<link> | 外部資源關聯,最常用於樣式表 | rel="stylesheet", href, type, media |
<style> | 內部樣式表 | type="text/css", media |
<base> | 指定頁面所有相對連結的基準 URL 與目標 | href, target |
<script> | 嵌入或引入 JavaScript | src, type, async, defer, charset |
<meta> | 元數據,模擬 HTTP 響應頭或提供文檔資訊 | name, http-equiv, content, charset |
<meta> 兩大屬性體系詳解
1. name 屬性:面向搜尋引擎與瀏覽器
| name 值 | 用途 | 範例 |
keywords | 頁面關鍵字(現搜尋引擎權重極低) | <meta name="keywords" content="HTML, CSS, 教學"> |
description | 頁面描述,搜尋結果摘要來源 | <meta name="description" content="HTML head 完整指南..."> |
author | 作者資訊 | <meta name="author" content="張三"> |
generator | 生成工具 | <meta name="generator" content="VS Code"> |
robots | 爬蟲指令:index/noindex, follow/nofollow, all/none | <meta name="robots" content="index,follow"> |
viewport | 行動裝置視口設定(響應式必備) | <meta name="viewport" content="width=device-width, initial-scale=1"> |
2. http-equiv 屬性:模擬 HTTP 響應頭
| http-equiv 值 | 用途 | 範例 |
Content-Type | 指定字符編碼(HTML5 建議用 <meta charset="utf-8">) | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
Content-Language | 文檔語言 | <meta http-equiv="Content-Language" content="zh-TW"> |
Refresh | 定時重導向 | <meta http-equiv="Refresh" content="5; url=https://example.com"> |
Expires | 緩存過期時間(GMT 格式) | <meta http-equiv="Expires" content="Mon, 12 May 2025 00:20:00 GMT"> |
Pragma | 禁用緩存(HTTP/1.0 相容) | <meta http-equiv="Pragma" content="no-cache"> |
Set-Cookie | 設定 Cookie(建議用 HTTP 標頭) | <meta http-equiv="Set-Cookie" content="name=value; expires=..."> |
X-UA-Compatible | 指定 IE 渲染模式 | <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
Page-Enter/Exit | 頁面進出轉場效果(IE 專屬,已廢棄) | <meta http-equiv="Page-Enter" content="revealTrans(duration=1,transition=12)"> |
現代最佳實踐精簡版
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="頁面精準描述,150 字內">
<title>頁面標題 | 網站名稱</title>
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
<script src="app.js" defer></script>
</head>
原則:最小化 head、優先外部資源、善用 defer/async、移除廢棄 http-equiv(轉用真實 HTTP 標頭)。