萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML 常用標籤完整速查表:語義化結構、文本格式化、表單、媒體、表格與 HTML5 新增元素

文檔結構標籤

標籤語義屬性示例
<html>文檔根元素lang="zh-Hant"
<head>元數據容器—
<title>文檔標題—
<meta>元數據charset="UTF-8", name="viewport" content="width=device-width, initial-scale=1"
<link>外部資源關聯rel="stylesheet" href="style.css"
<style>內部樣式—
<script>腳本src="app.js" defer
<body>可見內容容器—

語義化佈局標籤 (HTML5)

標籤語義典型用途
<header>頁面/區塊頭部Logo、導航、標題
<nav>導航鏈接組主導航、側邊欄、麵包屑
<main>主要內容區頁面核心內容(唯一)
<article>獨立完整內容博客文、新聞、評論
<section>主題性內容分組章節、標籤頁面板
<aside>側邊內容側邊欄、廣告、相關鏈接
<footer>頁面/區塊底部版權、聯系、相關鏈接
<address>聯系信息作者/機構聯系方式

文本內容標籤

標籤語義預設樣式備註
<h1>-<h6>標題層級粗體、字體遞減頁面僅一個 h1
<p>段落上下邊距—
<pre>預格式化文本等寬字體、保留空白換行代碼塊常用
<blockquote>長引用縮進配合 cite 屬性
<q>短行內引用自動添加引號—
<hr>主題分隔水平線語義非裝飾

文本語義/格式化標籤

標籤語義預設樣式區別說明
<strong>重要性強調粗體語義 > <b>
<em>語氣強調斜體語義 > <i>
<b>關鍵字/產品名(無語義強調)粗體純視覺
<i>異質文本(術語、思維、船名)斜體純視覺
<u>非文本標註(拼音、拼寫錯誤)下劃線HTML5 重新定義
<mark>高亮/標記黃底搜尋結果高亮
<small>側註/細則/版權小字非單純縮小字體
<del>刪除內容刪除線配合 datetime
<ins>插入內容下劃線配合 datetime
<sub>/<sup>下標/上標縮小、垂直偏移化學式、腳註
<code>代碼片段等寬字體行內代碼
<kbd>鍵盤輸入等寬、邊框快捷鍵提示
<samp>程序輸出等寬—
<var>變量斜體等寬數學/代碼變量
<abbr>縮寫底部虛線title 屬性展開全稱
<dfn>定義實例斜體定義術語處
<cite>作品標題斜體書名、電影、論文
<time>時間/日期—datetime 機器可讀格式
<data>機器可讀數據—value 屬性

列表標籤

<ul>          /* 無序列表 */
  <li>項目</li>
</ul>

<ol>          /* 有序列表 */
  <li>步驟 1</li>
  <li>步驟 2</li>
</ol>

<dl>          /* 描述列表 */
  <dt>術語</dt>
  <dd>定義</dd>
</dl>

鏈接與媒體

<a href="url" target="_blank" rel="noopener noreferrer">鏈接</a>
<img src="img.jpg" alt="描述" width="300" height="200" loading="lazy">
<picture>
  <source srcset="img.avif" type="image/avif">
  <source srcset="img.webp" type="image/webp">
  <img src="img.jpg" alt="描述">
</picture>
<video src="video.mp4" controls preload="metadata" poster="poster.jpg"></video>
<audio src="audio.mp3" controls></audio>
<iframe src="https://example.com" title="嵌入頁面" loading="lazy"></iframe>

表單標籤

<form action="/submit" method="post">
  <label for="name">姓名</label>
  <input type="text" id="name" name="name" required>
  
  <label for="email">信箱</label>
  <input type="email" id="email" name="email">
  
  <label for="pwd">密碼</label>
  <input type="password" id="pwd" name="pwd">
  
  <fieldset>
    <legend>性別</legend>
    <label><input type="radio" name="gender" value="m"> 男</label>
    <label><input type="radio" name="gender" value="f"> 女</label>
  </fieldset>
  
  <label for="bio">簡介</label>
  <textarea id="bio" name="bio" rows="4"></textarea>
  
  <label for="country">國家</label>
  <select id="country" name="country">
    <option value="tw">台灣</option>
    <option value="hk">香港</option>
  </select>
  
  <button type="submit">提交</button>
  <button type="reset">重置</button>
</form>

HTML5 新增輸入類型:tel、url、number、range、date、datetime-local、month、week、time、color、search。

表格標籤

<table>
  <caption>數據統計表</caption>
  <thead>
    <tr><th scope="col">項目</th><th scope="col">數值</th></tr>
  </thead>
  <tbody>
    <tr><th scope="row">收入</th><td>$1000</td></tr>
    <tr><th scope="row">支出</th><td>$500</td></tr>
  </tbody>
  <tfoot>
    <tr><th scope="row">淨利</th><td>$500</td></tr>
  </tfoot>
</table>

互動與腳本支援

<details>
  <summary>點擊展開</summary>
  <p>詳細內容...</p>
</details>

<dialog open>
  <h3>模態對話框</h3>
  <button autofocus>確定</button>
</dialog>

<template id="tpl">
  <div class="card">模板內容</div>
</template>

<slot> /* Web Components 影子 DOM 插槽 */ </slot>

廢棄/不推薦標籤(勿用)

  • <font>、<center>、<strike>、<big>、<tt>、<frame>、<frameset>、<noframes>、<acronym>、<applet>、<isindex>、<dir>、<basefont>
  • 替代方案:CSS (font-size, text-align, text-decoration) + 語義化標籤。

總結

**語義化 HTML 是無障礙、SEO、維護性的基石**。熟記上述標籤語義、屬性、預設行為與嵌套規則,避免「Divitis」(濫用 div/span)。**原則**:先選最具體語義標籤,再考慮通用容器。配合 CSS 實現視覺表現,構建結構清晰、機器可讀、樣式解耦的優質前端代碼庫。

copyright © 萬盛學電腦網 all rights reserved