19

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>
TylerH
  • 20,799
  • 66
  • 75
  • 101
George Katsanos
  • 13,524
  • 16
  • 62
  • 98
  • Why are you using flexbox in this case? First apply browser reset CSS by adding `html, body { width: 100%; height: 100%; } body {margin: 0; padding: 0;}` And for your .wrapper class, just add `.wrapper { height: 100%; }` So long as html and body each have 100% height, the wrapper class being a direct child will also be able to apply 100% screen height. – Pegues Feb 28 '17 at 12:19
  • 1
    I also suggest you add `*,*:before,*:after { -webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box; }` – Pegues Feb 28 '17 at 12:20
  • Do you have to use flex? – Pegues Feb 28 '17 at 12:21
  • @Pegues: I am using flexbox because I want to vertically distribute my items (justify-content: space-around) . I've already set box-sizing to border-box, no change. So : yes, I need to use flex. – George Katsanos Feb 28 '17 at 12:21
  • It seems a bug in chrome... – Mohammad Usman Feb 28 '17 at 12:22
  • `display: table` will also suite your purposes, although you may need to modify your markup a little in that case. This will then let you distribute vertically. And my suggestion of using box-sizing was if you didn't use flex. Sorry for lack of clarity in that comment. – Pegues Feb 28 '17 at 12:23
  • @MuhammadUsman did I discover my first Chrome bug?:) – George Katsanos Feb 28 '17 at 12:24
  • 1
    Try creating an extra container and giving that `100vh`. Then put your wrapper element inside that with height `100%`. It's the `justify-content` property that's creating extra white-space. – Jazcash Feb 28 '17 at 12:27
  • @GeorgeKatsanos So you think that you can't be a lucky man? :) – Mohammad Usman Feb 28 '17 at 12:27
  • @Jazcash can you provide a full answer pls? (btw I don't want to force height to be 100vh) – George Katsanos Feb 28 '17 at 12:28
  • Change `min-height: 100vh` to `height: 100vh;`, and that gets rid of the whitspace: https://jsfiddle.net/rxz7jh9f/ It looiks like min-height: 100vh adds extra white space. – Pegues Feb 28 '17 at 12:31
  • The point is to use `min-height: 100vh` – Nenad Vracar Feb 28 '17 at 12:32
  • @Pegues : that's not an acceptable solution, I don't want to force the height as it might trim content off if the viewport isn't tall enough – George Katsanos Feb 28 '17 at 12:32
  • The point may be to use min-height: 100vh, but if it adds extra whitespace, what else are you gonna do? Flex is widely misused to begin with. – Pegues Feb 28 '17 at 12:33
  • https://jsfiddle.net/uxgaaccr/ - Took a few more properties than I thought it would. – Jazcash Feb 28 '17 at 12:36
  • @Jazcash can you add an "answer" and add some reasoning behind it? – George Katsanos Feb 28 '17 at 12:38
  • Switching `min-height: 100vh;` to `height: 100vh;` produces the same outcome as what @Jazcash provided, even with a reduced viewport and no trimming. Scrollbars appear in both solutions when the viewport height is too small. Perhaps you have additional markup not present here or a layout that is the reason for needing to use `min-height` instead of `height`. – Pegues Feb 28 '17 at 12:49

2 Answers2

15

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

Jazcash
  • 3,145
  • 2
  • 31
  • 46
  • and the plot thickens.. the workaround as such seems to work but we're still left wondering : why does this happen to begin with? I am having a peek at https://github.com/philipwalton/flexbugs as we speak. I can accept it now but I'd like to understand what's going wrong there – George Katsanos Feb 28 '17 at 12:45
  • Updated my answer to use `body` instead, doesn't need that extra element. But yeah, weird bug. – Jazcash Feb 28 '17 at 12:46
  • accepting your answer even though it seems there's a simpler way. – George Katsanos Feb 28 '17 at 12:57
4

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>
George Katsanos
  • 13,524
  • 16
  • 62
  • 98