【发布时间】:2017-12-16 00:23:40
【问题描述】:
如果您使用 Flexbox 布局将某些项目排成一行,则 Flexbox 容器会占据其所在容器的整个宽度。如何将 Flexbox 容器设置为仅占据其子元素的宽度?
看看下面的例子:
https://jsfiddle.net/5fr2ay9q/
在本例中,flexbox 容器的大小超出了子元素的宽度。解决此类问题的典型方法是display: inline,但显然这不起作用,因为它不再是 flexbox 容器了。
可以这样做吗?
.container {
display: flex;
flex-direction: row;
outline: 1px solid red;
}
p {
outline: 1px solid blue;
}
Can you make the flex container not 100% width but rather the width of the content itself?
<div class='container'>
<img src='https://placehold.it/300x300'/>
<p>
This is some content hello world testing 123.
</p>
</div>
【问题讨论】:
-
我不同意将问题标记为 Difference between display:inline-flex and display:flex 的副本。这是一个完全不同的问题:提问者不知道 inline-flex 与他们的“我该怎么做?”有关。除非他们在提问之前已经知道问题的答案!