min-width 讓彈性容器不低於指定寬度,防止內容擠壓變形。IE6 不支援,需 Hack 實現。本文提供標準寫法、IE6 Hack 原理、現代替代方案。
.container {
width: 100%;
min-width: 300px; /* 最窄 300px,防止過度壓縮 */
}
<div class="container">
<div class="holder">內容區</div>
</div>
/* 現代瀏覽器生效 */
.container { min-width: 300px; }
/* IE6 Hack */
* html .container {
border-right: 300px solid #fff; /* 右邊框撐開最小寬度,色同背景隱形 */
}
* html .holder {
display: inline-block; /* 觸發 hasLayout */
position: relative; * 定位基準
margin-right: -300px; /* 負 margin 抵消邊框佔用空間,內容回正 */
}
* html 選擇器只有 IE6- 識別。border-right: 300px 強制容器最小寬度含邊框達 300px。margin-right: -300px 將內容拉回視覺正確位置。display: inline-block + position: relative 觸發 IE6 hasLayout,讓負 margin 生效。min-width 自然生效,無兼容問題。@container (min-width: 300px) { ... } 實現組件級響應。.container { min-height: 200px; }
/* IE6 Hack 同理用 border-bottom + margin-bottom 負值 */
min-width 是彈性佈局保護線。現代專案直接使用標準屬性;維護 IE6 專案時,雙層容器 + 邊框 + 負 margin Hack 經過實戰驗證可靠。隨著 IE6 徹底退出歷史舞台,此 Hack 將成為 CSS 考古遺跡。