我需要一个 div 来扩展到 HTML 文档的整个页面宽度,具体取决于它的内容。
场景如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Traditional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>testing</title>
<style type="text/css">
body
{
background-color:pink;
padding:0;
margin:0;
}
#testDiv
{
background-color:red;
}
</style>
</head>
<body>
<div id="testDiv">
<table width="2000px">
<tr>
<td>
test
</td>
</tr>
</table>
</div>
</body>
</html>
testDiv 只会拉伸(stretch)到浏览器窗口的大小,而不是整个页面本身。我已经将此行为用于表格布局,但我更希望有人可以提供 CSS 解决方案。我还需要适用于 IE 7 的解决方案。
最佳答案
拉伸(stretch)到 <div>
中内容的大小只需设置 display
规则为 inline-block
,对于 IE7,您还必须包括一些技巧。
<!DOCTYPE html>
<html>
<head>
<title>testing</title>
<style type="text/css">
body
{
background-color:pink;
padding:0;
margin:0;
}
#testDiv
{
background-color:red;
display: inline-block;
/* IE 7- hacks */
zoom: 1;
*display: inline;
}
</style>
</head>
<body>
<div id="testDiv">
test
<div style="width: 2000px"></div>
</div>
</body>
</html>
关于html - 将 DIV 拉伸(stretch)至页面宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10704517/