gpt4 book ai didi

html - flexbox 填充父级,但不要在溢出时拉伸(stretch)父级

转载 作者:搜寻专家 更新时间:2023-10-31 23:20:25 26 4
gpt4 key购买 nike

这是一个与 Can you make a flexbox child expand to fit parent but not contents? 类似的问题,但是,该解决方案对我不起作用。

我有一个布局,整个页面应该总是适合屏幕(在页眉和页脚之间),如果内容太大,那么它的容器应该滚动。 (基本上最后一个例子来自:https://css-tricks.com/snippets/css/a-guide-to-flexbox/)

我已经尝试了上面链接的问题的解决方案:

#chat {
background-color: #ceecf5;
flex-grow: 1;
overflow: auto;
}

但是父级仍然被拉出页面。

如果我将手动高度设置为 #chat 它可以正常工作,但它只适合我制作它的屏幕尺寸,所以我想要一个自动高度。

#flex-container {
display: flex;
flex-flow: column nowrap;
justify-content: flex-start;
min-width: 300px;
flex: 1;
}
#header {
height: 50px;
display: flex;
align-items: center;
justify-content: center;
background-color: red;
}
#column-flex-container {
display: flex;
flex-flow: row wrap-reverse;
justify-content: space-between;
flex: 1 1 auto;
align-items: stretch;
}
.column {
display: flex;
align-items: flex-start;
justify-content: center;
width: 300px;
overflow: auto;
background-color: aqua;
}
#chat {
background-color: #ceecf5;
flex-grow: 1;
overflow: auto;
}
#main-column {
flex: 1 1 auto;
background-color: azure;
}
#footer {
height: 50px;
background-color: red;
}
html,
body {
margin: 0;
display: flex;
min-height: 100vh;
flex-direction: column;
}
<div id="flex-container">
<div id="header"></div>
<div id="column-flex-container">
<div class="column side-column">
<div id="chat">
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
</div>
</div>
<div id="main-column" class="column"></div>
<div class="column side-column"></div>
</div>
</div>
<div id="footer" />

https://jsfiddle.net/dmn3gLz4/

最佳答案

需要考虑的几个问题:

最小高度:100vh

你写道:

I have a layout such that the whole page should always fit on screen (between header and footer), and if contents is too large, then its container should scroll.

那么你不应该将 body 元素的高度设置为 min-height: 100vh

使用 min-height,您允许元素从该点开始扩展。使用 高度:100vh

html,
body {
margin: 0;
display: flex;
/* min-height: 100vh; */
flex-direction: column;
height: 100vh; /* establish fixed height */
}

flex-flow: row wrap-reverse

如果您尝试构建三列布局,则不需要水平换行。

要反转列的顺序,请使用 row-reverse 而不是 wrap-reverse

#column-flex-container {
display: flex;
/* flex-flow: row wrap-reverse; */
justify-content: space-between;
flex: 1 1 auto;
align-items: stretch;
flex-flow: row-reverse nowrap; /* OR, just...
flex-direction: row-reverse; */
}

flex 收缩:1

您的页眉和页脚各有 height: 50px。但是因为它们是 flex 元素,并且 flex 元素默认可以收缩(flex-shrink: 1),所以页眉和页脚正在收缩。要保持 50 像素的高度,请禁用收缩(flex-shrink: 0,或仅使用 flex: 0 0 50px)。 More details ...


语法错误

您有一个错误的关闭 div。考虑将其移除。

        <div id="main-column" class="column"></div>
<div class="column side-column"></div>
</div>
<!-- </div> --> <-- STRAY CLOSING TAG
<div id="footer"></div>

revised fiddle

#flex-container {
display: flex;
flex-flow: column nowrap;
justify-content: flex-start;
min-width: 300px;
flex: 1;
}
#header {
/* height: 50px; */
display: flex;
align-items: center;
justify-content: center;
background-color: red;
flex: 0 0 50px; /* new */
}
#column-flex-container {
display: flex;
/* flex-flow: row wrap-reverse; */
justify-content: space-between;
flex: 1 1 auto;
align-items: stretch;
flex-direction: row-reverse;
}
.column {
display: flex;
align-items: flex-start;
justify-content: center;
width: 300px;
overflow: auto;
background-color: aqua;
}
#chat {
background-color: #ceecf5;
flex-grow: 1;
overflow: auto;
}
#main-column {
flex: 1 1 auto;
background-color: azure;
}
#footer {
height: 50px;
background-color: red;
flex-shrink: 0; /* new */
}
html,
body {
margin: 0;
display: flex;
height: 100vh; /* adjusted */
flex-direction: column;
}
<div id="flex-container">
<div id="header"></div>
<div id="column-flex-container">
<div class="column side-column">
<div id="chat">
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
<div class="message">Sender: Message</div>
</div>
</div>
<div id="main-column" class="column"></div>
<div class="column side-column"></div>
</div>
<!-- </div> -->
<div id="footer"></div>

关于html - flexbox 填充父级,但不要在溢出时拉伸(stretch)父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41922842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com