萬盛學電腦網

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

HTML `
` 標籤完全指南:語意、佈局、樣式隔離與現代最佳實踐

`
` 是什麼?

`

`(Division)是**通用區塊級容器**,**無任何語意**,僅用於**群組內容以套用 CSS/JS**。它是流式佈局的基本建構塊。

  • 預設 `display: block`,獨佔一行,寬度 100%。
  • 高度由內容撐開(`height: auto`)。
  • 無預設邊距、內距、邊框、背景。
  • 可巢狀無限層級。

核心用法分類

1. 版面佈局容器

<div class="container">
  <header class="site-header">...</header>
  <div class="main-wrapper">
    <main class="content">...</main>
    <aside class="sidebar">...</aside>
  </div>
  <footer class="site-footer">...</footer>
</div>

/* CSS */
.container { max-width: 1200px; margin: 0 auto; padding: 0 1rem; }
.main-wrapper { display: flex; gap: 2rem; }
.content { flex: 1; }
.sidebar { width: 280px; flex-shrink: 0; }

2. 元件封裝(BEM 命名慣例)

<div class="card">
  <div class="card__image"><img src="..." alt=""></div>
  <div class="card__body">
    <h3 class="card__title">標題</h3>
    <p class="card__desc">描述</p>
    <a href="#" class="card__link btn">閱讀更多</a>
  </div>
</div>

/* CSS */
.card { border: 1px solid #ddd; border-radius: 8px; overflow: hidden; background: white; }
.card__image { aspect-ratio: 16/9; overflow: hidden; }
.card__image img { width: 100%; height: 100%; object-fit: cover; transition: transform 0.3s; }
.card:hover .card__image img { transform: scale(1.05); }
.card__body { padding: 1.5rem; }
.card__title { margin: 0 0 0.5rem; font-size: 1.25rem; }
.card__desc { margin: 0 0 1rem; color: #666; line-height: 1.6; }

3. JS 掛鉤與資料屬性

<div 
  id="app" 
  class="theme-dark" 
  data-user-id="12345" 
  data-config='{"lang":"zh-Hant","theme":"dark"}'
  role="region" 
  aria-label="主要應用區域"
>
  <!-- Vue/React/Angular 掛載點 -->
</div>

// JS
const app = document.getElementById('app');
const userId = app.dataset.userId; // "12345"
const config = JSON.parse(app.dataset.config); // {lang, theme}

4. 樣式隔離與 CSS 作用域(Shadow DOM / CSS Modules / Scoped CSS)

<!-- 原生 Shadow DOM -->
<div id="widget-host"></div>
<script>
  const host = document.getElementById('widget-host');
  const shadow = host.attachShadow({mode: 'open'});
  shadow.innerHTML = `
    <style> .box { color: red; } </style>
    <div class="box">隔離樣式</div>
  `;
</script>

語意化替代:何時**不**該用 `
`

場景推薦語意標籤理由
頁面頂部區塊`
`
螢幕閱讀器快速導航、SEO
主要導航`同上
主要內容區`
`
跳轉主內容捷徑、SEO
獨立文章/貼文`
`
可獨立發布、RSS、搜尋索引
主題區塊`
`
通常帶標題、大綱算法識別
側邊補充`區分主次內容
頁尾`
`
同上
表單群組`
` + ``
無障礙群組宣告
圖片說明群組`
` + `
`
語意關聯圖文

現代佈局:Flexbox / Grid 讓 `
` 更強大

/* Flexbox:單維度(行或列) */
.flex-center { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.flex-wrap { display: flex; flex-wrap: wrap; gap: 1rem; }
.flex-col { display: flex; flex-direction: column; gap: 1rem; }

/* Grid:兩維度 */
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.5rem; }
.grid-auto { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1.5rem; }
.grid-areas {
  display: grid;
  grid-template-areas: 
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr auto;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }

無障礙屬性補強

  • `role="region"` + `aria-label="區塊名稱"`:當 `
    ` 代表重要區塊但無語意標籤適合時。
  • `role="group"` + `aria-labelledby="id"`:群組相關控制項。
  • `aria-live="polite|assertive"`:動態更新區域(通知、聊天訊息)。

效能與維護建議

  1. **避免過度巢狀**(Divitis):超過 4-5 層 `
    ` 通常表示結構設計可優化,改用語意標籤或 CSS Grid/Flex 減少包裝層。
  2. **類名語意化**:用 `.card`、`.form-group`、`.nav-bar`,而非 `.box1`、`.wrapper`、`.div-left`。
  3. **CSS 變數佈局值**:`--container-max: 1200px; --gap: 1.5rem;` 集中管理。
  4. **容器查詢(Container Queries)**:`@container (min-width: 400px) { .card { ... } }` 讓元件自適應容器而非視窗。

總結

`

` 是**無語意通用容器**,現代開發核心用途:

  1. **佈局骨架**(配合 Flex/Grid)
  2. **元件封裝**(BEM/語意類名)
  3. **JS/資料掛鉤點**
  4. **樣式隔離邊界**

**原則**:能用語意標籤(`header`, `main`, `section`...)就不用 `

`;需要通用容器時,給予清晰類名、適當 ARIA 屬性,配合現代 CSS 佈局系統。

copyright © 萬盛學電腦網 all rights reserved