【问题标题】:Why are these flex items spaced in this way? [duplicate]为什么这些弹性项目以这种方式间隔开? [复制]
【发布时间】:2017-08-09 06:36:58
【问题描述】:

我做了一个简单的 flexbox jsfiddle 来玩弄所有的 flexbox 值,但偶然发现了一些我无法解释的东西,我的 .item div 出于某种原因被隔开,.grid 自动拉伸到完全身高,我不完全确定为什么会这样?

HTML

<div class="container">
  <div class="grid">
    <div class="item red">a</div>
    <div class="item yellow">b</div>
    <div class="item blue">c</div>
  </div>
</div>

CSS

.container {
  width: 320px;
  height: 480px;
  background: black;
  padding:15px;
  margin: 20px auto;
  display: flex;
}

.grid {
  background: white;
  display: flex;
  width: 100%;
  justify-content: flex-start;
  align-items: flex-start;
  flex-direction: row;
  flex-wrap: wrap;
}

.item {
  width: 100%;
  flex-grow: 0;
  flex-basis: 100%;
  align-self: auto;
  align-items: flex-start;
  padding: 10px 0;
}

.red { background: red; }
.yellow { background: yellow; }
.blue { background: blue; }

【问题讨论】:

  • 了解align-items 属性。

标签: css flexbox


【解决方案1】:

align-items: flex-start(设置在.grid)会导致这种类型的行为。在MDN docs中指定

CSS align-items 属性定义了浏览器如何沿容器的横轴在弹性项目之间和周围分配空间。

如果禁用它,默认值将设置为stretch(每个弹性项目将被拉伸以填充容器)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    相关资源
    最近更新 更多