将div高度扩展到整个网页

4
有没有一种方法可以使用CSS将div的高度扩展到整个网页?
我不使用任何
5个回答

5

请确保所有父级元素都设置了高度。

html / body / etc.

如果父元素有高度,则该元素将占用其父元素的100%高度。

非设置高度的100%将导致另一个非设置高度 ;)


谢谢你实际上解释了为什么。 - edgerunner

3
使用 min-height 属性。
.height {
   min-height: 100%;
}

但是这非常依赖于上下文,而您没有提供。

1
<div><!-- make a <div> to hold everything in.. -->

<div style="width:125;height:100%;">blah blah blah</div>

<div style="height:100%;">blah blah blah</div>

</div>

0
这对我有用:
<html>
    <head>
        <style type="text/css">
            body {
                margin: 0;
                padding: 0;
            }

            #wholepage {
                height: 100%;
                width: 100%;
                background: red;
                color: white;
                font-weight: 800;
                font-size: 22px;
                font-family: sans-serif;
            }
        </style>
    </head>
    <body>
        <div id="wholepage">
            Simple Test
        </div>
    </body>
</html>

0

你可能需要为 <html><body> 元素指定高度:

<!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" xml:lang="en" lang="en">
<head>
    <title>extend div height to entire webpage</title>
    <style type="text/css">
        html, body {
            height:100%;
        }
        #tehDiv {
            height:100%;
            background:red;
        }
    </style>
</head>
<body>
    <div id="tehDiv">Example</div>
</body>
</html>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接