HTML 核心知識體系總結:標簽、元素、屬性、語義化
本文系統梳理 HTML 基礎:元素與標簽關係、四種元素形式、文檔結構、段落控制、文本格式化、特殊字符、註釋、列表、表格、表單,適合初學者建立完整知識框架,也可作為資深開發者的速查手冊。
1. 元素與標簽核心概念
- 元素 = 開始標簽 + 內容 + 結束標簽 (如
<p>內容</p>)
- 空元素:無內容、無結束標簽 (如
<br>, <hr>, <img>)
- 屬性-值對:
<tag attr="value">,HTML5 允許省略引號、布爾屬性簡寫
- 大小寫不敏感:但規範推薦小寫,XHTML 強制小寫
2. 標準文檔骨架
<!DOCTYPE html> <!-- HTML5 文檔類型聲明,必須首行 -->
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>頁面標題</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<!-- 可見內容 -->
</body>
</html>
3. 段落與換行控制
| 標簽 | 語義 | 關鍵屬性 | 現代替代 |
<p> | 段落 | align (廢棄) | CSS text-align |
<br> | 強制換行 | - | CSS white-space / display:block |
<hr> | 主題分隔 | color, size (廢棄) | CSS border-bottom |
4. 文本格式化標簽 (語義化優先)
| 標簽 | 語義 | 視覺效果 | CSS 替代 |
<strong> | 重要/強調 | 粗體 | font-weight:bold |
<em> | 重音強調 | 斜體 | font-style:italic |
<mark> | 高亮/標記 | 黃背景 | background:yellow |
<small> | 側註/小字 | 小號字 | font-size:smaller |
<del> | 刪除內容 | 刪除線 | text-decoration:line-through |
<ins> | 插入內容 | 下劃線 | text-decoration:underline |
<sub>/<sup> | 下標/上標 | 垂直偏移 | vertical-align:sub/super |
5. 特殊字符實體 (必須轉義)
| 字符 | 實體名 | 實體號 | 說明 |
| < | < | < | 小於號 |
| > | > | > | 大於號 |
| & | & | & | 和號 (必須轉義) |
| " | " | " | 雙引號 (屬性值中必須) |
| | |   | 不換行空格 |
| © | © | © | 版權符號 |
6. 列表家族
<!-- 有序列表 -->
<ol type="1" start="3"> <!-- type: 1/a/A/i/I, start 起始編號 -->
<li value="5">項目 5</li> <!-- value 強制編號 -->
<li>項目 6</li>
</ol>
<!-- 無序列表 -->
<ul type="disc"> <!-- disc/circle/square -->
<li>項目</li>
</ul>
<!-- 定義列表 -->
<dl>
<dt>術語</dt>
<dd>描述</dd>
<dt>HTML</dt>
<dd>超文本標記語言</dd>
</dl>
7. 表格語義化結構
<table>
<caption>月度銷售報表</caption>
<thead>
<tr><th scope="col">月份</th><th scope="col">金額</th></tr>
</thead>
<tbody>
<tr><th scope="row">1月</th><td>$1,000</td></tr>
</tbody>
<tfoot>
<tr><th scope="row">合計</th><td>$12,000</td></tr>
</tfoot>
</table>
8. 表單核心控件 (HTML5 增強)
| 類型 | 代碼 | 關鍵屬性 | 驗證特性 |
| 單行文本 | <input type="text"> | placeholder, maxlength, autocomplete | required, pattern, minlength |
| 郵箱 | <input type="email"> | multiple (多值) | 內置格式驗證 |
| 密碼 | <input type="password"> | - | required, minlength |
| 數字 | <input type="number"> | min, max, step | 範圍驗證 |
| 日期 | <input type="date"> | min, max | 日期選擇器 |
| 多行文本 | <textarea> | rows, cols, maxlength | required, minlength |
| 下拉選單 | <select><option>...</select> | multiple, size | required |
| 單選/複選 | <input type="radio/checkbox"> | value, checked | required (組級) |
| 文件上傳 | <input type="file"> | accept, multiple, capture | required |
9. 表單提交屬性
method="get":數據拼接 URL,長度受限,可收藏/分享,適合搜索/幂等查詢
method="post":數據在請求體,無長度限制,適合修改數據/敏感信息
action:處理腳本 URL,省略則提交當前頁
enctype:application/x-www-form-urlencoded (默認) / multipart/form-data (文件上傳)
10. 語義化原則總結
- 內容決定標簽,而非樣式
- 優先使用語義標簽 (
<article>, <section>, <nav>, <aside>, <header>, <footer>, <main>)
<div>/<span> 僅作為無語義容器 (掛掛鉤/樣式隔離)
- 標題層級 (h1-h6) 嚴格遵循文檔大綱
- 圖片必須有
alt,裝飾性圖片用 alt=""
copyright ©
萬盛學電腦網 all rights reserved