【问题标题】:Positioning flex items定位弹性项目
【发布时间】:2015-06-04 22:49:56
【问题描述】:

我有一个包含三个 flex 项子项的 flexbox 容器。我希望第一个 div 位于左侧(flex-start),第二个需要水平居中,第三个应该位于最右侧(flex-end)。

我尝试了几乎所有可能的组合,但没有运气。

.container {
  max-width: 80%;
  background-color: #222;
  height: 80px;
  margin: 50px auto;
  display: flex;
  flex-direction: row;
  align-items: center;
}
div:nth-of-type(1) {
  background-color: #666;
  flex-basis: 50px;
  height: 60px;
}
div:nth-of-type(2) {
  background-color: #fff;
  flex-basis: 150px;
  height: 60px;
}
div:nth-of-type(3) {
  background-color: #eee;
  flex-basis: 80px;
  height: 60px;
}
<section class="container">
  <div class></div>
  <div class></div>
  <div class></div>
</section>

【问题讨论】:

标签: css flexbox


【解决方案1】:

.container 上,您需要将justify-content 属性设置为space-between。此属性定义沿主轴的对齐方式。

.container{
  justify-content: space-between; 
}

更多信息在这里:https://css-tricks.com/almanac/properties/j/justify-content/

【讨论】:

  • 是的,但是如果第三个元素(在我的场景中)比前两个宽,那么第二个元素不会居中。当然,它现在向左倾斜:(
  • 我明白你的意思了——我会做一些测试,看看是否可以使用 flex。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2019-01-21
  • 2021-06-25
相关资源
最近更新 更多