弹性孩子长大了父母 [英] flex child is growing out of parent
问题描述
如何在不设置静态高度值且没有绝对位置的情况下强制将绿色框包含在红色框中?
How to force the green boxes to be contained in the red one without setting a static height value and no absolute position?
我想缩小内容以适合父项.
I want to shrink the content to fit into the parent.
允许内容(在这种情况下为视频)缩小,并允许滚动条.
The content (video in this case) is allowed to shrink and scrollbars are allowed.
.my-box {
height: 300px;
width: 600px;
background: red;
padding: 5px;
}
.content-box {
background: blue;
}
.col {
display: flex;
flex-direction: column;
justify-content: space-between
}
.box-shrink {
flex: 0 1 auto;
background: green;
padding: 5px;
margin: 5px;
}
.box-grow {
flex: 1 0 auto;
background: green;
padding: 5px;
margin: 5px;
}
video {
max-height: 100%;
max-width: 100%;
margin: auto;
display: block;
}
<div class="my-box col">
<div class="box-shrink">
small sized static content
</div>
<div class="content-box box-grow">
<video controls>
<source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm">
</video>
</div>
<div class="box-shrink">
small sized static content
</div>
</div>
推荐答案
解决方案1-无需滚动
使用flex: 1
代替视频容器上的flex: 1 0 auto
.这样会根据可用空间而不是内容的固有高度来调整项目的大小.
Solution #1 - Without Scroll
Instead of flex: 1 0 auto
on the video container, just use flex: 1
. This sizes the item based on available space, not the intrinsic height of the content.
然后,因为弹性项目不能小于其内容的大小– min-height: auto
是默认值–添加min-height: 0
以使项目收缩以适合容器内部.
Then, because flex items cannot be smaller than the size of their content – min-height: auto
is the default – add min-height: 0
to allow the item to shrink to fit inside the container.
.box-grow {
flex: 1; /* formerly flex: 1 0 auto; */
background: green;
padding: 5px;
margin: 5px;
min-height: 0; /* new */
}
.my-box {
height: 300px;
width: 600px;
background: red;
padding: 5px;
}
.content-box {
background: blue;
}
.col {
display: flex;
flex-direction: column;
justify-content: space-between
}
.box-shrink {
flex: 0 1 auto;
background: green;
padding: 5px;
margin: 5px;
}
.box-grow {
flex: 1; /* formerly flex: 1 0 auto; */
background: green;
padding: 5px;
margin: 5px;
min-height: 0; /* new */
}
video {
max-height: 100%;
max-width: 100%;
margin: auto;
display: block;
}
<div class="my-box col">
<div class="box-shrink">
small sized static content
</div>
<div class="content-box box-grow">
<video controls>
<source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm">
</video>
</div>
<div class="box-shrink">
small sized static content
</div>
</div>
或者,给视频容器overflow: auto
,除了保持视频全角外,它的操作与上述相同.您需要启用flex-shrink
才能使其正常工作.
Alternatively, give the video container overflow: auto
, which does the same as above, except it keeps the video full-width. You need to enable flex-shrink
for this to work.
.box-grow {
flex: 1 1 auto; /* formerly flex: 1 0 auto; */
background: green;
padding: 5px;
margin: 5px;
overflow: auto; /* new */
}
.my-box {
height: 300px;
width: 600px;
background: red;
padding: 5px;
}
.content-box {
background: blue;
}
.col {
display: flex;
flex-direction: column;
justify-content: space-between
}
.box-shrink {
flex: 0 1 auto;
background: green;
padding: 5px;
margin: 5px;
}
.box-grow {
flex: 1 1 auto; /* formerly flex: 1 0 auto; */
background: green;
padding: 5px;
margin: 5px;
overflow: auto; /* new */
}
video {
max-height: 100%;
max-width: 100%;
margin: auto;
display: block;
}
<div class="my-box col">
<div class="box-shrink">
small sized static content
</div>
<div class="content-box box-grow">
<video controls>
<source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm">
</video>
</div>
<div class="box-shrink">
small sized static content
</div>
</div>
这两种解决方案均在此处进行了详细说明:
Both solutions are explained in more detail here:
这篇关于弹性孩子长大了父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!