寫 XHTML 一開始最容易卡住的地方,不是語法而是「到底該有哪些東西」。以下是一份完整的骨架,每一段都加了註解說明用途,可以直接複製去改。
<!-- 告訴瀏覽器本網頁符合 XHTML 1.0 過渡型 DOCTYPE -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- html 標識擴展,定義名字空間 -->
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="zh-TW" lang="zh-TW">
<!-- 文檔頭 -->
<head>
<title>歡迎進入新《網頁設計師》:web 標準教程及推廣</title>
<!-- 定義語言編碼 -->
<meta http-equiv="Content-Type" content="text/Html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-TW" />
<!-- 為搜尋引擎準備的資訊 -->
<meta name="robots" content="all" />
<meta name="author" content="ajIE@netease.com, 阿捷" />
<meta name="Copyright" content="www.w3cn.org, 自由版權, 任意轉載" />
<meta name="description" content="新網頁設計師,web 標準的教程站點,推動 web 標準的應用。" />
<meta name="keywords" content="web標準, web標準教程, designing with web standards, xhtml, css, usability, accessibility, w3c" />
<!-- 瀏覽器收藏夾小圖標 -->
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="https://www.w3cn.org/favicon.ico" type="image/x-icon" />
<!-- 調用交替樣式表 -->
<link rel="stylesheet" rev="stylesheet" href="style/001/001.css" type="text/css" media="all" title="001" />
<link rel="alternate stylesheet" type="text/css" href="style/002/002.css" title="002" />
<link rel="alternate stylesheet" type="text/css" href="style/003/003.css" title="003" />
<script type="text/javascript" src="CSS/styleswitcher.js"></script>
</head>
<!-- 內容 -->
<body>
內容
</body>
</html>
DOCTYPE 必須放在文件最前面,任何內容之前,否則瀏覽器會進入 quirks mode,CSS 的呈現會跟預期不同。html 元素的 xmlns 是 XHTML 的必要宣告,lang 與 xml:lang 建議同時寫上以相容舊瀏覽器。
所有標籤都必須自我閉合:單獨的 meta、link、br、img 要寫成 /> 結尾;屬性值一律加引號;標籤與屬性名稱全部小寫。這幾點是 XHTML 與 HTML 在寫作習慣上最大的差異。
交替樣式表那三行 link 搭配 styleswitcher.js,是「多套版型讓使用者自己選」的經典做法。若不需要這個功能,留一條 rel="stylesheet" 即可。