hasLayout 是 IE 渲染引擎內部屬性,決定元素是否自行計算尺寸與定位內容。理解其運作原理,能有效解決 IE6/7 眾多顯示異常。
當元素 hasLayout = true 時,該元素擁有「佈局」,負責自己及子孫元素的尺寸計算與定位。預設擁有 layout 的元素包括 body、table、img、input、iframe 等替換元素與容器。
透過設定特定 CSS 屬性即可觸發:
display: inline-blockheight / width(非 auto)float: left/rightposition: absolute/fixedzoom(非 normal,IE5+)min/max-height/width、overflow(非 visible)行內元素在 IE6 標準模式下設定 width/height 不觸發,需改用 zoom:1 或 display:inline-block。
| Bug 名稱 | 現象 | 修復方案 |
|---|---|---|
| 雙邊距浮動 Bug (IE6) | 浮動元素外邊距加倍 | display: inline; |
| 3px 偏移 Bug (IE5-6) | 浮動旁內容莫名縮進 3px | _height: 1%;(觸發 hasLayout) |
| 躲躲貓 Bug (IE6) | 內容捲動時閃爍消失 | _height: 1%; 或父層觸發 hasLayout |
zoom:1 觸發 hasLayout 確認成因。width/height 等標準屬性修復,避免濫用 hack。* html .el { height:1%; }(Holly Hack),IE7 建議 min-height:0;。善用開發者工具檢視 hasLayout = -1,快速定位問題元素,精準修復 IE 專有渲染缺陷。