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

46,297

Solution 1

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...

Solution 2

Based on @Jazcash's answer above, it can be simplified even more.

Essentially we need to move the min-height: 100vh to the parent element, and apply display: flex to it. flex-grow isn't required.

https://jsfiddle.net/gkatsanos/uxgaaccr/3/

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  display: flex;
}

.wrapper {
  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>
Share:
46,297
George Katsanos
Author by

George Katsanos

Free for new projects starting June 2020 (short/med term range, partially remote, working out of Athens &amp; Berlin) Senior Experienced Web Developer with a focus on UI/UX &amp; Frontend Development with 10 years of experience in both the Corporate &amp; the startup world. Delivery &amp; scalability oriented, technical consulting : build on proper foundation, avoid costly mistakes. I collaborate with a wide range of Developers &amp; Designers.

Updated on August 19, 2020

Comments

  • George Katsanos
    George Katsanos almost 4 years

    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>