編輯 HTML 的三種層次
| 層次 | 工具代表 | 優點 | 缺點 | 適用階段 |
| 純文本編輯器 | Notepad (Win)、TextEdit (Mac)、Nano/Vim (Term) | 零依賴、啟動極快、任何系統可用 | 無高亮、無提示、無格式化、無預覽、易出錯 | 極簡環境、臨時修改、教學演示「本質」 |
| 代碼編輯器 | VS Code、Sublime Text、Notepad++、Zed、Neovim | 語法高亮、Emmet、智能提示、格式化、Git 整合、終端、擴展生態、Live Preview | 需安裝配置、Electron 類佔內存 | **現代前端開發主流選擇** |
| 專業 IDE | WebStorm、PhpStorm、IntelliJ IDEA、Visual Studio | 深度靜態分析、重構、測試運行器、資料庫工具、框架深度整合、團隊協作功能 | 收費(訂閱制)、啟動較慢、資源佔用高 | 大型項目、企業級開發、全棧、重構密集 |
新手「四步走」創建首個網頁(以 VS Code 為例)
- 安裝 VS Code → 安裝擴展:
Live Server、Prettier、HTML CSS Support、Auto Rename Tag。
- 創建項目資料夾 → `mkdir my-first-page && cd my-first-page && code .`
- 新建 index.html → 輸入 `!` 按 Tab (Emmet) 生成骨架 → 填充內容:
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的第一個網頁</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Hello, HTML!</h1>
<p>這是我手寫的第一個網頁。</p>
</body>
</html>
- 實時預覽: 右鍵編輯器 →「Open with Live Server」→ 瀏覽器自動打開 `https://127.0.0.1:5500`,保存即刷新。
進階工作流:專業級工具鏈
1. 代碼質量保障
# 項目根目錄
npm init -y
npm i -D eslint @eslint/js htmlhint stylelint prettier husky lint-staged
# .eslintrc.cjs
module.exports = { root:true, env:{browser:true, es2022:true}, extends:['eslint:recommended'], parserOptions:{ecmaVersion:'latest'} }
# .htmlhintrc
{ "tagname-lowercase": true, "attr-lowercase": true, "attr-value-double-quotes": true, "doctype-first": true, "tag-pair": true, "spec-char-escape": true, "id-unique": true, "src-not-empty": true, "alt-require": true, "head-script-disabled": true, "style-disabled": true }
# .stylelintrc.cjs
module.exports = { extends:['stylelint-config-standard'], rules:{'color-hex-case':'lower','color-hex-length':'short'} }
# .prettierrc
{ "printWidth":120, "tabWidth":2, "useTabs":false, "singleQuote":true, "htmlWhitespaceSensitivity":"css" }
# package.json scripts
"lint": "eslint . && htmlhint **/*.html && stylelint **/*.css",
"format": "prettier --write .",
"prepare": "husky install"
2. Git Hooks 自動化
npx husky add .husky/pre-commit "npx lint-staged"
# .lintstagedrc
{
"*.{html,js,ts,jsx,tsx,vue,svelte}": ["prettier --write", "eslint --fix"],
"*.css": ["prettier --write", "stylelint --fix"]
}
3. 組件化開發(可選)
- Vite + Vue/React/Svelte/Astro:單文件組件(SFC)封裝 HTML/CSS/JS。
- Web Components 原生:`` + `` + `customElements.define()`,無構建依賴。
常見編輯操作技巧
| 操作 | VS Code 快捷鍵 | 說明 |
| Emmet 展開 | `!` + Tab / `ul>li*3` + Tab | 秒生成骨架/重複結構 |
| 多游標編輯 | Alt+Click / Ctrl+D (選下一個相同) | 批量修改屬性/類名 |
| 行移動 | Alt+↑/↓ | 調整標籤順序 |
| 註釋切換 | Ctrl+/ | HTML/CSS/JS 通用 |
| 格式化文檔 | Shift+Alt+F | Prettier 統一風格 |
| 跳轉定義/引用 | F12 / Shift+F12 | CSS 類/JS 變量/組件 |
| Zen Mode | Ctrl+K Z | 無干擾專注編寫 |
避坑指南
- ❌ 用 Word/WordPad/富文本編輯器存 HTML —— 會插入隱藏格式字符導致亂碼。
- ❌ 直接雙擊 HTML 文件開發(`file://` 協議) —— ES Module、Fetch、Cookie、SW 全失效,**必須用本地服務器**。
- ❌ 不配置格式化/檢查工具 —— 風格混亂、低級錯誤頻出、Code Review 痛苦。
- ✅ 統一團隊編輯器配置(`.vscode/settings.json`、`extensions.json` 推薦擴展)。
- ✅ 學會閱讀瀏覽器開發者工具 Elements/Console/Network/Performance 面板。
- ✅ 定期備份/版本控制(Git),善用分支與 PR 流程。
工欲善其事,必先利其器。選對工具、建好鏈路、養成習慣,讓編輯 HTML 從「敲代碼」變為「高效構建產品」。