【问题标题】:webpage (app) layout - set 100% height (html, css)网页(应用程序)布局 - 设置 100% 高度(html、css)
【发布时间】:2012-01-25 00:26:21
【问题描述】:

我遇到了一个大问题,让我的母版页布局拉伸到 100% 浏览器窗口高度。宽度没有问题。我尝试了许多不同的解决方案,但都没有奏效。

这是我目前所拥有的。我设法将 html 和正文扩展到高度:100%,但 div#container 给我带来了问题……它不会扩展到高度:100%。出于测试目的,我在 html、body 和 container 周围设置了边框,以查看哪个不起作用。得到任何帮助。我正在 VS2010 中设计我的网络应用程序并使用 Chrome(也在 IE 和 Mozilla 中进行了测试,但没有成功)。

html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
        <div id="container">
            <div id="headercontent">
                HEADER
            </div>
            <div id="leftcontent">
                <p>LEFT CONTENT</p>
                <p>MENU</p>
                <p>MENU</p>
                <p>MENU</p>
            </div>
            <div id="rightcontent">
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                </asp:ContentPlaceHolder>
            </div>
            <div id="footercontent">
                FOOTER
            </div>
        </div>
    </form>
</body>
</html>

还有我的 stylesheet.css

body{
    font-family: "Lucida Grande" , "Lucida Sans Unicode" , Verdana, Arial, Helvetica, sans-serif;
    font-size: 15px;
    background: yellow;
}

html{
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    border: 2px solid red;
}

body{
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    border: 2px solid blue;
}

#container{
    width: 100%;
    height: 100%;
    margin: 0px;
    border: 2px solid black;
    line-height:150%;
}

#headercontent{
    height: 70px;
    padding: 0.5em;
    color: white;
    background-color: gray;
   clear: left;
}

#leftcontent{
    float: left;
    width: 160px;
   margin: 0;
    padding: 1em;
    border-right: 1px solid gray;
}

#rightcontent{
    margin-left: 190px;
    border-left: 1px solid gray;
    padding: 1em;
}

#footercontent{
    padding: 0.5em;
    color: white;
    background-color: gray;
    clear: left;
}

【问题讨论】:

  • 我重新标记了您的问题,添加了 JQuery、Javascript 和 html,因为使用这些技术也可以使用许多解决方案

标签: javascript jquery asp.net html css


【解决方案1】:

您的 body 元素在您设置时是 100% 的高度,但您没有将内容设置为任何位置,以便它们遵循正常流程,就像一堆盒子一样。您也没有足够的内容来填满屏幕。如果您希望页脚粘在底部,请在 SO 或 Google 上搜索“sticky footer”。

如果您希望某些内容进一步延伸,则必须为其添加一些高度。

【讨论】:

  • 我确实寻找了粘性页脚并设法使其工作。我现在唯一的问题是,如何将内容拉伸为始终为 100% 的高度(最小值为 100%,最大值为 100%)。有什么建议么? :)
【解决方案2】:

只需执行以下操作:

设置html和body为height: 100%;:

html,
body {
  height: 100%;
}

下一步:用容器包裹表单并将其最小高度设为 100%:

#wrapper { 
  min-height: 100%;
}

这在 IE6 中不起作用。因此添加一个小技巧:

* html #wrapper {
  height: 100%;
}

【讨论】:

  • thnx,但它不起作用:(我尝试了很多东西(divs、table、divs+table),但都没有奏效。这真的让我很生气。不知道为什么......
  • 问题是......如果我在 w3schools“模拟器”中尝试相同的代码,一切都会完美地延伸......但是当我在浏览器中启动它时它不起作用。
【解决方案3】:

您的#container 元素嵌套在您的&lt;form&gt; 标记内,因此即使您已将#container 的高度设置为100%,这会将其高度设置为其父元素的100%,即@987654325 @标签。

尝试移动您的 #container &lt;div&gt; 标记,使其位于 &lt;form&gt; 标记之外,如下所示:http://jsfiddle.net/ianoxley/E4zAm/

【讨论】:

  • 我做了浮动页脚解决方案,但是直到我移动了我的表单标签它才起作用(我需要它在母版页上,因为有一个下拉列表,所以我不能只删除它) .感谢帮助。 :)
【解决方案4】:

如果你可以忍受 div#container 使用 position: absolute,你可以使用这个小技巧:

#container{
    position: absolute;
    top: 0; 
    left: 0;
    bottom: 0; 
    right: 0;
    margin: 0px;
    border: 2px solid black;
    line-height:150%;
}

将所有四个布局属性(上/右/下/左)设置为您想要的值将导致元素拉伸以适应这些值。

【讨论】:

  • 这是一个选项。但我想要百分比的原因是因为它将用于许多不同的分辨率和屏幕尺寸,所以我希望它具有灵活性。 :/ 我会发疯的。我尝试了几乎所有在谷歌上找到的解决方案,但都没有奏效。
猜你喜欢
  • 2013-02-16
  • 2012-12-20
  • 2011-09-03
  • 1970-01-01
  • 2013-01-17
  • 2010-11-03
  • 2010-09-06
  • 1970-01-01
  • 2013-02-16
相关资源
最近更新 更多