現代專案建議:整體頁面架構用 Grid,元件內部用 Flexbox。
.container { max-width: 960px; margin: 0 auto; }
.sidebar { float: left; width: 280px; }
.main { float: right; width: calc(100% - 280px); }
/* 或 */
.main { margin-left: 300px; } /* 側邊欄固定寬 */
.clearfix::after { content: ""; display: table; clear: both; }
.left { float: left; width: 200px; }
.right { float: right; width: 200px; }
.center { margin: 0 220px; }
.row { display: flex; gap: 16px; }
.col { flex: 1; } /* flex-grow:1, flex-shrink:1, flex-basis:0% */
.container { display: flex; }
.left { width: 200px; flex-shrink: 0; order: -1; }
.center { flex: 1; }
.right { width: 200px; flex-shrink: 0; }
.center-box {
display: flex;
align-items: center; /* 交叉軸置中 */
justify-content: center; /* 主軸置中 */
min-height: 100vh;
}
body { display: flex; flex-direction: column; min-height: 100vh; }
main { flex: 1; }
.page {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 260px 1fr;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
/* 響應式:小螢幕疊成單欄 */
@media (max-width: 768px) {
.page {
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
grid-template-columns: 1fr;
}
}
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 24px;
}
.magazine {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 200px;
gap: 16px;
}
.item-hero { grid-column: span 2; grid-row: span 2; }
.item-wide { grid-column: span 2; }
.item-tall { grid-row: span 2; }
.modal {
position: fixed;
inset: 0; /* top:0; right:0; bottom:0; left:0; */
display: flex;
align-items: center;
justify-content: center;
background: rgba(0,0,0,0.5);
}
.dropdown {
position: relative;
}
.dropdown-menu {
position: absolute;
top: 100%; left: 0;
margin-top: 4px;
}
.d-flex { display: flex; }
.flex-1 { flex: 1; }
.gap-4 { gap: 1rem; }
.justify-between { justify-content: space-between; }
.align-center { align-items: center; }
.grid { display: grid; }
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
:root {
--bp-sm: 640px;
--bp-md: 768px;
--bp-lg: 1024px;
--bp-xl: 1280px;
}
/* Mobile First */
.container { padding: 1rem; }
@media (min-width: 768px) { .container { padding: 2rem; } }
@media (min-width: 1024px) { .container { max-width: 1200px; margin: 0 auto; } }
佈局技術已從 Hack 走向標準化:Grid 處理宏觀架構,Flexbox 處理微觀元件,定位處理脫離文檔流的覆蓋層。熟練掌握 fr、minmax()、auto-fill/fit、gap、order、align/justify-content、inset 等現代屬性,配合 Mobile First 響應式策略,即可構建彈性、可維護、無 Hack 的佈局體系。