CSS 問題 90% 來自盒模型理解偏差。可視化工具比 Console.log 高效 10 倍。
.debug-layout * {
background: rgba(255,0,0,0.1) !important;
outline: 1px solid rgba(255,0,0,0.3) !important;
}
/* 階層著色:深度越深顏色越暖 */
.debug-layout * { outline: 1px solid rgba(255,0,0,0.2); }
.debug-layout * * { outline-color: rgba(255,128,0,0.3); }
.debug-layout * * * { outline-color: rgba(255,255,0,0.4); }
.debug-layout * * * * { outline-color: rgba(0,255,0,0.5); }
.debug-layout * * * * * { outline-color: rgba(0,128,255,0.6); }
優於 border:outline 不佔空間、不影響佈局、不觸發重排、支援非矩形 (如行內元素換行)。
| 面板 | 核心功能 | 快捷鍵/入口 |
|---|---|---|
| Elements → Computed | 最終生效樣式、來源追蹤、盒模型可視化 | Cmd/Ctrl + Shift + C |
| Elements → Layout | Grid/Flex 覆蓋層、對齊線、間距標註 | 元素旁 badge 點擊 |
| Elements → Styles → :hov | 強制觸發 :hover/:focus/:active 狀態 | Styles 面板右上 :hov |
| Rendering | Paint 閃爍、Layer 邊界、滾動效能、模擬色盲 | Cmd/Ctrl + Shift + P → Rendering |
| Performance | 長任務、Layout Shift、重排/重繪分析 | 錄製分析 |
| Accessibility | 對比度、ARIA、語意結構、鍵盤導航 | Elements → Accessibility |
:root {
--debug: 0; /* 0=關閉, 1=開啟 */
}
@media (--debug) {
* { outline: 1px solid red; }
.container { background: rgba(255,0,0,0.05); }
}
/* DevTools Console 一鍵切換 */
document.documentElement.style.setProperty('--debug', 1); // 開啟
document.documentElement.style.setProperty('--debug', 0); // 關閉
/* 1. 強制顯示滾動條 (調試 overflow) */
.debug-scroll { overflow: scroll !important; }
/* 2. 強制可見隱藏內容 */
.debug-hidden { display: block !important; visibility: visible !important; opacity: 1 !important; }
/* 3. 顯示佈局斷點 */
@media (max-width: 640px) { body::before { content: 'Mobile'; position: fixed; bottom: 1rem; right: 1rem; background: #333; color: #fff; padding: 0.5rem; z-index: 9999; } }
@media (min-width: 641px) and (max-width: 1024px) { body::before { content: 'Tablet'; ... } }
@media (min-width: 1025px) { body::before { content: 'Desktop'; ... } }
/* 4. 顯示元素類別名 (無需 DevTools) */
.debug-labels *::before {
content: attr(class);
position: absolute;
top: -1.2em;
left: 0;
font: 10px monospace;
background: #000;
color: #0f0;
padding: 0 2px;
white-space: nowrap;
z-index: 9999;
pointer-events: none;
}
outline-offset:邊框與元素間距 (可負值)outline-color: invert:自動反色 (確保可見)/* Performance 面板關注指標 */
- Recalculate Style: 選擇器匹配耗時 (超過 50ms 需優化)
- Layout (Reflow): 幾何計算耗時 (觸發: width/height/position/float 等)
- Paint: 繪製耗時 (觸發: color/background/border/shadow 等)
- Composite: 合成層處理 (transform/opacity 無觸發 Layout/Paint)
/* 優化目標 */
- Style Recalc < 10ms
- Layout < 16ms (60fps 預算)
- 避免強制同步佈局 (JS 讀寫交替 offsetWidth 等)
- 推廣 transform/opacity 動畫 (僅 Composite)