gpt4 book ai didi

html - 比屏幕大的 Flexbox child

转载 作者:可可西里 更新时间:2023-11-01 13:24:12 24 4
gpt4 key购买 nike

我试图让 flexbox 元素不会因其内容而变得比屏幕大。

这是一些基本代码:

section,
aside {
font-size: 1.5em;
}
#container1,
#container2 {
display: flex;
}
#overflower {
overflow-x: auto;
}
aside {
background-color: red;
flex-basis: 15em;
width: 15em;
min-width: 15em;
flex-grow: 0;
}
section {
background-color: green;
flex-grow: 1;
flex-basis: 15em;
}
<div id="container1">
<aside>Short text OK</aside>
<section>Short text OK</section>
</div>
<br>
<div id="container2">
<aside>Short text OK</aside>
<section>
<div id="overflower">
<table>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
</table>
</div>
</section>
</div>

我也做了一个 jsfiddle 来解释它:https://jsfiddle.net/51jxxe4p/2/

左边的列有固定的宽度,右边的应该使用剩余的空间(正确的行为)。但是,如果表格太大,它会长到屏幕之外,我尽量避免这种情况。我以为 #overflower 会阻止它,但它不起作用。我尝试在各个地方将宽度设置为 100%,但也没有成功。

最佳答案

不要在其中添加任何溢出元素,直接将 overflow-x: auto 设置为 flex 元素。

section, aside {
font-size: 1.5em;
}
#container1, #container2 {
display: flex;
}
aside {
background-color:red;
flex-basis: 15em;
width: 15em;
min-width: 15em;
flex-grow: 0;
}
section {
background-color: green;
flex-grow: 1;
flex-basis: 15em;
overflow-x: auto;
}
<div id="container1">
<aside>Short text OK</aside>
<section>Short text OK</section>
</div>
<br>
<div id="container2">
<aside>Short text OK</aside>
<section>
<table>
<tr>
<td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td>
</tr>
</table>
</section>
</div>

原因是 flex 元素中隐含的最小尺寸,如 Why doesn't flex item shrink past content size? 中所述。

关于html - 比屏幕大的 Flexbox child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41731600/

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