HTML 的 head 部分涵蓋了大量標籤與元素,牽涉到瀏覽器渲染、SEO 等層面。由於各家瀏覽器內核與國內廠商都有自己的擴充標籤,差異性相當大。到了行動網路時代,head 的結構、行動端的 meta 元素顯得更為關鍵。本文以一絲的文章為基礎加以擴充,整理常用 head 標籤的意義與使用時機。
DOCTYPE(Document Type)位於文件最前方、html 標籤之前,用來告知瀏覽器文件採用哪種 HTML 或 XHTML 規範。DTD 聲明以 <!DOCTYPE> 開頭、不分大小寫,前方不可有其他內容(空白除外),否則會讓 IE 進入怪異模式(quirks mode)渲染。
公共 DTD 的名稱格式為「註冊/組織/類型 標籤/語言」:註冊表示組織是否由 ISO 認證(+ 是、- 否),組織即組織名稱如 W3C,類型一般為 DTD,標籤是公開文字描述並可附版本號,語言則是 ISO 639 語言代碼(EN 英文、ZH 中文)。XHTML 1.0 可宣告嚴格、過渡、框架三種 DTD:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "https://www.w3.org/TR/html4/strict.dtd"> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd"> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "https://www.w3.org/TR/html4/frameset.dtd"> <!doctype html> <!-- HTML5,向前向後相容,推薦 -->
doctype 有兩個主要目的:一是對文件進行有效性驗證(被動,僅在手動校驗時啟用);二是決定瀏覽器的呈現模式,共有非怪異(標準)、怪異、部分怪異(近乎標準)三種解析方式。若未撰寫,瀏覽器會依自身規則解析,可能嚴重影響排版。
宣告文件字元編碼,HTML5 寫法為 <meta charset="utf-8">,舊寫法為 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">,兩者等效,建議採用較短好記的版本。
<html lang="zh-cmn-Hans"> <!-- 簡體中文 --> <html lang="zh-cmn-Hant"> <!-- 繁體中文 -->
之所以寫 zh-cmn-Hans 而非 zh-CN,是因為前者語意更標準。
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <meta name="renderer" content="webkit"> <!-- 360 使用 Chrome Frame -->
如此一來,若安裝了 Google Chrome Frame 便以 GCF 渲染,否則使用最高版本的 IE 內核。
行動裝置開啟網頁時,百度可能對頁面轉碼並插入廣告,可加入以下標籤避免:
<meta http-equiv="Cache-Control" content="no-siteapp" />
<title>your title</title> <meta name="keywords" content="your keywords"> <meta name="description" content="your description"> <meta name="author" content="author,email address"> <meta name="robots" content="index,follow">
其中 robots 的 robotterms 以英文逗號分隔,常見值有 none、noindex、nofollow、all、index、follow。
<meta name="viewport" content="width=device-width, initial-scale=1.0">
參數包含 width、height、initial-scale、maximum-scale、minimum-scale、user-scalable、minimal-ui 等。若非響應式網站,請勿使用 initial-scale 或停用縮放。
<meta name="apple-mobile-web-app-title" content="標題"> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> <meta name="format-detection" content="telephone=no" />
狀態欄樣式 content 值有 default、black、black-translucent 三種,僅在 apple-mobile-web-app-capable 為 yes 時生效。
apple-touch-icon 會自動套用圓角與高光,apple-touch-icon-precomposed 則直接顯示原圖。各尺寸如下:
<link rel="apple-touch-icon-precomposed" href="/apple-touch-icon-57x57-precomposed.png" /> <!-- iPhone/iTouch 57x57 --> <link rel="apple-touch-icon-precomposed" sizes="72x72" href="/apple-touch-icon-72x72-precomposed.png" /> <!-- iPad 72x72 --> <link rel="apple-touch-icon-precomposed" sizes="114x114" href="/apple-touch-icon-114x114-precomposed.png" /> <!-- Retina iPhone 114x114 --> <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144x144-precomposed.png" /> <!-- Retina iPad 144x144 --> <link rel="apple-touch-icon-precomposed" sizes="180x180" href="retinahd_icon.png"> <!-- iPhone 6 Plus 180x180 -->
iPad 的啟動畫面不含狀態欄區域,iPhone/iPod touch 則含狀態欄。常用寫法如:
<link rel="apple-touch-startup-image" sizes="768x1004" href="/splash-screen-768x1004.png" /> <!-- iPad 豎屏 --> <link rel="apple-touch-startup-image" href="/splash-screen-320x480.png" /> <!-- iPhone 豎屏 --> <link rel="apple-touch-startup-image" sizes="640x1136" href="/splash-screen-640x1136.png" /> <!-- iPhone 5 -->
iPhone 6 對應 750×1294、iPhone 6 Plus 對應 1242×2148,可用 media 查詢區分。
<meta name="msapplication-TileColor" content="#000"/> <meta name="msapplication-TileImage" content="icon.png"/> <link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml" /> <link rel="shortcut icon" type="image/ico" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta name="format-detection" content="telephone=no, email=no" /> <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="HandheldFriendly" content="true"> <meta name="MobileOptimized" content="320"> <meta name="screen-orientation" content="portrait"> <meta name="x5-orientation" content="portrait"> <meta name="full-screen" content="yes"> <meta name="x5-fullscreen" content="true"> <meta name="browsermode" content="application"> <meta name="x5-page-mode" content="app"> <meta name="msapplication-tap-highlight" content="no">
以上整理自 toobug 的分享,涵蓋桌面與行動情境中最常見的 head 標籤寫法。