萬盛學電腦網

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

CSS 水平垂直居中對齊完全指南:從經典到現代的 10+ 種方案

居中分類與選型決策樹

居中方案取決於:元素類型(行內/塊級)、尺寸是否已知、單行/多行、佈局模式(Flex/Grid/傳統)。

一、水平居中

1. 行內/行內塊元素:text-align: center

.parent { text-align: center; }
.child { display: inline-block; }

2. 塊級元素定寬:margin: 0 auto

.child { width: 300px; margin: 0 auto; }

3. 塊級元素不定寬(現代 Flex/Grid)

/* Flex */
.parent { display: flex; justify-content: center; }

/* Grid */
.parent { display: grid; place-items: center; }  /* 同時垂直水平置中 */

4. 絕對定位元素:left: 50% + transform

.parent { position: relative; }
.child {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

5. 表格佈局(IE8+)

.parent { display: table; margin: 0 auto; }

二、垂直居中

1. 單行文字/圖片:line-height = height

.single-line {
    height: 100px;
    line-height: 100px;   /* 必須等於 height */
}

2. 多行文字/未知高度:Flex(推薦)

.parent {
    display: flex;
    align-items: center;   /* 垂直置中 */
    min-height: 200px;     /* 容器需有高度 */
}

3. Grid 佈局

.parent {
    display: grid;
    place-items: center;   /* 水平+垂直雙向置中 */
    min-height: 200px;
}

4. 絕對定位 + transform(萬能、IE9+)

.parent { position: relative; height: 200px; }
.child {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
}

5. 表格單元格 vertical-align(IE8+)

.parent {
    display: table-cell;
    vertical-align: middle;
    height: 200px;
    text-align: center;    /* 水平置中 */
}

6. 偽元素輔助(不定高、不定寬、IE8+)

.parent::before {
    content: '';
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}
.child {
    display: inline-block;
    vertical-align: middle;
}

7. Flex + margin: auto(單個子元素)

.parent { display: flex; height: 200px; }
.child { margin: auto; }

三、水平垂直雙向居中(綜合方案對比)

方案適用場景兼容性優點缺點
Flex + justify/align現代佈局首選IE10+代碼少、響應式強、不需知尺寸舊瀏覽器需前綴
Grid + place-items二維佈局IE 不支援最語義化、功能最強兼容性較新
Absolute + Transform脫流定位、彈層IE9+不依賴父佈局、萬能脫離文檔流
Table-cell舊專案兼容IE8+無需知道尺寸語義不純、嵌套麻煩
Line-height單行文本/圖片全瀏覽器極簡僅限單行
偽元素 + inline-blockIE8+ 不定尺寸IE8+兼容性好代碼較繁瑣

四、多行文字水平垂直居中(難點攻克)

方案:inline-block + table-cell 模擬

.multiline-center {
    display: table-cell;
    width: 550px;
    height: 300px;
    padding: 0 10px;
    border: 4px solid #beceeb;
    color: #069;
    font-size: 24px;
    vertical-align: middle;     /* 關鍵 */
    text-align: center;         /* 水平置中 */
}

.multiline-center span {
    display: inline-block;
    vertical-align: middle;     /* 雙重保障 */
    font-size: 1rem;
}

現代 Flex 方案(更簡單)

.multiline-flex {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 300px;
    text-align: center;
    padding: 20px;
}

.multiline-flex p {
    max-width: 80%;  /* 防止過寬 */
}

五、圖片水平垂直居中

1. 背景圖片方案(最簡單)

.img-center-bg {
    width: 300px; height: 200px;
    background: url(image.jpg) center / cover no-repeat;
}

2. <img> 標籤 + Flex

.img-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 300px; height: 200px;
    border: 1px solid #ddd;
}
.img-wrapper img {
    max-width: 100%;
    max-height: 100%;
}

3. 透明 GIF + background-position(舊技巧,必應圖片搜索用過)

<img src="transparent.gif" 
     style="width: 300px; height: 200px; 
            background: url(real.jpg) center / cover no-repeat;" 
     alt="...">

六、實戰模板:模態框完美居中

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;        /* 防止貼邊 */
    z-index: 1000;
}

.modal-content {
    background: #fff;
    border-radius: 8px;
    max-width: 500px;
    width: 100%;
    max-height: 90vh;
    overflow: auto;
    /* 無需 transform,Flex 自動置中 */
}

@media (prefers-reduced-motion: reduce) {
    .modal-overlay { display: block; }
    .modal-content { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); }
}

七、避坑指南

  • ❌ 父容器無高度 → 垂直居中失效(除非 fixed/absolute 相對視窗)
  • ❌ Flex 子項不加 min-width: 0 導致文字溢出不換行
  • ❌ vertical-align: middle 用在 block 元素上無效
  • ❌ margin: auto 在 absolute 元素上需配合 inset: 0
  • ✅ 優先用 Flex/Grid,語義清晰、維護簡單
  • ✅ 彈層/Toast 用 Absolute + Transform
  • ✅ 圖片容器用 aspect-ratio 保持比例
copyright © 萬盛學電腦網 all rights reserved