#content { width: 700px; margin: 0 auto; }
左右 margin: auto 會自動計算相等值,實現水平置中。
body { text-align: center; } /* IE6 會錯誤地將區塊元素也置中 */
#content { text-align: left; width: 700px; margin: 0 auto; } /* 重置文字對齊 */
原理: IE6 將 text-align:center 套用至區塊元素,故需在容器內重置為 left。
.parent { display: flex; justify-content: center; }
.child { width: 700px; /* 或 max-width */ }
.parent { display: grid; place-items: center; }
.parent { position: relative; }
.child { position: absolute; left: 50%; transform: translateX(-50%); }
| 方法 | 支援度 | 適用場景 |
|---|---|---|
| margin:0 auto | IE7+ | 固定寬度區塊 |
| Flexbox | IE10+ | 彈性佈局、多欄置中 |
| Grid | IE10+ (部分) | 二維佈局置中 |
| Absolute+Transform | IE9+ | 不定寬度、垂直水平雙置中 |