Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent vh-units from ignoring the scrollbar?

I am working on a page layout with a horizontal scrollbar. I have some panels with fixed widths in a horizontal order, which should all have the viewport height. I decided to test the vh unit to do that.

.panel { height: 100vh; }

This is working fine, until I get a scrollbar. The problem is, that vh ignores the height used by the scrollbar and therefore adds a vertical scrollbar.

I would like to subtract the height of the scrollbar from the measurements of vh; is there any way to do this?

like image 825
Afterlame Avatar asked Oct 29 '13 20:10

Afterlame


People also ask

Does VH include scrollbar?

This is true on a page that doesn't scroll vertically, but what you might not realize is that the viewport actually includes the scrollbars, too.

Why 100vh is not working?

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

How do I lock the scrollbar in HTML?

Add overflow: hidden; to hide both the horizontal and vertical scrollbar.

How do I disable scroll overflow?

To hide the scrollbar and disable scrolling, we can use the CSS overflow property. This property determines what to do with content that extends beyond the boundaries of its container. To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element.


1 Answers

You could use a parent element with overflow-y: hidden to ensure that you don't get a vertical scrollbar and then safely use 100vh inside it. This is assuming that you do actually need 100vh for some reason and don't just need to fill the vertical space.

HTML

<div id="main">
  <div id="container"></div>
</div>

CSS

#main {
  width:200vw;
  height:100%;
  background: red;
  position: absolute;
  overflow-y: hidden;
}

#container {
  height: 100vh;
  width:10px;
  background: blue;
}
like image 98
mirichan Avatar answered Sep 22 '22 01:09

mirichan