简体   繁体   English

Div 高度:100% 不适用于身体最小高度:100vh

[英]Div height: 100% not working with body min-height: 100vh

I encountered what for me is an unexpected behaviour.我遇到了对我来说出乎意料的行为。 I wanted to set the body min-height to the height of the viewport (100vh), then make a div (with the class of container) inside it that would take the whole height of the body (100%), so that the div would extend at least to all of the height of the viewport.我想将body最小高度设置为视口的高度(100vh),然后在其中制作一个div (带有容器的 class),它将占据身体的整个高度(100%),这样 div将至少延伸到视口的所有高度。 Except that this doesn't happen;除非这不会发生; the only way to achieve this result is to set html and body height to 100%.实现此结果的唯一方法是将htmlbody高度设置为 100%。 What's the technical reason that makes this this happen?造成这种情况的技术原因是什么? Shouldn't the div take the (min-)height of 100% of its parent anyway? div 不应该采用其父级的 100% 的(最小)高度吗?

Here you can find a codepen with the expected behaviour commented out at the end of the css在这里您可以找到在 css 末尾注释掉的预期行为的代码笔

 *, *::after, *::before { margin: 0; padding: 0; box-sizing: border-box; } body { background: #ddd; min-height: 100vh; }.container { background: #aaa; max-width: 500px; margin: 0 auto; padding: 1rem; min-height: 100%; height: 100%; } /* Uncomment for the expected behaviour */ /* html, body { height: 100% } */
 <div class="container"> <h1>Some generic title</h1> <p>Some random paragraph</p> </div>

The answer is simple as percentage min-height is calculated based on parent container's height value not min-height .答案很简单,因为min-height百分比是根据父容器的height值而不是min-height计算的。

 * {margin:0;} body { background: gray; height: 100vh; /* look here */ }.container { background: silver; margin: 0 20px; min-height: 100%; /* look here */ }
 <div class="container"> <h1>Some generic title</h1> <p>Some random paragraph</p> </div>

But, with min-height:inherit it then inherits the min-height value from the parent container.但是,使用min-height:inherit它会从父容器继承min-height值。

 * {margin:0;} body { background: gray; min-height: 100vh; /* look here */ }.container { background: silver; margin: 0 20px; min-height: inherit; /* look here */ }
 <div class="container"> <h1>Some generic title</h1> <p>Some random paragraph</p> </div>

I would set min-height:100% on .container directly if it always covers the viewport height.如果它总是覆盖视口高度,我会直接在.container上设置min-height:100%

 * {margin:0;} body { background: gray; }.container { background: silver; margin: 0 20px; min-height: 100vh; /* look here */ }
 <div class="container"> <h1>Some generic title</h1> <p>Some random paragraph</p> </div>

 *, *::after, *::before { margin: 0; padding: 0; box-sizing: border-box; } body { background: #ddd; min-height: 100vh; height: 100vh; }.container { background: #aaa; max-width: 500px; margin: 0 auto; padding: 1rem; min-height: 100%; height: 100%; } /* Uncomment for the expected behaviour */ /* html, body { height: 100% } */
 <div class="container"> <h1>Some generic title</h1> <p>Some random paragraph</p> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM