<!-- HTML: id 在頁面中唯一 -->
<header id="site-header">...</header>
<main id="main-content">...</main>
<footer id="site-footer">...</footer>
/* CSS: # 開頭 */
#site-header { border-bottom: 1px solid #eee; }
#main-content { padding: 2rem; }
#site-footer { background: #f5f5f5; }
HTML 標準強制:同一頁面 id 值必須唯一。違反會導致:
document.getElementById() 只返回第一個href="#id" 行為不可預測#header { position: sticky; top: 0; z-index: 100; }
#main { display: grid; grid-template-columns: 1fr 300px; gap: 2rem; }
#footer { margin-top: auto; }
/* 限定在 #sidebar 內的所有段落 */
#sidebar p { font-size: 0.875rem; line-height: 1.6; }
/* 限定在 #main 內的標題 */
#main h2 { border-bottom: 2px solid var(--color-primary); padding-bottom: 0.5rem; }
<button id="submit-btn" class="btn btn--primary">提交</button>
/* CSS 不用 #submit-btn 定義樣式 */
/* JS 使用 */
const btn = document.getElementById('submit-btn');
btn.addEventListener('click', handleSubmit);
/* ❌ 反模式:ID 用於樣式,導致難以覆蓋 */
#header .nav .link { color: #333; } /* 特異性 0,1,1,1 */
/* 後來想覆蓋某頁面導航顏色,需更高特異性 */
.page-about #header .nav .link { color: #007bff; } /* 0,1,1,1 → 後寫者勝,但脆弱 */
/* 或被迫用 !important */
.link--highlight { color: #007bff !important; } /* 污染代碼庫 */
/* ✅ 推薦:低特異性 class,易覆蓋、易複用、語意化 */
.site-header { ... }
.main-content { ... }
.site-footer { ... }
.nav { ... }
.nav__link { ... }
.nav__link--active { ... } /* BEM Modifier */
/* 頁面級變體:在 body/page-wrapper 加類別 */
.page-about .nav__link { color: var(--color-primary); } /* 特異性 0,0,2,0 易覆蓋 */
document.getElementById()、label[for="id"]、aria-labelledby<a href="#section1"> 對應 <section id="section1"><label for="email"> + <input id="email"><use href="#icon-home">"selector-max-id": 0