Flexbox - justify specific element [duplicate]

南笙酒味 提交于 2020-01-05 05:30:07

问题


I’m using Flexbox, I want to create container that contains two divs in one row. One of them should be horizontally centered(relatively to container) and second one should be on the left(like it wouldn't be justified). Is there any solution that doesn’t require a third, hidden div?

Unfortunately both of divs don’t have specified width.

Here is the structure of html:

<div class="container">
  <div>Div on the left</div>
  <div>Horizontally centered div</div>
</div>

回答1:


You can do it with the positioning:

.container {
  display: flex;
  justify-content: center;
  position: relative;
}

.container > div:first-child {
  position: absolute;
  left: 0;
}
<div class="container">
  <div>Div on the left</div>
  <div>Horizontally centered div</div>
</div>



回答2:


this is not perfect, but you can do this.

.container {
  display: flex;
}

.container > div{
   margin-right: auto;
}

.container > div:first-child {
  min-width:0;
  width:0;
  white-space:nowrap;
}
<div class="container">
  <div>Div on the left</div>
  <div>Horizontally centered div</div>
</div>


来源:https://stackoverflow.com/questions/50273585/flexbox-justify-specific-element

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