英文:
Can't add flex-grow for column flex box with min-height
问题
以下是代码中需要翻译的部分:
I am trying to design a website layout that uses the power of flex box in order to fill the entire page with content even when the content does not fill the entire length of the page.
I have created a big flex-box container with flex-direction: column;
. When I set the last child's to have flex-grow: 1;
and all of the rest of the children have flex-grow: unset;
something breaks, and the flex-grow
property does not affect the content of the children when I am using min-height
.
refer to the following example on JSFiddle.
The .should-stretch
class represents the content of one of the children that should fill the entire children's height. When I am using height: 100px
, everything works well (See the first box, the yellow area fills the entire space).
When I am using min-height: 100px;
, The yellow area does not fill the entire height anymore.
I tried using another flex box inside the child, but it is not a best practice for my situation because this is a generic component, and the number of "contents" in a children is unknown or dynamic.
英文:
I am trying to design a website layout that uses the power of flex box in order to fill the entire page with content even when the content does not fill the entire length of the page.
I have created a big flex-box container with flex-direction: column;
. When I set the last child's to have flex-grow: 1;
and all of the rest of the childrens have flex-grow: unset;
something breaks and the flex-grow
property does not affect the content of the children when I am using min-height
refer to the following example on JSFiddle.
The .should-strech
class represents the content of one of the childrens that should fill the entire children's height. When I am using height: 100px
everything works well (See the first box, the yellow area fills the entire space).
When I am using min-height: 100px;
The yellow area does not fills the entire height anymore.
I tried using another flex box inside the child but it is not a best practice for my situation becuase this is a generic component that the number of "contents" in a children is unknown or dynamic.
<div class="container height">
<div class="child">
1
</div>
<div class="child">
<div class="should-strech">
2
</div>
</div>
</div>
<br/>
<div class="container min-height">
<div class="child">
1
</div>
<div class="child">
<div class="should-strech">
2
</div>
</div>
</div>
.min-height {
min-height: 100px;
}
.height {
height: 100px;
}
.container {
width: 100px;
display: flex;
flex-direction: column;
background-color: aqua;
}
.child {
background-color: red;
}
.child:last-child {
background-color: green;
flex-grow: 1;
}
.should-strech {
background-color: yellow;
height: 100%;
}
答案1
得分: 1
尝试嵌套的 flexbox,如下所示:
.min-height {
min-height: 100px;
}
.height {
height: 100px;
}
.container {
width: 100px;
display: flex;
flex-direction: column;
background-color: aqua;
}
.child {
background-color: red;
display: flex;
flex-direction: column;
}
.child:last-child {
background-color: green;
flex-grow: 1;
}
.should-stretch {
background-color: yellow;
flex-grow: 1;
}
<div class="container height">
<div class="child">
1
</div>
<div class="child">
<div class="should-stretch">
2
</div>
</div>
</div>
<br/>
<div class="container min-height">
<div class="child">
1
</div>
<div class="child">
<div class="should-stretch">
2
</div>
</div>
</div>
希望这个翻译对你有帮助。
英文:
Try a nested flexbox like below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.min-height {
min-height: 100px;
}
.height {
height: 100px;
}
.container {
width: 100px;
display: flex;
flex-direction: column;
background-color: aqua;
}
.child {
background-color: red;
display: flex;
flex-direction: column;
}
.child:last-child {
background-color: green;
flex-grow: 1;
}
.should-strech {
background-color: yellow;
flex-grow: 1;
}
<!-- language: lang-html -->
<div class="container height">
<div class="child">
1
</div>
<div class="child">
<div class="should-strech">
2
</div>
</div>
</div>
<br/>
<div class="container min-height">
<div class="child">
1
</div>
<div class="child">
<div class="should-strech">
2
</div>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论