<div style="width:1000px; margin:0 auto;">
<div style="background:red; text-align:left;">區塊 1</div>
<div style="background:yellow;">區塊 2</div>
</div>
**現象**:IE9 下外層容器靠左,不置中。
IE9 支援三種文檔模式:
| 模式 | 觸發條件 | margin:auto 行為 |
|---|---|---|
| **標準模式 (Standards)** | 正確 <!DOCTYPE html> | ✅ 支援 margin: 0 auto 置中 |
| **準標準模式** | HTML4 過渡型 Doctype | ✅ 支援 |
| **怪異模式** | 無 Doctype / 舊 Doctype / IE 註釋在 Doctype 前 | ❌ **不支援** margin:auto,需 text-align:center 父級 |
<!DOCTYPE html> <!-- HTML5 簡潔標準模式 -->
X-UA-Compatible: IE=Edge 強制最高標準模式。
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
width: 1000px 必須生效(未被 float/position:absolute 破壞)。<!-- 怪異模式下的置中 Hack -->
<div style="text-align:center"> <!-- 父級 text-align:center -->
<div style="margin-left:auto; margin-right:auto; width:1000px; text-align:left;">
內容區塊
</div>
</div>
原理:怪異模式繼承 IE5.5 行為,text-align:center 會錯誤地置中**塊級子元素**,內層再 text-align:left 修正文字對齊。
<!DOCTYPE html> + <meta http-equiv="X-UA-Compatible" content="IE=edge">。.container { max-width: 1200px; margin: 0 auto; } (用 max-width 適配窄屏)。.container { display: flex; justify-content: center; } .content { width: 1000px; }。.container { display: grid; place-items: center; }。flexibility Polyfill 支援 Flexbox,或優雅降級至 Float/Table 佈局。IE9 置中失效 99% 由 **怪異模式** 導致。修正 Doctype、強制 Edge 模式即可解決。新專案請直接使用標準 margin: 0 auto 或 Flex/Grid,不再為已淘汰瀏覽器編寫 Hack 代碼。