html - 将 DIV 拉伸(stretch)至页面宽度

标签 html css internet-explorer internet-explorer-7

我需要一个 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,您还必须包括一些技巧。

Example

<!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/

相关文章:

css - 通过 css 修复布局

jquery - JQuery Image Rotator 问题

c++ - 如何通过单击 HTML 表单按钮在本地运行程序?

html - 获取一个 <canvas> 元素来填充父 div 中的剩余高度?

javascript - 如何基于图钉在 Bing map 上绘制多边形?

javascript - 如果用户从下拉列表中选择其他,则 jquery 克隆不起作用当前文本字段已禁用 false

javascript - jQuery CSS 在 Ajax 加载后未触发 - 无法使用点击事件

javascript - XDomainRequest 与 XMLHTTPRequest

java - Web 开发人员可以停止未安装 Java 的 IE 弹出对话框吗?

javascript - 当没有安装 Flash 时,如何让 "Install Missing Plugin"黄条出现在 Firefox 中?