Here are a few important things to keep in mind:
Padding and Margins: When using height:100%
, be wary of how padding and margins can affect the element's height. For example, if you place an h1
inside a div
with height:100%
, the margin on the h1
may cause unexpected issues. Additionally, remember that html
(or maybe body
) often have default padding values of 5px
or 10px
.
Height on html/body: In some browsers, setting a div
with height:100%
may not actually result in 100% height. To ensure this works as expected, it may be necessary to explicitly set the height of both html
and body
to 100%. This is a common practice.
Remember to test your code thoroughly. The following example will display a full-screen green area followed by a full-screen red area upon scrolling:
<html>
<head>
<style>
html, body {
margin: 0px;
padding: 0px;
height: 100%;
width: 100%;
}
.full {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div class="full" style="background:green">
</div>
<div class="full" style="background:red">
</div>
</body>
</html>