居中方案取決於:元素類型(行內/塊級)、尺寸是否已知、單行/多行、佈局模式(Flex/Grid/傳統)。
.parent { text-align: center; }
.child { display: inline-block; }
.child { width: 300px; margin: 0 auto; }
/* Flex */
.parent { display: flex; justify-content: center; }
/* Grid */
.parent { display: grid; place-items: center; } /* 同時垂直水平置中 */
.parent { position: relative; }
.child {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.parent { display: table; margin: 0 auto; }
.single-line {
height: 100px;
line-height: 100px; /* 必須等於 height */
}
.parent {
display: flex;
align-items: center; /* 垂直置中 */
min-height: 200px; /* 容器需有高度 */
}
.parent {
display: grid;
place-items: center; /* 水平+垂直雙向置中 */
min-height: 200px;
}
.parent { position: relative; height: 200px; }
.child {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
}
.parent {
display: table-cell;
vertical-align: middle;
height: 200px;
text-align: center; /* 水平置中 */
}
.parent::before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.child {
display: inline-block;
vertical-align: middle;
}
.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-block | IE8+ 不定尺寸 | IE8+ | 兼容性好 | 代碼較繁瑣 |
.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;
}
.multiline-flex {
display: flex;
justify-content: center;
align-items: center;
min-height: 300px;
text-align: center;
padding: 20px;
}
.multiline-flex p {
max-width: 80%; /* 防止過寬 */
}
.img-center-bg {
width: 300px; height: 200px;
background: url(image.jpg) center / cover no-repeat;
}
.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%;
}
<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%); }
}
min-width: 0 導致文字溢出不換行vertical-align: middle 用在 block 元素上無效margin: auto 在 absolute 元素上需配合 inset: 0aspect-ratio 保持比例