CSS Div stretch 100% page height

Viewed 421516

I have a navigation bar on the left hand side of my page, and I want it to stretch to 100% of the page height. Not just the height of the viewport, but including the areas hidden until you scroll. I don't want to use javascript to accomplish this.

Can it be done in HTML/CSS?

14 Answers

It's simple using a table:

<html>

<head>
    <title>100% Height test</title>
</head>

<body>
    <table style="float: left; height: 100%; width: 200px; border: 1px solid red">
        <tbody>
            <tr>
                <td>Nav area</td>
            </tr>
        </tbody>
    </table>
    <div style="border: 1px solid green;">Content blabla... text
        <br /> text
        <br /> text
        <br /> text
        <br />
    </div>
</body>

</html>

When DIV was introduced, people were so afraid of tables that the poor DIV became the metaphorical hammer.

I want to cover the whole web page before prompting a modal popup. I tried many methods using CSS and Javascript but none of them help until I figure out the following solution. It works for me, I hope it helps you too.

<!DOCTYPE html>
<html>
 <head>
  <style>
   html, body {
       margin: 0px 0px;
       height 100%;
   }
          
            div.full-page {
                position: fixed;
                top: 0;
                bottom: 0;
                width: 100%;
                height: 100%;
                background-color: #000;
                opacity:0.8;
                overflow-y: hidden;
                overflow-x: hidden;
            }
          
            div.full-page div.avoid-content-highlight {
                position: relative;
                width: 100%;
                height: 100%;
            }
          
            div.modal-popup {
                position: fixed;
                top: 20%;
                bottom: 20%;
                left: 30%;
                right: 30%;
                background-color: #FFF;
                border: 1px solid #000;
            }
  </style>
  <script>
  
   // Polling for the sake of my intern tests
   var interval = setInterval(function() {
    if(document.readyState === 'complete') {
     clearInterval(interval);
     isReady();
    }    
   }, 1000);
   
   function isReady() {
    document.getElementById('btn1').disabled = false;
    document.getElementById('btn2').disabled = false;
    
    // disable scrolling
                document.getElementsByTagName('body')[0].style.overflow = 'hidden';
   }
   
   function promptModalPopup() {
                document.getElementById("div1").style.visibility = 'visible';
                document.getElementById("div2").style.visibility = 'visible';
    
    // disable scrolling
                document.getElementsByTagName('body')[0].style.overflow = 'hidden';
            }
          
            function closeModalPopup() {
                document.getElementById("div2").style.visibility = 'hidden';  
                document.getElementById("div1").style.visibility = 'hidden';
    
    // enable scrolling
                document.getElementsByTagName('body')[0].style.overflow = 'scroll';
            }
  </script>
  
 </head>
 <body id="body">
  <div id="div1" class="full-page">
   <div class="avoid-content-highlight">
                
            </div>
  </div>
        <button id="btn1" onclick="promptModalPopup()" disabled>Prompt Modal Popup</button>
  <div id="demo">
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
   <h2>Original content</h2>
  </div>
        <div id="div2" class="modal-popup">
            I am on top of all other containers
            <button id="btn2" onclick="closeModalPopup()" disabled>Close</button>
        <div>
 </body>
</html>

Good luck ;-)

This is how you can make your side nav as tall as the page content, without having to change the body to be flex or table.

Don't set html or body to height 100%, because that will make it only as tall as the browser viewport, and the page will be overflowing that, and your nav will only be as tall as the viewport.

Just set your nav to height:100% position:absolute with the html tag position:relative.

The reason this works is because height 100% only works if its container is fixed height, with the exception (for some reason) the html tag.

<html style='position:relative'>
    <body style='margin:0'>
        <div style='height:100%; position:absolute; top:0; background:linear-gradient(to bottom,red,green); border:2px solid blue'>
            nav
        </div>
        <div style='font-size:99px;padding:33px'>
            I want my side div to be as tall as the page content.<br />
            I want my side div to be as tall as the page content.<br />
            I want my side div to be as tall as the page content.<br />
            I want my side div to be as tall as the page content.<br />
            I want my side div to be as tall as the page content.<br />
            I want my side div to be as tall as the page content.<br />
            I want my side div to be as tall as the page content.<br />
            I want my side div to be as tall as the page content.<br />
            I want my side div to be as tall as the page content.<br />
            I want my side div to be as tall as the page content.<br />
            I want my side div to be as tall as the page content.<br />
        </div>
    </body>
</html>

I succeeded with

min-height: 100vh

for both, the menu and content div.

Related