How can I make a flexbox parent have the width of its children?

一曲冷凌霜 提交于 2019-12-23 07:16:00

问题


I have a parent element, with two elements inside:

<div class="parent">
  <div class="child">
     one
  </div>
  <div class="child">
    two
  </div>
</div>

Currently, the .parent is 100% wide. I would like it only to be the width of the two children.

.parent {
  display: flex;
  background-color: aliceblue;
}

How can I make the parent to be the width of it's contents, and not wider?

See codepen for a live demo.


回答1:


Change the parent's display from flex to inline-flex.

codepen demo



来源:https://stackoverflow.com/questions/33722001/how-can-i-make-a-flexbox-parent-have-the-width-of-its-children

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!