<div class="card">
<p>內容
</div> <!-- 缺少 </p>,導致後續選擇器匹配錯誤 -->
✅ **解決**:HTML 驗證器 (W3C Validator / VS Code HTMLHint)、DevTools Elements 面板看 DOM 實際樹。
<div class="user-card">...</div>
.user-card { } /* ✅ */
.UserCard { } /* ❌ CSS 類名區分大小寫 */
/* 框架/全局樣式 */
.button { background: gray; }
/* 業務代碼 */
.my-btn { background: blue; } /* (0,0,1,0) 同級,順序靠前 → 失效 */
/* 解決:提升 Specificity 或調整順序 */
.c-btn.c-btn--primary { background: blue; } /* (0,0,2,0) 勝出 */
/* 或引入 CSS Layers 控制層級 */
.box {
background: url(bg.png) no-repeat center;
background: red; /* ❌ 簡寫會重置 image/position/repeat 等為初始值 */
}
✅ **分開寫** 或 **簡寫一次性寫全**。
.box { width: 100px; padding: 20px; border: 5px solid; }
/* 無 border-box:實際寬 150px,超出父容器導致佈局崩塌 */
✅ **Reset 階段全局 box-sizing: border-box**。
.parent { display: flex; }
.child { flex: 1; } /* 若父級非 flex,flex 屬性完全無效 */
.child { position: absolute; top: 0; left: 0; }
/* 父級無 position:relative/absolute/fixed/sticky → 相對 body 定位,非預期位置 */
.icon::before {
/* content: ""; 缺失 → 偽元素不生成,所有樣式無效 */
width: 16px; height: 16px; background: url(icon.svg);
}
.box {
display: grid;
/* 舊瀏覽器無回退 → 佈局崩塌 */
}
/* ✅ 漸進增強 */
.box { display: flex; }
@supports (display: grid) { .box { display: grid; } }
<link rel="stylesheet"> 順序:Reset → 基礎 → 組件 → 頁面 → Utility
3. CSP style-src 需包含 'self' 或 hash/nonce
CSS 不生效**絕非玄學**,均可歸因於上述 10 大類根因。建立 **「驗證 HTML → 檢查選擇器匹配 → 對比 Specificity → 確認載入順序 → 驗證特性支援」** 標準排查流程,配合 DevTools 深度檢查,分鐘級定位任何樣式疑難雜症。