<header> 代表**頁面或區塊的引導性內容容器**,通常包含標題、Logo、搜索、導航、作者信息、發布時間等。它不僅用於頁面頂部,更可在 `
header 元素代表介紹性內容或導航輔助組。通常包含標題 (h1-h6)、Logo、搜索表單、作者名、發布日期、目錄導航等。
<body>
<header class="site-header">
<div class="container">
<a href="/" class="logo" aria-label="首頁"><svg>...</svg></a>
<nav aria-label="主導航">
<ul><li><a href="/products">產品</a></li>...</ul>
</nav>
<form role="search"><input type="search" placeholder="搜索..."><button>搜</button></form>
<a href="/login" class="btn">登入</a>
</div>
</header>
<main>...</main>
</body>
<article>
<header>
<h1>HTML5 header 標籤完全指南</h1>
<div class="meta">
<span class="author"><a href="/author/zhangsan">張三</a></span>
<time datetime="2024-01-15T09:00">2024-01-15 09:00</time>
<span class="tags"><a href="/tag/html5">HTML5</a> <a href="/tag/semantic">語義化</a></span>
</div>
</header>
<div class="content">...</div>
<footer>...</footer>
</article>
<section aria-labelledby="features-heading">
<header>
<h2 id="features-heading">核心特性</h2>
<p>HTML5 賦能現代 Web 開發的三大支柱</p>
</header>
<ul class="feature-grid">...</ul>
</section>
<header>
<hgroup>
<h1>HTML5 核心指南</h1>
<h2>從入門到精通的系統化學習路徑</h2>
</hgroup>
<p>更新於 <time datetime="2024-01-15">2024-01-15</time></p>
</header>
`` 將主標題與副標題語義綁定,大綱算法視為單一標題節點。
interface HTMLHeaderElement : HTMLElement {};
// 無專有屬性/方法,繼承 HTMLElement
// 通用屬性: id, class, style, dataset, aria-*, 事件處理器...
| 誤區 | 正確認知 |
|---|---|
| header 必須在頁面最頂部 | 可在任何區塊內作為引導容器 |
| header 只能有一個 h1 | 每個 sectioning root (body/article/section/nav/aside) 可各有一個 h1 |
| header 等同於導航欄 | header 是容器,nav 是導航語義,二者常共現但可獨立存在 |
| 把頁腳信息放入 header | 版權、聯系、備案屬於 footer 語義 |
.site-header {
position: sticky; top: 0; z-index: 100;
background: rgba(255,255,255,0.95);
backdrop-filter: blur(8px);
border-bottom: 1px solid #eee;
}
.site-header .container {
display: flex; align-items: center; justify-content: space-between;
gap: 1rem; padding: 0.75rem 1.5rem;
}
article header { border-bottom: 1px solid #eee; padding-bottom: 1rem; margin-bottom: 1.5rem; }
article header .meta { display: flex; gap: 1rem; font-size: 0.875rem; color: #666; }
section header { margin-bottom: 1rem; text-align: center; }
section header p { color: #666; margin-top: 0.25rem; }
`