Search code examples
htmlcssflexboxviewport-units

min-height 100vh creates vertical scrollbar even though content is smaller than viewport


I'm applying min-height: 100vh; to a flexbox container and I get a vertical scrollbar when I use justify-content: space-around;

I don't want to force the height, but I don't understand why the scrollbar is there since the contents have smaller dimensions than the viewport.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body {
  margin: 0;
  padding: 0;
}
.wrapper {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}
<link href="https://necolas.github.io/normalize.css/5.0.0/normalize.css" rel="stylesheet"/>
<div class="wrapper">
  <div>
    <h1>min-height: 100vh;</h1>
    <h2>why is there a scrollbar here?</h2>
  </div>
  <div>
    Be sure to expand window. 
    <a href="#">skill one</a>
    <a href="#">skill one</a>
    <a href="#">skill one</a>
    <a href="#">skill one</a>
  </div>
</div>


Solution

  • Adding flex-grow seems to do the trick:

    body {
      min-height: 100vh;
      display: flex;
      flex-direction: column;
    }
    
    .wrapper {
      display: flex;
      flex-direction: column;
      flex-grow: 1;
      justify-content: space-around;
    }
    

    https://jsfiddle.net/uxgaaccr/2/

    Not sure why, but height: 100% on .wrapper doesn't seem to suffice, it needs flex-grow instead. I think there was some extra white-space coming from justify-content: space-around that was adding to the height. Not confident in my reasoning, but it seems to work...