css - 包裹的 flex 行元素从顶部开始

标签 css flexbox

<分区>

同时尝试回答 this question ,我试图想出一个灵活的解决方案。我能得到的最接近的是:

.container {
  height: 500px;
  width: 500px;
  background-color: red;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content:space-between;  /* puts spacing between left and right column */
}


.headerTitle {
  width:100%;
  height: 24px;
  margin: 24px 24px 0;
  padding: 0;
  line-height: 24px;
}

.sectionClass {
  width: 249px;
  height: 200px;
  background-color: yellow;
}

.rightSideDiv {
  width: 249px;
  height: 200px;
  border: 4px solid green;
  box-sizing:border-box;     /* need this otherwise border  will take an extra 8px width in some browsers */

}
<aside>
  <div class="container">
    <header class="headerTitle"> Header Title </header>
    <section class="sectionClass"> . </section>
    <div class="rightSideDiv"> </div>
  </div>
</aside>

但是,我无法让下方的 2 个框开始与顶部标题齐平。有没有办法使用 flex 来做到这一点?我尝试了 align-itemsalign-self 但这似乎没有做任何事情。

我还尝试使用 flex-grow:1; 向容器添加一个伪元素,但它没有以所需的方式增长。

看看 flex 是否可以处理这个会很有趣,因为我仍在努力学习它的复杂性

最佳答案

只需将 align-content: flex-start 添加到 .container div:

.container {
  height: 500px;
  width: 500px;
  background-color: red;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content:space-between; /* puts spacing between left and right column */
  align-content: flex-start;
}

.headerTitle {
  width:100%;
  height: 24px;
  margin: 24px 24px 0;
  padding: 0;
  line-height: 24px;
}

.sectionClass {
  width: 249px;
  height: 200px;
  background-color: yellow;
}

.rightSideDiv {
  width: 249px;
  height: 200px;
  border: 4px solid green;
  box-sizing:border-box; /* need this otherwise border will take an extra 8px width in some browsers */
}
<aside>
  <div class="container">
    <header class="headerTitle"> Header Title </header>
    <section class="sectionClass"> . </section>
    <div class="rightSideDiv"> </div>
  </div>
</aside>

关于css - 包裹的 flex 行元素从顶部开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48868699/

上一篇:javascript - 如何使div网格相互忽略

下一篇:css - 如何在背景图像过渡中滑动?

相关文章:

html - 具有 flexbox 属性的布局

html - Bootstrap : How to center image in panel heading?

javascript - jQuery 缓慢更改 css 属性

html - 动态内容高度

html - 如何垂直对齐具有不同高度的网格元素?

css - 在 flex 元素上显示表格

css - 如何在 Extjs 4.1.3 中更改一行网格单元格的 css

php - 样式化动态内容

html - 以 flexbox 或 float 包裹其余部分之前的最后一个 div 为目标

html - Flexbox 列将元素对齐为相同宽度但居中,无需包装(仅限 css)