HTML head 頭部分的標籤、元素有不少,涉及到瀏覽器對網頁的渲染,SEO 等等,而各個瀏覽器內核以及各個國內瀏覽器廠商都有些本身的標籤元素,這就形成了不少差別性。移動互聯網時代,head 頭部結構,移動端的 meta 元素,顯得更爲重要。瞭解每一個標籤的意義,寫出知足本身需求的 head 頭標籤,是本文的目的。本篇以一絲的文章爲基礎,進行擴展總結介紹經常使用的 head 中各個標籤、元素的意義以及使用場景。html
DOCTYPE(Document Type),該聲明位於文檔中最前面的位置,處於 html 標籤以前,此標籤告知瀏覽器文檔使用哪一種 HTML 或者 XHTML 規範。html5
DTD(Document Type Definition) 聲明以 <!DOCTYPE> 開始,不區分大小寫,前面沒有任何內容,若是有其餘內容(空格除外)會使瀏覽器在 IE 下開啓怪異模式(quirks mode)渲染網頁。公共 DTD,名稱格式爲註冊//組織//類型 標籤//語言,註冊指組織是否由國際標準化組織(ISO)註冊,+表示是,-表示不是。組織即組織名稱,如:W3C。類型通常是 DTD。標籤是指定公開文本描述,即對所引用的公開文本的惟一描述性名稱,後面可附帶版本號。最後語言是 DTD 語言的 ISO 639 語言標識符,如:EN 表示英文,ZH 表示中文。XHTML 1.0 可聲明三種 DTD 類型。分別表示嚴格版本,過渡版本,以及基於框架的 HTML 文檔。ios
1
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
1
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
1
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
|
1
|
<!doctype html>
|
在 HTML中 doctype 有兩個主要目的。git
它告訴用戶代理和校驗器這個文檔是按照什麼 DTD 寫的。這個動做是被動的,每次頁面加載時,瀏覽器並不會下載 DTD 並檢查合法性,只有當手動校驗頁面時才啓用。github
聲明文檔使用的字符編碼,web
1
|
<meta charset="utf-8">
|
html5 以前網頁中會這樣寫:算法
1
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
這兩個是等效的,具體可移步閱讀:<meta charset='utf-8'> vs <meta http-equiv='Content-Type'>,因此建議使用較短的,易於記憶。chrome
簡體中文windows
1
|
<html lang="zh-cmn-Hans">
|
繁體中文瀏覽器
1
|
<html lang="zh-cmn-Hant">
|
爲何 lang="zh-cmn-Hans" 而不是咱們一般寫的 lang="zh-CN" 呢,請移步閱讀: 頁頭部的聲明應該是用 lang=」zh」 仍是 lang=」zh-cn」。
1
|
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
|
1
|
<meta name="renderer" content="webkit">
|
360 瀏覽器就會在讀取到這個標籤後,當即切換對應的極速核。 另外爲了保險起見再加入
1
|
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
|
這樣寫能夠達到的效果是若是安裝了 Google Chrome Frame,則使用 GCF 來渲染頁面,若是沒有安裝 GCF,則使用最高版本的 IE 內核進行渲染。
相關連接:瀏覽器內核控制 Meta 標籤說明文檔
經過百度手機打開網頁時,百度可能會對你的網頁進行轉碼,脫下你的衣服,往你的身上貼狗皮膏藥的廣告,爲此可在 head 內添加
1
|
<meta http-equiv="Cache-Control" content="no-siteapp" />
|
相關連接:SiteApp 轉碼聲明
1
|
<title>your title</title>
|
1
|
<meta name="keywords" content="your keywords">
|
1
|
<meta name="description" content="your description">
|
1
|
<meta name="author" content="author,email address">
|
1
|
<meta name="robots" content="index,follow">
|
相關連接:WEB1038 - 標記包含無效的值
viewport 可讓佈局在移動瀏覽器上顯示的更好。 一般會寫
1
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
width=device-width 會致使 iPhone 5 添加到主屏後以 WebApp 全屏模式打開頁面時出現黑邊(http://bigc.at/ios-webapp-viewport-meta.orz)
content 參數:
1
|
<meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">
|
而若是你的網站不是響應式的,請不要使用 initial-scale 或者禁用縮放。
1
|
<meta name="viewport" content="width=device-width,user-scalable=yes">
|
相關連接:非響應式設計的viewport
適配 iPhone 6 和 iPhone 6plus 則須要寫:
1
2
|
<meta name="viewport" content="width=375">
<meta name="viewport" content="width=414">
|
大部分 4.7~5 寸的安卓設備的 viewport 寬設爲 360px,iPhone 6 上倒是 375px,大部分 5.5 寸安卓機器(好比說三星 Note)的 viewport 寬爲 400,iPhone 6 plus 上是 414px。
添加到主屏後的標題(iOS 6 新增)
1
|
<meta name="apple-mobile-web-app-title" content="標題"> <!-- 添加到主屏後的標題(iOS 6 新增) -->
|
是否啓用 WebApp 全屏模式
1
|
<meta name="apple-mobile-web-app-capable" content="yes" /> <!-- 是否啓用 WebApp 全屏模式 -->
|
設置狀態欄的背景顏色
1
|
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> <!-- 設置狀態欄的背景顏色,只有在 `"apple-mobile-web-app-capable" content="yes"` 時生效 -->
|
只有在 「apple-mobile-web-app-capable」 content=」yes」 時生效
content 參數:
禁止數字識自動別爲電話號碼
1
|
<meta name="format-detection" content="telephone=no" /> <!-- 禁止數字識自動別爲電話號碼 -->
|
rel 參數: apple-touch-icon 圖片自動處理成圓角和高光等效果。 apple-touch-icon-precomposed 禁止系統自動添加效果,直接顯示設計原圖。 iPhone 和 iTouch,默認 57×57 像素,必須有
1
|
<link rel="apple-touch-icon-precomposed" href="/apple-touch-icon-57x57-precomposed.png" /> <!-- iPhone 和 iTouch,默認 57x57 像素,必須有 -->
|
iPad,72×72 像素,能夠沒有,但推薦有
1
|
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/apple-touch-icon-72x72-precomposed.png" /> <!-- iPad,72x72 像素,能夠沒有,但推薦有 -->
|
Retina iPhone 和 Retina iTouch,114×114 像素,能夠沒有,但推薦有
1
|
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/apple-touch-icon-114x114-precomposed.png" /> <!-- Retina iPhone 和 Retina iTouch,114x114 像素,能夠沒有,但推薦有 -->
|
Retina iPad,144×144 像素,能夠沒有,但推薦有
1
|
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144x144-precomposed.png" /> <!-- Retina iPad,144x144 像素,能夠沒有,但推薦有 -->
|
IOS 圖標大小在iPhone 6 plus上是180×180,iPhone 6 是120×120。 適配iPhone 6 plus,則須要在中加上這段
1
|
<link rel="apple-touch-icon-precomposed" sizes="180x180" href="retinahd_icon.png">
|
官方文檔:https://developer.apple.com/library/ios/qa/qa1686/_index.html 參考文章:http://wxd.ctrip.com/blog/2013/09/ios7-hig-24/
iPad 的啓動畫面是不包括狀態欄區域的。
iPad 豎屏 768 x 1004(標準分辨率)
1
|
<link rel="apple-touch-startup-image" sizes="768x1004" href="/splash-screen-768x1004.png" /> <!-- iPad 豎屏 768 x 1004(標準分辨率) -->
|
iPad 豎屏 1536×2008(Retina)
1
|
<link rel="apple-touch-startup-image" sizes="1536x2008" href="/splash-screen-1536x2008.png" /> <!-- iPad 豎屏 1536x2008(Retina) -->
|
iPad 橫屏 1024×748(標準分辨率)
1
|
<link rel="apple-touch-startup-image" sizes="1024x748" href="/Default-Portrait-1024x748.png" /> <!-- iPad 橫屏 1024x748(標準分辨率) -->
|
iPad 橫屏 2048×1496(Retina)
1
|
<link rel="apple-touch-startup-image" sizes="2048x1496" href="/splash-screen-2048x1496.png" /> <!-- iPad 橫屏 2048x1496(Retina) -->
|
iPhone 和 iPod touch 的啓動畫面是包含狀態欄區域的。
iPhone/iPod Touch 豎屏 320×480 (標準分辨率)
1
|
<link rel="apple-touch-startup-image" href="/splash-screen-320x480.png" /> <!-- iPhone/iPod Touch 豎屏 320x480 (標準分辨率) -->
|
iPhone/iPod Touch 豎屏 640×960 (Retina)
1
|
<link rel="apple-touch-startup-image" sizes="640x960" href="/splash-screen-640x960.png" /> <!-- iPhone/iPod Touch 豎屏 640x960 (Retina) -->
|
iPhone 5/iPod Touch 5 豎屏 640×1136 (Retina)
1
|
<link rel="apple-touch-startup-image" sizes="640x1136" href="/splash-screen-640x1136.png" /> <!-- iPhone 5/iPod Touch 5 豎屏 640x1136 (Retina) -->
|
添加智能 App 廣告條 Smart App Banner(iOS 6+ Safari)
1
|
<meta name="apple-itunes-app" content="app-id=myAppStoreID, affiliate-data=myAffiliateData, app-argument=myURL"> <!-- 添加智能 App 廣告條 Smart App Banner(iOS 6+ Safari) -->
|
iPhone 6對應的圖片大小是750×1294,iPhone 6 Plus 對應的是1242×2148 。
1
2
3
|
<link rel="apple-touch-startup-image" href="launch6.png" media="(device-width: 375px)">
<link rel="apple-touch-startup-image" href="launch6plus.png" media="(device-width: 414px)">
|
Windows 8 磁貼顏色
1
|
<meta name="msapplication-TileColor" content="#000"/> <!-- Windows 8 磁貼顏色 -->
|
Windows 8 磁貼圖標
1
|
<meta name="msapplication-TileImage" content="icon.png"/> <!-- Windows 8 磁貼圖標 -->
|
1
|
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml" /> <!-- 添加 RSS 訂閱 -->
|
1
|
<link rel="shortcut icon" type="image/ico" href="/favicon.ico" /> <!-- 添加 favicon icon -->
|
比較詳細的 favicon 介紹可參考https://github.com/audreyr/favicon-cheat-sheet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<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="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="telphone=no, email=no" /><!-- 忽略頁面中的數字識別爲電話,忽略email識別 -->
<!-- 啓用360瀏覽器的極速模式(webkit) -->
<meta name="renderer" content="webkit">
<!-- 避免IE使用兼容模式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- 針對手持設備優化,主要是針對一些老的不識別viewport的瀏覽器,好比黑莓 -->
<meta name="HandheldFriendly" content="true">
<!-- 微軟的老式瀏覽器 -->
<meta name="MobileOptimized" content="320">
<!-- uc強制豎屏 -->
<meta name="screen-orientation" content="portrait">
<!-- QQ強制豎屏 -->
<meta name="x5-orientation" content="portrait">
<!-- UC強制全屏 -->
<meta name="full-screen" content="yes">
<!-- QQ強制全屏 -->
<meta name="x5-fullscreen" content="true">
<!-- UC應用模式 -->
<meta name="browsermode" content="application">
<!-- QQ應用模式 -->
<meta name="x5-page-mode" content="app">
<!-- windows phone 點擊無高光 -->
<meta name="msapplication-tap-highlight" content="no">
<!-- 適應移動端end -->
|