左側導航欄背景色需延伸至頁腳,但內容高度不固定,傳統 background-color 只覆蓋內容區高度。
body { background: url(sidebar-bg.png) repeat-y 0 0; }
#sidebar { width: 200px; float: left; }
缺點:固定寬度、無法用 em/rem、圖片維護麻煩。
.wrapper { overflow: hidden; }
#sidebar { float: left; width: 200px; margin-bottom: -9999px; padding-bottom: 9999px; }
缺點:極大數值可能觸發瀏覽器渲染 Bug、錨點定位偏移。
.container { display: flex; }
#sidebar { width: 200px; background: #333; }
#main { flex: 1; }
優點:自動等高、無 Hack、支援任意欄數。
.container { display: grid; grid-template-columns: 200px 1fr; grid-template-rows: 1fr auto; }
#sidebar { grid-row: 1 / 3; background: #333; } /* 跨 header/main/footer */
#header { grid-column: 1 / 3; }
#footer { grid-column: 1 / 3; }
.container { display: table; width: 100%; }
#sidebar, #main { display: table-cell; vertical-align: top; }