*, *::before, *::after {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
/* 使 width/height 包含 padding + border,符合直覺,避免佈局撐破 */
支援:IE8+、所有現代瀏覽器。**放在 Reset/Normalize 之後、所有樣式之前**。
/* ❌ 錯誤:無定位屬性,z-index 無效 */
.modal { z-index: 1000; }
/* ✅ 正確:建立定位上下文 */
.modal {
position: fixed; /* 或 relative/absolute/sticky */
z-index: 1000;
}
/* 進階:隔離層疊上下文 */
.isolated-context {
isolation: isolate; /* 建立新層疊上下文,內部 z-index 不外洩 */
}
**層疊上下文觸發條件**:position: fixed/sticky、opacity < 1、transform/ filter/ perspective 不為 none、will-change、contain: layout/paint、z-index 不為 auto 的 flex/grid 子項。
.disabled-overlay {
pointer-events: none; /* 點擊穿透,事件傳遞給下層元素 */
opacity: 0.5;
}
/* 恢復子元素交互 */
.disabled-overlay .btn-allow {
pointer-events: auto;
}
支援:IE11+、所有現代瀏覽器。**不阻止鍵盤聚焦**,無障礙場景需配合 inert 屬性(HTML 標準屬性,Chrome 102+ 支援)。
.break-long-text {
overflow-wrap: break-word; /* 標準屬性 */
word-wrap: break-word; /* 別名,兼容舊瀏覽器 */
word-break: break-word; /* 非標準,但支援度好 */
/* 建議組合 */
overflow-wrap: anywhere; /* 更激進:任意字符間斷行,不考慮斷點 */
}
表格單元格需配合 table-layout: fixed。
@media
(-webkit-min-device-pixel-ratio: 2),
(min-resolution: 192dpi),
(min-resolution: 2dppx) {
/* Retina / 高清屏專用樣式 */
.logo { background-image: url(logo@2x.png); background-size: contain; }
.avatar { background-image: url(avatar@2x.jpg); }
}
現代寫法:min-resolution: 2dppx(dppx = dots per px)。
.blur-bg { filter: blur(20px); }
.grayscale { filter: grayscale(100%); }
.sepia { filter: sepia(1); }
.contrast { filter: contrast(1.5); }
.brightness { filter: brightness(1.2); }
.hue-rotate { filter: hue-rotate(90deg); }
.invert { filter: invert(1); }
.saturate { filter: saturate(2); }
.drop-shadow { filter: drop-shadow(0 4px 6px rgba(0,0,0,.15)); }
/* 組合 */
.composite { filter: grayscale(1) contrast(1.2) brightness(0.9); }
/* SVG 濾鏡(更強大) */
.custom-filter { filter: url(#my-svg-filter); }
支援:Chrome 53+、Firefox 35+、Safari 9.1+、Edge 79+。**IE 不支援**,需優雅降級。
/* 單行 */
.ellipsis-1 {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
/* 多行 (WebKit/Blink 事實標準) */
.ellipsis-3 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
/* 標準化多行 (草案) */
.ellipsis-standard {
overflow: hidden;
text-overflow: ellipsis;
/* block-ellipsis: auto; */
}
.placeholder {
min-height: 1px; /* 使空元素佔據高度,可見邊框/背景 */
/* 或 */
display: flex; /* Flex 容器即使無內容也有高度(若設定 height/min-height) */
}
/* 檢測支援 */
@supports (display: grid) {
.layout { display: grid; grid-template-columns: 1fr 3fr; }
}
@supports not (display: grid) {
.layout { display: flex; } /* 降級方案 */
}
/* 組合查詢 */
@supports (display: flex) and (gap: 1rem) {
.flex-gap { gap: 1rem; }
}
/* 檢測偽類 */
@supports selector(:focus-visible) {
:focus:not(:focus-visible) { outline: none; }
}
支援:Chrome 28+、Firefox 22+、Safari 9+、Edge 12+。**IE 不支援**,但可用於優雅增強。
, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
-webkit-text-size-adjust: 100%;
line-height: 1.5;
font-family: system-ui, sans-serif;
}
body { min-height: 100vh; }
img, picture, video, canvas, svg {
display: block;
max-width: 100%;
}
input, button, textarea, select { font: inherit; }
p, h1, h2, h3, h4, h5, h6 { overflow-wrap: break-word; }
這 10 大技巧覆蓋了**盒模型統一、層疊上下文控制、交互禁用、文本處理、高清屏適配、視覺特效、特性查詢**等高頻場景。掌握它們能解決 80% 的 CSS 疑難雜症。建議將常用片段封裝為 VS Code Snippets 或專案 Mixin/Utility,構建個人/團隊 CSS 技巧庫。