【问题标题】:Stretch DIV to Page Width将 DIV 拉伸到页面宽度
【发布时间】:2012-05-22 23:14:18
【问题描述】:

我需要一个 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 只会拉伸到浏览器窗口的大小,而不是整个页面本身。我已经让这种行为与表格布局一起使用,但如果有人可以提供 CSS 解决方案,我会更喜欢。我还需要适用于 IE 7 的解决方案。

【问题讨论】:

  • 您的 HTML 缺少 doctype,并且您使用 internet-explorer-7 标记了问题。您想要 HTML 或 IE7 的解决方案吗?
  • 你是对的;我编辑了问题

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


【解决方案1】:

要拉伸到 &lt;div&gt; 内的内容大小,只需将 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>

【讨论】:

    【解决方案2】:

    试试这个。 :)

    html {width: 100%; height: 100%; padding: 0; margin: 0;}
    body {width: 100%; height: 100%; position: relative; padding: 0; margin: 0;}
    #testDiv {width: 100%; height: 100%; position: absolute; top: 0; left: 0;}
    

    【讨论】:

    • 不幸的是这不起作用:(。#testDiv 仍然只延伸到窗口的宽度。
    猜你喜欢
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2012-02-04
    相关资源
    最近更新 更多