这是简化版的布局:
<div class="root">
<div class="container">
<div class="cell">
<input type="text" class="contact"></input>
</div>
</div>
</div>
这是简化的CSS:
.root {
background-color: red;
overflow: auto;
width: 300px;
}
.container {
background-color: green;
display: flex;
height: 50px;
}
.cell {
background-color: black;
height: 30px;
}
.contact {
width: 400px;
}
这是jsFiddle。
对我来说,出乎意料的是,container
宽度不是其子元素所需的宽度,而是被root
div
限制的。 您可以在此jsFiddle中看到红色区域(根div
)未填充绿色container
div
。
有人能解释一下为什么flex
容器的宽度不会随着其子元素而增长以及如何修复吗?