Home > Article > Web Front-end > Can You Stretch a Div to 100% Page Height Using Only CSS?
Stretching a Div to 100% Page Height in CSS
Enhancing a webpage's aesthetics and functionality often involves aligning elements precisely to occupy specific portions of the page. One common requirement is stretching a navigation menu or sidebar to span the entire height of the viewport, extending beyond the visible area when scrolled.
Can It Be Done Without Javascript?
The question of whether such a feature can be achieved solely using HTML and CSS has been raised by developers. The answer is a resounding yes.
The Solution
To resolve this issue, consider the following CSS modifications:
Here's an example CSS code that demonstrates the solution:
<code class="css">html { min-height: 100%; /* ensures minimum viewport height */ position: relative; } body { height: 100%; /* matches the height of the HTML element */ } #cloud-container { position: absolute; top: 0; bottom: 0; left: 0; right: 0; overflow: hidden; }</code>
The accompanying HTML code:
<code class="html"><!doctype html> <html> <body> <div id="cloud-container"></div> </body> </html></code>
The Rationale
The html {min-height: 100%; position: relative;} declaration ensures the cloud-container div remains within the HTML layout, allowing for accurate height calculations.
The above is the detailed content of Can You Stretch a Div to 100% Page Height Using Only CSS?. For more information, please follow other related articles on the PHP Chinese website!