html - flexbox 对齐内容不会在元素之间留下空间

标签 html css flexbox

我在这两个地方引用了关于flexbox align-content的 Material ,stack overflowcss-tricks .

我尝试放置三个不同颜色的水平条,高度为 100%,并且它们之间的间距相等。

对于 Flex 容器,我将 align-content 设置为 space- Between 。但所有条纹仍然紧密排列,之间没有任何间隙。

如何强制条纹下方的空白区域均匀分布在条纹之间和周围?

骗子代码是 here .

<body style="height:100%;">
  <div style="display:flex;height:100%;background-color:#cccccc;flex-direction:column;align-content:space-between;">
    <div style="height:100px;background-color:#222222;width:100%;">
    </div>
    <div style="height:100px;background-color:#777777; width:100%;">
    </div>
    <div style="height:100px;background-color:pink; width:100%;">
    </div>
  </div>
</body>

最佳答案

首先,您需要在 html 上设置 height: 100%,接下来您应该使用 justify-content: space- Between;

默认情况下,flex-directionrow主轴 为从左到右,因此 justify-content沿主轴或水平放置元素。但是,当您将 flex-direction 更改为 column 时,主轴现在更改为从上到下,并且 justify-content 仍沿主轴定位元素但现在是垂直的。

html {
  height: 100%;
}
<body style="height:100%;">
  <div style="display:flex;height:100%;background-color:#cccccc;flex-direction:column;justify-content:space-between;">
    <div style="height:100px;background-color:#222222;width:100%;">
    </div>
    <div style="height:100px;background-color:#777777; width:100%;">
    </div>
    <div style="height:100px;background-color:pink; width:100%;">
    </div>
  </div>

关于html - flexbox 对齐内容不会在元素之间留下空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39307572/

相关文章:

css - 在移动设备上折叠 flexbox 列

javascript - 使用 javascript 将自动生成的 HTML 中的字体大小从 px 更改为 em

javascript - onload 事件未触发 - JS

php - 如何使元素固定在 Bootstrap 中

jquery - 具有相同类的多个 div,所有这些都只有一个 jquery

html - Flexbox元素不滚动溢出

html - 使用 flexbox 的水平滚动 div 问题

html - 带有鼠标悬停的多 channel 音频HTML5

javascript - 尝试向字符串添加值,并且每次都会不断添加 1

javascript - 需要使 html 页面响应