:target 偽類選中**當前 URL 片段標識符(hash)對應的元素**。當點擊錨點鏈接 `` 時,URL 變為 `#tab1`,ID 為 `tab1` 的元素即匹配 `:target`。
<nav>
<a href="#tab1">標籤一</a>
<a href="#tab2">標籤二</a>
<a href="#tab3">標籤三</a>
</nav>
<div class="tabs">
<section id="tab1">內容一</section>
<section id="tab2">內容二</section>
<section id="tab3">內容三</section>
</div>
.tabs section {
display: none; /* 預設隱藏 */
}
.tabs section:target {
display: block; /* 被選中時顯示 */
}
/* 預設顯示第一個(無 hash 時) */
.tabs section:first-child {
display: block;
}
.tabs section:first-child:not(:target) ~ section:target ~ .tabs section:first-child {
display: none; /* 有其他 target 時隱藏預設 */
}
/* 以上兄弟選擇器技巧較複雜,實務上建議配合 JS 或預設 hash */
<div class="tab-container" role="tablist">
<button role="tab" aria-selected="true" aria-controls="panel1" id="tab1" data-target="#panel1">面板一</button>
<button role="tab" aria-selected="false" aria-controls="panel2" id="tab2" data-target="#panel2">面板二</button>
<button role="tab" aria-selected="false" aria-controls="panel3" id="tab3" data-target="#panel3">面板三</button>
</div>
<div class="tab-panels">
<section role="tabpanel" id="panel1" aria-labelledby="tab1" hidden>內容一</section>
<section role="tabpanel" id="panel2" aria-labelledby="tab2" hidden>內容二</section>
<section role="tabpanel" id="panel3" aria-labelledby="tab3" hidden>內容三</section>
</div>
.tab-panels section {
display: none;
}
.tab-panels section:target {
display: block;
}
/* 預設顯示第一個:若無 hash 或 hash 不匹配任何面板 */
.tab-panels section:first-child:not(:target):only-child,
.tab-panels section:first-child:not(:target):first-of-type {
display: block;
}
/* 更穩健:給第一個面板預設 ID,HTML 加載時自動跳轉,或用 JS 初始化 */
document.querySelectorAll('[role="tab"]').forEach(tab => {
tab.addEventListener('click', () => {
// 更新 ARIA
document.querySelectorAll('[role="tab"]').forEach(t => {
t.setAttribute('aria-selected', 'false');
t.removeAttribute('aria-current');
});
tab.setAttribute('aria-selected', 'true');
tab.setAttribute('aria-current', 'true');
// 滾動行為可選:history.replaceState(null, '', tab.dataset.target);
});
});
// 初始化:若無 hash,顯示第一個
if (!location.hash) {
const firstTab = document.querySelector('[role="tab"]');
firstTab?.click(); // 觸發上述邏輯
}
<a href="#lightbox1">打開燈箱</a>
<div id="lightbox1" class="lightbox">
<a href="#" class="lightbox-close">×</a>
<img src="large.jpg" alt="大圖">
</div>
.lightbox {
position: fixed; inset: 0;
background: rgba(0,0,0,.8);
display: flex; align-items: center; justify-content: center;
opacity: 0; pointer-events: none;
transition: opacity 0.3s;
z-index: 1000;
}
.lightbox:target {
opacity: 1; pointer-events: auto;
}
.lightbox-close {
position: absolute; top: 1rem; right: 1rem;
color: white; font-size: 2rem; text-decoration: none;
}
點擊關閉鏈接 `` 移除 hash,燈箱關閉。純 CSS 無 JS!
section:target {
animation: highlight 1s ease;
outline: 3px solid #3498db;
}
@keyframes highlight {
from { background: #ffffe0; }
to { background: transparent; }
}
點擊導航鏈接跳轉到區段時,目標區段高亮閃爍,引導視線。
<a href="#error-email" class="error-link">請修正 Email 格式</a>
<input id="email" name="email">
<span id="error-email" class="error-message">Email 格式不正確</span>
.error-message { display: none; }
.error-message:target { display: block; color: #e74c3c; animation: shake 0.4s; }
scroll-behavior: smooth 或 JS 阻止)| 方案 | 優點 | 缺點 | 適用場景 |
|---|---|---|---|
| :target 純 CSS | 零 JS、極簡、SEO 友好 | 歷史記錄、無障礙需補、巢狀困難 | 簡單頁面、靜態站點、原型 |
| JS 控制 class | 完全控制、無歷史污染、無障礙完善 | 需 JS 載入執行 | 生產級應用、複雜交互 |
| <details>/<summary> | 原生折疊、無障礙內建 | 樣式難控制、非標籤語意 | 簡單展開/收合 |
| Web Components | 封裝性、可復用、Shadow DOM 隔離 | 學習曲線、Polyfill 體積 | 設計系統、大型應用 |
:target 是 CSS 稀有的「狀態感知」選擇器,能根據 URL hash 切換顯示狀態,實現**零 JavaScript 的選項卡、燈箱、錨點高亮**等互動。雖有歷史記錄等限制,但配合少量 JS 修正 ARIA 與初始狀態,即可構建輕量、無障礙的切換組件。理解其原理,能拓展 CSS 狀態管理的想像空間。