<分区>
<body>
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
</body>
如何设置“ child ”相对于 body 的高度,同时让“ parent ”占据 child 的全部高度?
最佳答案
如果 body
是视口(viewport)高度的 100%,它也是 100 vh(视口(viewport)高度)单位高。您可以使用它们来设置您的子元素:
body {
height: 100%; /* could also be "100vh" for better clarity */
}
.child {
height: 50vh; /* 50% of the height of the body and viewport
}
通常,父元素将采用子元素的总高度。根据它们的内容,您可能不得不在其中一个或两个上摆弄 overflow
属性。
关于html - 如何设置元素相对于其祖父元素的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53292648/