Languages
[Edit]
EN

CSS - set 100% body height (match to window height)

4 points
Created by:
Jan-Alfaro
281

In this article, we would like to show how to stretch body element to 100% height using CSS.

Note: it is required to set height: 100% for html and body elements to stretch body to window height.

Quick solution:

<!doctype html>
<html>
<head>
  <style>

    html, body {  /*  <-------------------- required html & body  */
        height: 100%;  /*  <--------------- required              */
    }

  </style>
</head>
<body>
  Body here ...
</body>
</html>

 

Practical example

The solution presented in this section shows nested div in body element that is stretched to body that has 100% width and height.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    html, body {  /*  <-------------------- required html & body  */
        height: 100%;  /*  <--------------- required              */
    }

    body {
        margin: 0;
    }

    div.layout {
        background: gold;
        width: 100%;
        height: 100%;
    }

  </style>
</head>
<body>
  <div class="layout">
    Layout ...
  </div>
</body>
</html>

Screenshot:

Body with 100% width and height - CSS.
Body with 100% width and height - CSS.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join