萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 經典佈局模式大全:從單欄到多欄、Flexbox 到 Grid 實戰

佈局演進脈絡

  1. 表格佈局(已淘汰)
  2. Float + Clearfix(經典時代)
  3. Inline-block(過渡期)
  4. Flexbox(單維彈性,現代主流)
  5. Grid(二維網格,最強大)

現代專案建議:整體頁面架構用 Grid,元件內部用 Flexbox。

經典 Float 佈局模式(歷史參考)

單欄置中

.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; }

Flexbox 佈局模式(單維)

單行多欄等寬

.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; }

垂直置中(Flex 終極解)

.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; }

Grid 佈局模式(二維)

頁面級架構:Header / Sidebar / Main / Footer

.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; }

定位佈局

固定定位:模態框、Toast、固定導航

.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);
}

絕對定位:Tooltip、下拉選單、徽章

.dropdown {
  position: relative;
}
.dropdown-menu {
  position: absolute;
  top: 100%; left: 0;
  margin-top: 4px;
}

實用工具類(Utility Classes)

.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 的佈局體系。

copyright © 萬盛學電腦網 all rights reserved