How to give a div container the full height of the browser window?

  • css
  • 57 Views
  • 1 Min Read
  • 9 Jul 2024

Hi! Let's quickly see how to give a div container the full height of the browser window. We can use 'vh' as a unit for this. 'vh' stands for viewport height, which means the height of your browser window. It's a common way to make things fit perfectly!

 

The vh unit determines the height of an element in relation to the height of the browser window.

 
<head>
    <style>
        * {
            margin: 0;
        }

        #myDiv {
            height: 100vh;
            background-color: steelblue;
        }
    </style>
</head>

<body>
    <div id="myDiv"></div>
</body>

 

Element full height

 

When we set an element's height to 100vh, it fills the entire height of the viewport and automatically takes up the full width if the width is not defined. However, you can still adjust the width of the element independently if needed.

 

Using VH units for height means 1vh equals 1% of your browser window's height. So, 100vh equals the full height of the browser window, no matter where the element is placed on the webpage.

 

It's important to avoid using percentage units in parent elements without content. Without content, percentage units lose their meaning. Many developers face this problem when they set heights using % units, and it doesn't work as expected.

 

I hope you grasp, How to give a div container the full height of the browser window. Remember, avoid using percentage % units in parent elements without content.

 

If you have any questions related to web development, you can ask us in the question box given below, which you will get as soon as possible.

Didn't find your answer? Add your question.

Share

Comments (0)

No comments yet. Be the first to comment!