我可以得到100%的高度DIV,像這樣:CSS:是否有可能獲得100%高度的div,而不是頂部和底部邊距?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>T5</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css"
href="http://yui.yahooapis.com/3.0.0/build/cssreset/reset-min.css">
</link>
<style type="text/css">
* { padding: 0; margin: 0; }
html, body { height: 100%; }
body {
font-family: "lucida sans", verdana, arial, helvetica, sans-serif;
font-size: 75%;
}
h1 { font-weight: bold; font-size: 1.4em; padding: 10px 10px 0;}
p { padding: 0 10px 1em; }
#container {
min-height: 100%;
background-color: #DDD;
border-left: 2px solid #666;
border-right: 2px solid #666;
width: 280px;
margin: 0 auto;
}
* html #container { height: 100%; }
</style>
</head>
<body>
<div id="container">
<h1>100% Height Div</h1>
<p>Lorem ipsum ...</p>
</div>
</body>
</html>
它看起來像這樣:
alt text http://i41.tinypic.com/1j1do8.jpg
當我說 「100%的高度」 - 我的意思是它保持全高不管div中有多少內容,也不管窗口如何調整大小。
但是有可能得到一個高度幾乎100%的高度的div嗎?如果我想要利潤率在頂部和底部,我可以這樣做嗎?
我希望它看起來像這樣:
alt text http://i44.tinypic.com/j76kvl.jpg
我可以用JavaScript + CSS做到這一點。我可以用CSS來做到嗎?
答:
是的,這是可能的絕對定位。
#container {
width: 380px;
background-color: #DDD;
border: 2px solid #666;
position: absolute;
top: 20px; /* margin from top */
bottom: 20px; /* margin from bottom */
left: 50%; /* start left side in middle of window */
margin-left: -190px; /* then, reverse indent */
overflow: auto; /* scrollbar as necessary */
}
結果:
alt text http://i42.tinypic.com/33vj3nq.jpg
感謝to keithjgrant for the answer。查看http://jsbin.com/otobi的所有代碼。
不能通過添加_padding:20px 0 20px 0; _到#container id樣式嗎? – amelvin 2010-03-22 23:05:59
不,因爲填充和邊距被添加到高度/寬度。所以這會使100%+ 40px的高度。 – poke 2010-03-22 23:10:43
你需要在你的容器內部創建一個內部容器,然後將填充添加到那個 – stephenmurdoch 2010-03-23 16:10:26