萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

HTML <head> 標籤完整指南:DOCTYPE、Meta、SEO、行動裝置與安全

<head> 是網頁的大腦,雖不直接顯示內容,卻決定瀏覽器渲染模式、SEO 表現、行動裝置適配、安全策略與社群分享樣貌。本文系統整理所有常用 <head> 元素,說明用途、寫法與最佳實踐,助你打造標準、高效、安全的頁面頭部。

文件類型宣告

<!DOCTYPE html>

必置於檔案最開頭。觸發標準模式,避免怪異模式排版錯亂。HTML5 寫法最簡短、向前相容。

字元編碼

<meta charset="utf-8">

等同舊寫法 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">,但更簡短易記。務必在 <title> 之前宣告,避免中文亂碼。

語言屬性

<html lang="zh-Hant">  
<html lang="zh-Hans">  

正確的 lang 有助螢幕閱讀器、搜尋引擎、瀏覽器翻譯功能辨識語言。採用 BCP 47 標準(zh-Hant、zh-Hans)優於舊式 zh-TW/zh-CN。

瀏覽器渲染模式控制

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  • IE=edge:強制 IE 使用最新引擎渲染。
  • chrome=1:若安裝 Chrome Frame 則使用(歷史遺產)。

360 等雙核瀏覽器另需:

<meta name="renderer" content="webkit">

SEO 核心標籤

<title>頁面標題 - 品牌名</title>
<meta name="description" content="150 字內精準描述頁面內容,含核心關鍵字">
<meta name="keywords" content="關鍵字1, 關鍵字2, 關鍵字3"> <!-- 現代搜尋引擎權重極低,可選 -->
<meta name="author" content="作者名, email@example.com">
<meta name="robots" content="index, follow"> <!-- all/none/noindex/nofollow -->

行動裝置 Viewport 設定

<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
參數說明
width=device-width視窗寬度等於裝置寬度(CSS 像素)
initial-scale=1.0初始縮放 100%
maximum-scale最大縮放比例(建議不限制,利無障礙)
user-scalable=yes允許使用者縮放
viewport-fit=coveriOS 11+ 刘海屏適配,內容延伸至安全區外

非響應式網站:勿使用 initial-scale,改用固定寬度 width=1200 並允許縮放。

iOS Web App / PWA 專屬 Meta

<meta name="apple-mobile-web-app-capable" content="yes">       <!-- 全螢幕模式 -->
<meta name="apple-mobile-web-app-title" content="App標題">         <!-- 主畫面圖示標題 -->
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> <!-- 狀態列樣式 -->
<meta name="format-detection" content="telephone=no">           <!-- 禁用電話號碼自動連結 -->

iOS 圖示與啟動圖

<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon-180.png">
<link rel="apple-touch-icon" sizes="167x167" href="/apple-touch-icon-167.png">
<link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon-152.png">
<link rel="apple-touch-icon" sizes="120x120" href="/apple-touch-icon-120.png">
<!-- 非預覽版(無光澤圓角)加 precomposed -->

社群分享優化

Open Graph (Facebook, LINE, LinkedIn...)

<meta property="og:type" content="website">
<meta property="og:title" content="分享標題">
<meta property="og:description" content="分享描述">
<meta property="og:image" content="https://example.com/og-image.jpg">
<meta property="og:url" content="https://example.com/page">
<meta property="og:site_name" content="網站名稱">

Twitter Card

<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="標題">
<meta name="twitter:description" content="描述">
<meta name="twitter:image" content="https://example.com/twitter-image.jpg">

安全與效能相關

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' https://cdn.example.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:;">
<meta http-equiv="Referrer-Policy" content="strict-origin-when-cross-origin">
<link rel="preconnect" href="https://cdn.example.com">
<link rel="dns-prefetch" href="https://api.example.com">
<link rel="preload" as="style" href="/critical.css">
<link rel="preload" as="font" type="font/woff2" crossorigin href="/font.woff2">

其他實用標籤

  • <link rel="canonical" href="..."> 規範網址,防重複內容。
  • <link rel="alternate" hreflang="..." href="..."> 多語言版本。
  • <meta http-equiv="Cache-Control" content="no-siteapp"> 禁止百度轉碼。
  • <meta name="theme-color" content="#ffffff"> 瀏覽器標籤/工具列顏色(Android Chrome)。
  • <link rel="manifest" href="/manifest.json"> PWA 資訊清單。

總結:最小化生產環境 Head 模板

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
  <meta name="renderer" content="webkit">
  <title>頁面標題</title>
  <meta name="description" content="...">
  <meta property="og:title" content="...">
  <meta property="og:description" content="...">
  <meta property="og:image" content="...">
  <meta name="twitter:card" content="summary_large_image">
  <link rel="canonical" href="...">
  <link rel="preconnect" href="https://cdn.example.com">
  <link rel="manifest" href="/manifest.json">
  <meta name="theme-color" content="#your-brand-color">
  <!-- CSS/JS 放這裡或 body 末尾 -->
</head>

根據專案需求增減,保持 <head> 精簡、語意明確、安全優先,是前端工程師的基本功。

copyright © 萬盛學電腦網 all rights reserved