短頁面無捲動條 → 長頁面出現捲動條 → 視口寬度減少(約 15-17px) → 內容重排 → 視覺左移/右移。此為體驗殺手,尤其在切換頁面時明顯。
html {
overflow-y: scroll; /* 垂直捲動條永遠佔位 */
}
/* 或更現代: */
html {
overflow: auto; /* 內容超出才捲動 */
min-height: 100vh; /* 強制最小高度滿視口 */
}
html {
overflow: -moz-scrollbars-vertical; /* 僅 Firefox 支援 */
}
現代 Firefox 已支援標準 overflow-y: scroll,無需專屬前綴。
body {
min-height: 100vh; /* 內容區最小高度滿視口 */
/* 配合內容實際高度,自然超出即出現捲動條 */
}
結合 html { overflow-y: scroll; } 雙重保險。
/* 現代 CSS Reset 片段 */
*, *::before, *::after {
box-sizing: border-box;
}
html {
/* 消除頁面切換抖動 */
overflow-y: scroll;
/* 字體平滑 */
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
/* 文字大小調整禁用(移動端) */
text-size-adjust: 100%;
}
body {
margin: 0;
min-height: 100vh;
line-height: 1.6;
font-family: system-ui, "PingFang TC", "Microsoft JhengHei", sans-serif;
color: var(--text-primary);
background: var(--bg-primary);
}
/* 縮窄捲動條 */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
background: #888;
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover { background: #555; }
/* Firefox */
html { scrollbar-width: thin; scrollbar-color: #888 transparent; }
⚠️ 捲動條樣式屬非標準(WebKit)或新標準(Firefox),視為增強體驗而非核心功能。
html, body { height: 100%; overflow: hidden; } → 內容超出直接截斷,災難。body { overflow: hidden; } 再用 JS 計算高度 → 複雜、易斷、破壞原生捲動體驗。html { overflow-y: scroll; } + body { min-height: 100vh; } → 簡單、穩健、零 JS。頁面抖動根因是捲動條出現/消失改變視口寬度。最簡單有效方案:html { overflow-y: scroll; } 強制捲動條永久佔位,配合 body { min-height: 100vh; } 確保內容區最小高度。這兩行 CSS 解決 99% 抖動問題,零 JS、零副作用、全瀏覽器相容。將其納入專案 Reset 基礎樣式,一勞永逸。