零 JavaScript、零框架、純 CSS3 實現一個功能完整的後台管理面板:左側導航、頂部工具欄、多標籤內容區、響應式佈局、圖標字體、:target 偽類實現標籤頁切換。適合原型驗證、靜態站點、學習 CSS 進階佈局技巧。
@font-face 引入):target 偽類 + 錨點鏈接實現標籤切換transition: opacity 內容區淡入淡出.clearfix、box-sizing: border-box 全域<div class="admin-panel">
<aside class="slidebar">
<div class="logo"></div>
<ul>
<li><a href="#dashboard">儀表盤</a></li>
<li><a href="#posts">文章</a></li>
<li><a href="#media">媒體庫</a></li>
...
</ul>
</aside>
<main class="main">
<header class="topbar">
<ul>
<li><a href="#" class="nodrop">☰</a></li>
<li><a href="#">🔍</a></li>
<li><a href="#">⚙️</a></li>
</ul>
</header>
<section class="mainContent">
<div id="dashboard">...儀表盤內容...</div>
<div id="posts">...文章列表...</div>
<div id="media">...媒體庫...</div>
...
</section>
</main>
</div>
.admin-panel { display: flex; width: 990px; margin: 50px auto; }
.slidebar { width: 20%; min-height: 690px; background: #f7f7f7; border-right: 1px solid #ebebeb; }
.main { flex: 1; height: 690px; background: #fff; position: relative; }
@font-face {
font-family: 'icomoon';
src: url('fonts/icomoon.eot'); /* 略 */
}
.slidebar li:before {
font-family: 'icomoon'; speak: none;
position: absolute; right: 20px; line-height: 40px;
color: #666; -webkit-font-smoothing: antialiased;
}
.slidebar li:nth-child(1):before { content: '\63'; } /* 對應圖標編碼 */
.slidebar li:nth-child(2):before { content: '\64'; }
...
.mainContent > div {
position: absolute; top: 0; left: 0; right: 0; bottom: 0;
opacity: 0;
transition: opacity 200ms linear;
pointer-events: none; /* 隱藏時不可點擊 */
}
.mainContent > div:target {
opacity: 1;
pointer-events: auto;
z-index: 1;
}
/* 預設顯示第一個 */
#dashboard { opacity: 1; pointer-events: auto; }
原理:URL Hash 變化(#dashboard、#posts)觸發 :target 匹配,對應面板 opacity: 1 顯示,其餘隱藏。瀏覽器前進/後退、書籤分享天然支援。
#dashboard > div {
float: left; width: 345px; height: 262px;
border: 1px solid #ebebeb; border-radius: 5px;
margin-left: 30px; display: inline-block;
}
.monitor ul { float: left; padding: 0; margin: 0 31px 0 17px; }
.monitor li { list-style: none; font: 600 14px/28px helvetica; color: #666; }
| 優點 | 缺點 |
|---|---|
| 零 JS 依賴,極輕量(< 10KB CSS) | :target 會改變 URL Hash,影響瀏覽歷史 |
| 語義化 HTML、SEO 友好 | 無法實現嵌套路由、權限控制、數據獲取 |
| CSS 變數/媒體查詢輕鬆響應式 | 內容區 position: absolute 脫離文檔流,高度固定難適應動態內容 |
| 適合原型、靜態站、文檔站 | 生產級後台建議用 Vue/React + Router |
CSS Variables 管理主題色、間距、斷點@container 查詢實現組件級響應式IntersectionObserver 實現側邊欄滾動吸頂prefers-reduced-motion 尊重用戶動畫偏好完整代碼請見文中 CSS 區塊。這不是生產級框架,但展示了 **CSS 現代能力的邊界**——很多時候,你以為需要 JS 的交互,CSS 早就內建好了。