我如何使用nowrapflex-wrap 展开一个 flex 父级以适应其内容,即使这意味着溢出任何包裹父级的东西?
基本上,内容有一个最小宽度,我希望 flex 父项不要缩小超过所有 flex 项目所需的空间。
这是一个 JSFiddle https://jsfiddle.net/lazamar/odat477r/
.wrapper {
background-color: yellowgreen;
display: block;
padding: 10px;
max-width: 180px;
}
.parent {
display: flex;
flex-flow: row nowrap;
background-color: yellow;
}
.child {
display: block;
background-color: orange;
margin: 10px;
min-width: 50px;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
<div class="parent">
<div class="child">Content</div>
<div class="child">Content</div>
<div class="child">Content</div>
<div class="child">Content</div>
<div class="child">Content</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
使用display: inline-flex代替display: flex是你最好的选择。
如果出于某种原因这不是一个选项,请使用 CSS 定位属性。
.wrapper {
background-color: yellowgreen;
display: block;
padding: 10px;
max-width: 180px;
position: relative; /* new; set bounding box for flex container */
min-height: 40px; /* new */
}
.parent {
display: flex;
flex-flow: row nowrap;
background-color: yellow;
position: absolute; /* new; remove flex container from document flow */
}
.child {
/* display: block; <-- not necessary */
background-color: orange;
margin: 10px;
min-width: 50px;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
<div class="parent">
<div class="child">Content</div>
<div class="child">Content</div>
<div class="child">Content</div>
<div class="child">Content</div>
<div class="child">Content</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)