DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
HTML5 中 meta 標籤的實用寫法整理
編輯:HTML5詳解     

meta 標籤在 HTML 裡的位置很微妙:它畫不出任何東西,卻同時決定了編碼、相容模式、SEO、行動裝置的縮放行為,甚至是各家 App 內建瀏覽器的顯示細節。有些網頁完全不用它也能跑,但只要你想把頁面做「完整」,這些標籤就是必備的基本功。以下依用途分類整理常用的寫法。

1. 宣告文件編碼

<meta charset="utf-8">

放在 <head> 的前段,確保中文等多位元字元不會變成亂碼。

2. 宣告相容模式

<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="X-UA-Compatible" content="IE=Emulate IE7" />

第一行指示 IE 以目前可用的最高模式來呈現內容;第二行則是模擬 IE7,此時瀏覽器會依 <!DOCTYPE> 決定排版方式——標準模式指令以 IE7 標準模式顯示,而 Quirks 模式指令則退回 IE5 行為。

3. SEO 相關

<meta name="description" content="頁面描述,建議不超過 150 字元" />
<meta name="keyWords" content="Html5, CSS3, 關鍵字" />
<meta name="author" content="作者名稱" />
<meta name="robots" content="index,follow" />

description 是搜尋結果摘要的來源,keyWords 用來列出主題關鍵字,author 標示網頁作者。robots 則定義搜尋引擎的索引方式,robotterms 是一組以英文逗號分隔的值,常見的有 none、noindex、nofollow、all、index、follow。

4. 為行動裝置加上 viewport

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">

content 參數說明:

  • width:viewport 寬度(數值或 device-width)
  • height:viewport 高度(數值或 device-height)
  • initial-scale:初始縮放比例
  • maximum-scale / minimum-scale:最大與最小縮放比例
  • user-scalable:是否允許使用者縮放(yes / no)
  • minimal-ui:iOS 7.1 beta 2 新增,頁面載入時可最小化上下狀態列,屬布林值,直接寫在 content 中即可。
<meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">

5. iOS 裝置設定

<meta name="apple-mobile-web-app-title" content="標題">        <!-- 加入主畫面後的名稱(iOS 6 新增)-->
<meta name="apple-mobile-web-app-capable" content="yes" />  <!-- 是否啟用 WebApp 全螢幕模式 -->
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />

最後一行用來設定狀態列樣式,只有在 apple-mobile-web-app-capable 為 yes 時才會生效。content 可取:

  • default:預設值。
  • black:狀態列背景為黑色。
  • black-translucent:狀態列背景為黑色半透明。

設為 default 或 black 時,網頁內容從狀態列底部開始;設為 black-translucent 時,網頁內容會填滿整個螢幕,頂部會被狀態列遮住。

6. iOS 圖示(apple-touch-icon)

apple-touch-icon 會讓系統自動為圖片加上圓角與高光;apple-touch-icon-precomposed 則停用自動效果,直接顯示設計原圖。

<link rel="apple-touch-icon-precomposed" href="/apple-touch-icon-57x57-precomposed.png" />               <!-- iPhone / iPod touch,預設 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 / iPod touch,114x114,建議提供 -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144x144-precomposed.png" /> <!-- Retina iPad,144x144 -->

另外,apple-mobile-web-app-title 的內容最好控制在六個中文字以內,過長會被截斷隱藏。

7. iOS 啟動畫面

iPad 的啟動畫面不含狀態列區域:

<link rel="apple-touch-startup-image" sizes="768x1004" href="/splash-screen-768x1004.png" />    <!-- iPad 直向 768x1004(標準解析度)-->
<link rel="apple-touch-startup-image" sizes="1536x2008" href="/splash-screen-1536x2008.png" /> <!-- iPad 直向 1536x2008(Retina)-->
<link rel="apple-touch-startup-image" sizes="1024x748" href="/Default-Portrait-1024x748.png" /> <!-- iPad 橫向 1024x748(標準解析度)-->
<link rel="apple-touch-startup-image" sizes="2048x1496" href="/splash-screen-2048x1496.png" /> <!-- iPad 橫向 2048x1496(Retina)-->

