[英]CSS - flexbox height 100% don't consider children's heights
div.container { display: flex; flex-direction: column; height: 100%; } div.container div.block { display: flex; justify-content: center; align-items: center; } div.container div.advanced-items { display: flex; flex-direction: column; overflow-y: scroll; height: 100%; } div.container div.advanced-items span { height: 100px; }
<div class="container"> <div class="block"> <span>some data here</span> </div> <div class="block"> <span>some data here</span> </div> <div class="advanced-items"> <span>1. item</span> <span>2. item</span> <span>3. item</span> <!-- more items here --> </div> </div>
div.advanced-items
高度值设置为100%
,但它以某种方式等于整个div.container
高度。 但考虑到孩子的身高,我需要填充div.container
的剩余高度。
请帮帮我,如何制作?
考虑到儿童的身高,我需要填充div.container
的剩余高度。 正是它发生了什么,看看它是如何发生的,只需向div.advanced-items
添加border
以查看它包裹了多少空间
div.container { display: flex; flex-direction: column; height: 100%; border: 1px solid black; } div.container div.block { display: flex; justify-content: center; align-items: center; } div.container div.advanced-items { display: flex; flex-direction: column; overflow-y: scroll; height: 100%; border: 1px solid black; } div.container div.advanced-items span { height: 100px; }
<div class="container"> <div class="block"> <span>some data here</span> </div> <div class="block"> <span>some data here</span> </div> <div class="advanced-items"> <span>1. item</span> <span>2. item</span> <span>3. item</span> <!-- more items here --> </div> </div>
现在你的div.advanced-items
包裹了所有左边的高度,除了上面的divs
包裹的高度,但你可以通过将它们的height
设置为100%
来使它们平均共享其父级的height
div.container { display: flex; flex-direction: column; height: 100%; border: 1px solid black; } div.container div.block { display: flex; justify-content: center; align-items: center; height: 100%; border: 1px solid black; } div.container div.advanced-items { display: flex; flex-direction: column; overflow-y: scroll; height: 100%; border: 1px solid black; } div.container div.advanced-items span { height: 100px; }
<div class="container"> <div class="block"> <span>some data here</span> </div> <div class="block"> <span>some data here</span> </div> <div class="advanced-items"> <span>1. item</span> <span>2. item</span> <span>3. item</span> <!-- more items here --> </div> </div>
你想要这样吗? :
div.container { display: flex; flex-direction: column; height: 100%; } div.container div.block { display: flex; justify-content: center; align-items: center; } div.container div.advanced-items { display: flex; flex-direction: column; overflow-y: scroll; height: 100%; } div.container div.advanced-items span { height: calc(100% / var(--item-count)); min-height : 100px; }
<div class="container"> <div class="block"> <span>some data here</span> </div> <div class="block"> <span>some data here</span> </div> <div style="--item-count:3;" class="advanced-items"> <span>1. item</span> <span>2. item</span> <span>3. item</span> <!-- more items here --> </div> </div>
或者这个? :
div.container { display: flex; flex-direction: column; height: 100%; } div.container div.block { display: flex; justify-content: center; align-items: center; } div.container div.advanced-items { display: flex; flex-direction: column; justify-content: stretch; overflow-y: scroll; height: 100%; } div.container div.advanced-items span { height: 100px; }
<div class="container"> <div class="block"> <span>some data here</span> </div> <div class="block"> <span>some data here</span> </div> <div style = "--itemCount:3;" class="advanced-items"> <span>1. item</span> <span>2. item</span> <span >3. item</span> <!-- more items here --> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.