<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>自適應寬度流體版面</title>
<style>
body {
background: #e1ddd9;
font: 12px/1.5 Verdana, Arial, Helvetica, sans-serif;
color: #564b47;
margin: 20px 140px;
text-align: center; /* IE5.x 置中 hack */
}
#content {
width: 100%;
background: #fff;
overflow: auto; /* 閉合內部浮動 */
text-align: left; /* 重置內容對齊 */
}
h1 {
margin: 0; padding: 10px;
font-size: 11px; text-transform: uppercase;
text-align: right; color: #564b47;
background: #90897a;
}
p, pre { margin: 0; padding: 10px; }
a { color: #ff66cc; font-size: 11px; text-decoration: none; }
</style>
</head>
<body>
<div id="content">
<p><b>Centered Fluid Box</b><br><br>
This BOX is centered and adjusts to the browser window.<br>
The height adjusts to the content.<br>
<a href="/">More templates</a>
</p>
<pre>/* CSS 代碼展示區 */</pre>
<p><img src="xhtml10.gif" alt="" width="80" height="15">
<img src="css.gif" alt="" width="80" height="15"></p>
</div>
</body>
</html>
body margin:20px 140px:左右保留 140px 邊距,中間內容區自適應瀏覽器寬度。text-align:center(body)+ text-align:left(#content):IE5.x 置中 hack,標準瀏覽器用 margin:0 auto 但此處配合寬度 100% 已自然置中。overflow:auto:觸發 BFC,包含浮動子元素,防止高度塌陷。width:100%:配合 body 邊距實現流體寬度,無需媒體查詢。body {
margin: 0; padding: 20px 140px;
max-width: 1200px; margin-left: auto; margin-right: auto; /* 內容區最大寬度置中 */
}
#content { /* 同前 */ }
加上 max-width 避免超寬螢幕行長過長影響閱讀,並移除 IE5.x hack(已可忽略)。