當 URL 片段識別符(#id)與頁面某元素 id 匹配時,該元素匹配 :target 偽類。點擊錨點連結即可觸發,無需 JavaScript。
<!-- HTML -->
This is tab 1 content
This is tab 2 content
This is tab 3 content
/* CSS */
.tab-control a { display: inline-block; width: 40px; height: 20px; line-height: 20px; text-align: center; color: #fff; background: rgba(70,121,189,.75); text-decoration: none; }
.tab-control a:hover { background: rgba(70,121,189,1); }
.tabs { position: relative; width: 135px; height: 200px; border: 1px solid red; overflow: hidden; }
.tab { width: 100%; height: 100%; display: none; }
.tab:target { display: block; }
<a href="#sidebar"> + #sidebar:target { transform: translateX(0); }#lightbox:target { display: flex; } 配合背景遮罩。:target 實現純 CSS 路由雛形。IE9+、所有現代瀏覽器支援。IE8 及以下需 JavaScript 替代方案。
transition)、無法程式化控制。