Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this div with 100vh height not covering the whole viewport?

Tags:

html

css

The blue div in this code has a height of 100vh whereas the red div has the height of 5000px. Shouldn't the blue div extend itself to cover the whole viewport because of the 100vh height attribute?

body {
  margin: 0;
}
<div style="position: absolute; left: 35%; width: 100px; height: 100vh; background-color: blue;">
</div>
<div style="position: absolute; left: 50%; width: 100px; height: 5000px; background-color: red;"></div>

What I want to achieve from this is that suppose there's some text which overflows on sizing down the window then how should I get around that situation so that the 100vh height div again covers up the new window size?

like image 713
Neeraj Kumar Avatar asked Apr 06 '19 14:04

Neeraj Kumar


People also ask

Why the 100vh is not working?

This is caused by the default margin being 8px so redefining it using CSS will correct it.

Is 100vh same as 100%?

100% is 100% width/height of its parent width/height. And 100vh is not means 100% unless its parent is 100vh height.

How do you adjust the height of a viewport?

A simple way to solve this is to use the viewport percentage unit vh instead of %. One vh is defined as being equal to 1% of a viewport's height. As such, if you want to specify that something is 100% of the latter, use " height: 100vh ". Or if you want to make it half the height of the viewport, say " height: 50vh ".

What does height 100vh mean?

Here, 100vh means that the initial body height will take 100% of the viewport height, whereas the use of min-height instead of height will let the body element grow even more if necessary.


1 Answers

Viewport height just means that the height of the element will be a percentage of the viewport. The viewport being the window your viewing the page on. So if it is a 1920x1080 screen the viewport height will be 1080px or whatever your browser window is, it may be less if you have a toolbar at the bottom of your screen. The issue you are having is that if you want the sidebar to follow the main content down the page you need to disregard the viewport and have a wrapper around both of them like so:

.main-wrapper{
  display:flex;
}

aside{
   background:blue;
   color:#fff;
   width:200px;
}

main{
  background:green;
  height:5000px;
  flex:1
}
<div class="main-wrapper">
  <aside>Your sidebar</aside>
  <main>Your Main Content</main>
</div>

That being said you may want to check out the following fiddle Here. I have made this fiddle responsive for mobile screens so you may have to resize the output window larger to view the sidebar layout. The issue is you are absolutely positioning items and fixing items outside divs which is bad practice. In your example you commented on above you have many layout and z-index issues that you will run into. This fiddle is just an example but it should give you a good frame of reference on how the layout should be thought of.

like image 181
Steve K Avatar answered Oct 20 '22 20:10

Steve K