html head 頭部分的標簽、元素有很多,涉及到瀏覽器對網(wǎng)頁的渲染,seo 等等,而各個瀏覽器內核以及各個國內瀏覽器廠商都有些自己的標簽元素,這就造成了很多差異性。移動互聯(lián)網(wǎng)時代,head 頭部結構,移動端的 meta 元素,顯得更為重要。了解每個標簽的意義,寫出滿足自己需求的 head 頭標簽,是本文的目的。本篇以一絲的文章為基礎,進行擴展總結介紹常用的 head 中各個標簽、元素的意義以及使用場景。
doctype
doctype(document type),該聲明位于文檔中最前面的位置,處于 html 標簽之前,此標簽告知瀏覽器文檔使用哪種 html 或者 xhtml 規(guī)范。
dtd(document type definition) 聲明以 <!doctype> 開始,不區(qū)分大小寫,前面沒有任何內容,如果有其他內容(空格除外)會使瀏覽器在 ie 下開啟怪異模式(quirks mode)渲染網(wǎng)頁。公共 dtd,名稱格式為注冊//組織//類型 標簽//語言,注冊指組織是否由國際標準化組織(iso)注冊,+表示是,-表示不是。組織即組織名稱,如:w3c。類型一般是 dtd。標簽是指定公開文本描述,即對所引用的公開文本的唯一描述性名稱,后面可附帶版本號。最后語言是 dtd 語言的 iso 639 語言標識符,如:en 表示英文,zh 表示中文。xhtml 1.0 可聲明三種 dtd 類型。分別表示嚴格版本,過渡版本,以及基于框架的 html 文檔。
●html 4.01 strict
xml/html code復制內容到剪貼板
<!doctype html public -//w3c//dtd html 4.01//en >
●html 4.01 transitional
xml/html code復制內容到剪貼板
<!doctype html public -//w3c//dtd html 4.01 transitional//en >
●html 4.01 frameset
xml/html code復制內容到剪貼板
<!doctype html public -//w3c//dtd html 4.01 frameset//en >
●最新 html5 推出更加簡潔的書寫,它向前向后兼容,推薦使用。
xml/html code復制內容到剪貼板
<!doctype html>
在 html中 doctype 有兩個主要目的。
●對文檔進行有效性驗證。
它告訴用戶代理和校驗器這個文檔是按照什么 dtd 寫的。這個動作是被動的,每次頁面加載時,瀏覽器并不會下載 dtd 并檢查合法性,只有當手動校驗頁面時才啟用。
●決定瀏覽器的呈現(xiàn)模式
對于實際操作,通知瀏覽器讀取文檔時用哪種解析算法。如果沒有寫,則瀏覽器則根據(jù)自身的規(guī)則對代碼進行解析,可能會嚴重影響 html 排版布局。瀏覽器有三種方式解析 html 文檔。
●非怪異(標準)模式
●怪異模式
●部分怪異(近乎標準)模式 關于ie瀏覽器的文檔模式,瀏覽器模式,嚴格模式,怪異模式,doctype 標簽,可詳細閱讀模式?標準!的內容。
charset
聲明文檔使用的字符編碼,
xml/html code復制內容到剪貼板
<meta charset=utf-8>
html5 之前網(wǎng)頁中會這樣寫:
xml/html code復制內容到剪貼板
<meta http-equiv=content-type content=text/html; charset=utf-8>
lang屬性
簡體中文
xml/html code復制內容到剪貼板
<html lang=zh-cmn-hans>
繁體中文
xml/html code復制內容到剪貼板
<html lang=zh-cmn-hant>
為什么 lang=zh-cmn-hans 而不是我們通常寫的 lang=zh-cn 呢
優(yōu)先使用 ie 最新版本和 chrome
xml/html code復制內容到剪貼板
<meta http-equiv=x-ua-compatible content=ie=edge,chrome=1 />
360 使用google chrome frame
xml/html code復制內容到剪貼板
<meta name=renderer content=webkit>
360 瀏覽器就會在讀取到這個標簽后,立即切換對應的極速核。 另外為了保險起見再加入
xml/html code復制內容到剪貼板
<meta http-equiv=x-ua-compatible content=ie=edge,chrome=1>
這樣寫可以達到的效果是如果安裝了 google chrome frame,則使用 gcf 來渲染頁面,如果沒有安裝 gcf,則使用最高版本的 ie 內核進行渲染。
百度禁止轉碼
通過百度手機打開網(wǎng)頁時,百度可能會對你的網(wǎng)頁進行轉碼,脫下你的衣服,往你的身上貼狗皮膏藥的廣告,為此可在 head 內添加
xml/html code復制內容到剪貼板
<meta http-equiv=cache-control content=no-siteapp />
seo 優(yōu)化部分
頁面標題<title>標簽(head 頭部必須)
xml/html code復制內容到剪貼板
<title>your title</title>
頁面關鍵詞 keywords
xml/html code復制內容到剪貼板
<meta name=keywords content=your keywords>
頁面描述內容 description
xml/html code復制內容到剪貼板
<meta name=description content=your description>
定義網(wǎng)頁作者 author
xml/html code復制內容到剪貼板
<meta name=author content=author,email address>
定義網(wǎng)頁搜索引擎索引方式,robotterms 是一組使用英文逗號「,」分割的值,通常有如下幾種取值:none,noindex,nofollow,all,index和follow。
xml/html code復制內容到剪貼板
<meta name=robots content=index,follow>
viewport
viewport 可以讓布局在移動瀏覽器上顯示的更好。 通常會寫
xml/html code復制內容到剪貼板
<meta name=viewport content=width=device-width, initial-scale=1.0>
width=device-width 會導致 iphone 5 添加到主屏后以 webapp 全屏模式打開頁面時出現(xiàn)黑邊
content 參數(shù):
width viewport 寬度(數(shù)值/device-width)
height viewport 高度(數(shù)值/device-height)
initial-scale 初始縮放比例
maximum-scale 最大縮放比例
minimum-scale 最小縮放比例
user-scalable 是否允許用戶縮放(yes/no)
minimal-ui ios 7.1 beta 2 中新增屬性,可以在頁面加載時最小化上下狀態(tài)欄。這是一個布爾值,可以直接這樣寫:
xml/html code復制內容到剪貼板
<meta name=viewport content=width=device-width, initial-scale=1, minimal-ui>
而如果你的網(wǎng)站不是響應式的,請不要使用 initial-scale 或者禁用縮放。
xml/html code復制內容到剪貼板
<meta name=viewport content=width=device-width,user-scalable=yes>
適配 iphone 6 和 iphone 6plus 則需要寫:
xml/html code復制內容到剪貼板
<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 設備
添加到主屏后的標題(ios 6 新增)
xml/html code復制內容到剪貼板
<meta name=apple-mobile-web-app-title content=標題> <!-- 添加到主屏后的標題(ios 6 新增) -->
是否啟用 webapp 全屏模式
xml/html code復制內容到剪貼板
<meta name=apple-mobile-web-app-capable content=yes /> <!-- 是否啟用 webapp 全屏模式 -->
設置狀態(tài)欄的背景顏色
xml/html code復制內容到剪貼板
<meta name=apple-mobile-web-app-status-bar-style content=black-translucent />
<!-- 設置狀態(tài)欄的背景顏色,只有在 `apple-mobile-web-app-capable content=yes` 時生效 -->
只有在 apple-mobile-web-app-capable content=yes 時生效
content 參數(shù):
default 默認值。
black 狀態(tài)欄背景是黑色。
black-translucent 狀態(tài)欄背景是黑色半透明。 如果設置為 default 或 black ,網(wǎng)頁內容從狀態(tài)欄底部開始。 如果設置為 black-translucent ,網(wǎng)頁內容充滿整個屏幕,頂部會被狀態(tài)欄遮擋。
禁止數(shù)字識自動別為電話號碼
xml/html code復制內容到剪貼板
<meta name=format-detection content=telephone=no /> <!-- 禁止數(shù)字識自動別為電話號碼 -->
ios 圖標
rel 參數(shù): apple-touch-icon 圖片自動處理成圓角和高光等效果。 apple-touch-icon-precomposed 禁止系統(tǒng)自動添加效果,直接顯示設計原圖。 iphone 和 itouch,默認 57x57 像素,必須有
代碼如下:
<link rel=apple-touch-icon-precomposed href=/apple-touch-icon-57x57-precomposed.png /> <!-- iphone 和 itouch,默認 57x57 像素,必須有 -->
ipad,72x72 像素,可以沒有,但推薦有
代碼如下:
<link rel=apple-touch-icon-precomposed sizes=72x72 href=/apple-touch-icon-72x72-precomposed.png /> <!-- ipad,72x72 像素,可以沒有,但推薦有 -->
retina iphone 和 retina itouch,114x114 像素,可以沒有,但推薦有
代碼如下:
<link rel=apple-touch-icon-precomposed sizes=114x114 href=/apple-touch-icon-114x114-precomposed.png /> <!-- retina iphone 和 retina itouch,114x114 像素,可以沒有,但推薦有 -->
retina ipad,144x144 像素,可以沒有,但推薦有
代碼如下:
<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 是120x120。 適配iphone 6 plus,則需要在中加上這段
代碼如下:
<link rel=apple-touch-icon-precomposed sizes=180x180 href=retinahd_icon.png>
ios 啟動畫面
ipad 的啟動畫面是不包括狀態(tài)欄區(qū)域的。
ipad 豎屏 768 x 1004(標準分辨率)
代碼如下:
<link rel=apple-touch-startup-image sizes=768x1004 href=/splash-screen-768x1004.png /> <!-- ipad 豎屏 768 x 1004(標準分辨率) -->
ipad 豎屏 1536x2008(retina)
代碼如下:
<link rel=apple-touch-startup-image sizes=1536x2008 href=/splash-screen-1536x2008.png /> <!-- ipad 豎屏 1536x2008(retina) -->
ipad 橫屏 1024x748(標準分辨率)
代碼如下:
<link rel=apple-touch-startup-image sizes=1024x748 href=/default-portrait-1024x748.png /> <!-- ipad 橫屏 1024x748(標準分辨率) -->
ipad 橫屏 2048x1496(retina)
代碼如下:
<link rel=apple-touch-startup-image sizes=2048x1496 href=/splash-screen-2048x1496.png /> <!-- ipad 橫屏 2048x1496(retina) -->
iphone 和 ipod touch 的啟動畫面是包含狀態(tài)欄區(qū)域的。
iphone/ipod touch 豎屏 320x480 (標準分辨率)
代碼如下:
<link rel=apple-touch-startup-image href=/splash-screen-320x480.png /> <!-- iphone/ipod touch 豎屏 320x480 (標準分辨率) -->
iphone/ipod touch 豎屏 640x960 (retina)
代碼如下:
<link rel=apple-touch-startup-image sizes=640x960 href=/splash-screen-640x960.png /> <!-- iphone/ipod touch 豎屏 640x960 (retina) -->
iphone 5/ipod touch 5 豎屏 640x1136 (retina)
代碼如下:
<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)
代碼如下:
<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 。
代碼如下:
<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
windows 8 磁貼顏色
代碼如下:
<meta name=msapplication-tilecolor content=#000/> <!-- windows 8 磁貼顏色 -->
windows 8 磁貼圖標
代碼如下:
<meta name=msapplication-tileimage content=icon.png/> <!-- windows 8 磁貼圖標 -->
rss訂閱
代碼如下:
<link rel=alternate type=application/rss+xml title=rss href=/rss.xml /> <!-- 添加 rss 訂閱 -->
favicon icon
代碼如下:
<link rel=shortcut icon type=image/ico href=/favicon.ico /> <!-- 添加 favicon icon -->
移動端的meta
xml/html code復制內容到剪貼板
<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-detectioncontent=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 /><!-- 忽略頁面中的數(shù)字識別為電話,忽略email識別 -->
<!-- 啟用360瀏覽器的極速模式(webkit) -->
<meta name=renderer content=webkit>
<!-- 避免ie使用兼容模式 -->
<meta http-equiv=x-ua-compatible content=ie=edge>
<!-- 針對手持設備優(yōu)化,主要是針對一些老的不識別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 -->
更多信息請查看IT技術專欄