css - 为什么这些 flex 元素以这种方式间隔?

标签 css flexbox

<分区>

我做了一个简单的 flexbox jsfiddle玩弄所有的 flexbox 值,但偶然发现了一些我无法解释的事情高度,我不完全确定为什么会这样?

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: flex-start(在 .grid 上设置)会导致此类行为。如 MDN docs 中所述

The CSS align-items property defines how the browser distributes space between and around flex items along the cross-axis of their container.

如果禁用它,该值将默认设置为stretch(每个 flex 元素将被拉伸(stretch)以填充容器)。

关于css - 为什么这些 flex 元素以这种方式间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42862002/

相关文章:

jquery - 在移动浏览器中的 slider 上无法通过点击和滚动滚动网页

jquery - Twitter-Bootstrap 响应式导航栏下拉子链接在平板电脑上无法点击

css - 嵌套的 flexboxes(在两个维度上)

html - 是否可以组合通用样式并删除重复

css - 使用 CSS 清除修复和展开边距而没有副作用?

php - “flex”CSS 在 Woocommerce 中被覆盖

jquery - 对制作移动友好的导航有点困惑

html - 将元素粘贴到固定元素的底部

css - 这种发光效果可能吗?

html - 显示 body 的 flex