標準文檔格式
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="...">
<title>頁面標題 | 站點名</title>
<link rel="stylesheet" href="style.css">
<script type="module" src="app.js" defer></script>
</head>
<body>
<header></header>
<main></main>
<footer></footer>
</body>
</html>
縮排與格式化規範
- 縮排:2 空格 (主流) 或 4 空格/Tab,**團隊統一**。
- 區塊元素換行:每個區塊元素獨佔一行,巢狀縮進。
- 行內元素可同行:
<p>文字 <strong>重點</strong> 文字</p>。
- 屬性順序建議:
id → class → name → data-* → src/href → type/value → style → aria-* → events。
- 雙引號屬性值:
class="btn",不使用單引號或無引號。
- 布林屬性簡寫:
disabled、required、hidden (無需 ="true")。
- 自閉合標籤:
<br>, <img>, <input> 等**不加**斜槓 (HTML5 標準)。
屬性順序範例
<input
id="email"
class="form-input"
name="email"
type="email"
value=""
placeholder="請輸入信箱"
required
aria-describedby="email-hint"
autocomplete="email"
>
HTMLHint / Markuplint 團隊配置
// .htmlhintrc
{
"rules": {
"doctype-first": true,
"doctype-html5": true,
"tagname-lowercase": true,
"attr-lowercase": true,
"attr-value-double-quotes": true,
"attr-no-duplication": true,
"id-class-value": "dash", // kebab-case
"style-disabled": false, // 允許內聯但建議避免
"inline-script-disabled": true, // 禁止內聯 script
"inline-style-disabled": true, // 禁止內聯 style
"head-script-disabled": true, // script 放 body 末
"alt-require": true, // img 必須有 alt
"title-require": true
}
}
// package.json
"scripts": {
"lint:html": "htmlhint \"src/**/*.html\"",
"format": "prettier --write \"**/*.{html,css,js,ts,json,md}\""
}
HTML Living Standard 關鍵差異 (vs HTML 4.01/XHTML)
- 無 DTD、無版本號、持續演進 (Living Standard)。
- 語意化新標籤:
main, article, section, nav, aside, header, footer, figure, figcaption, time, mark, details, summary, dialog。
- 表單新類型:
email, url, tel, date, number, range, color, search + 驗證屬性。
- 多媒體原生:
video, audio, track, source, picture。
- API 整合:
canvas, svg, websocket, fetch, storage, worker。
- 容錯解析:瀏覽器統一解析算法,不再依賴嚴格 DTD。