iPhone 與 iPod touch 的啟動畫面則是包含狀態列區域的:

<link rel="apple-touch-startup-image" href="/splash-screen-320x480.png" />                 <!-- 320x480(標準解析度)-->
<link rel="apple-touch-startup-image" sizes="640x960" href="/splash-screen-640x960.png" />   <!-- 640x960(Retina)-->
<link rel="apple-touch-startup-image" sizes="640x1136" href="/splash-screen-640x1136.png" /> <!-- iPhone 5 / iPod touch 5,640x1136(Retina)-->
<link rel="apple-touch-startup-image" href="Startup.png" />

使用者從主畫面點開 WebApp 時,系統會先顯示啟動畫面;若沒有設定,就只會拿網站首頁的截圖充數,體驗自然差很多。

8. Windows 8 磁貼

<meta name="msapplication-TileColor" content="#000" />   <!-- 磁貼顏色 -->
<meta name="msapplication-TileImage" content="icon.png" /> <!-- 磁貼圖示 -->

9. 較少用但偶爾派上用場的寫法

<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml" />  <!-- RSS 訂閱 -->
<link rel="shortcut icon" type="image/ico" href="/favicon.ico" />             <!-- favicon -->
<meta name="format-detection" content="telephone=no" />                      <!-- 不要把數字誤判為電話 -->
<meta name="format-detection" content="email=no" />                          <!-- Android 不要自動辨識信箱 -->
<meta name="renderer" content="webkit">                                      <!-- 360 瀏覽器使用極速模式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">                <!-- 讓 IE 使用最高級核心,裝了 Chrome Frame 則走 webkit -->
<meta name="HandheldFriendly" content="true">                                <!-- 針對不認 viewport 的舊手持裝置,如 BlackBerry -->
<meta name="MobileOptimized" content="320">                                  <!-- 微軟舊式瀏覽器 -->
<meta name="x5-orientation" content="portrait">                              <!-- QQ 瀏覽器強制直向 -->
<meta name="full-screen" content="yes">                                      <!-- UC 強制全螢幕 -->
<meta name="x5-fullscreen" content="true">                                   <!-- QQ 強制全螢幕 -->
<meta name="browsermode" content="application">                              <!-- UC 應用模式 -->
<meta name="x5-page-mode" content="app">                                     <!-- QQ 應用模式 -->
<meta http-equiv="Cache-Control" content="no-siteapp" />                      <!-- 禁止百度轉碼 -->
<meta name="msapplication-tap-highlight" content="no">                       <!-- Windows Phone 點擊不顯示高光 -->
<meta name="mobile-web-app-capable" content="yes">                           <!-- 新版 Chrome 全螢幕 -->
<meta name="apple-itunes-app" content="app-id=myAPPStoreID, affiliate-data=myAffiliateData, app-argument=myURL"> <!-- Smart App Banner(iOS 6+ Safari)-->

此外也常一併寫上 title、description、keywords、author、Copyright 等基本欄位,讓文件資訊更完整。

10. SNS 社群分享標籤(Open Graph)

<meta property="og:type" content="類型" />
<meta property="og:url" content="URL 位址" />
<meta property="og:title" content="標題" />
<meta property="og:image" content="圖片" />
<meta property="og:description" content="描述" />

這組標籤決定了連結分享到 Facebook 等社群平台時,顯示出來的標題、圖片與摘要。

11. 條件註解判斷 IE

<!--[if lt IE 7]><html class="oldie ie IE6"><![endif]-->
<!--[if IE 7]><html class="oldie ie IE7"><![endif]-->
<!--[if IE 8]><html class="ie IE8"><![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html><!--<![endif]-->

透過在 <html> 標籤上掛版本 class,就能針對特定 IE 版本寫差異化樣式,而不必額外引入 hack 檔案。

以上就是 meta 標籤常見用法的整理。實際專案裡不需要全數塞進去,但編碼、viewport、description 與 og 這幾項,幾乎是每個網站都該具備的底線。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved