CSS Flex 两列布局问题

标签 css flexbox

<分区>

如何对齐我的元素以始终将其放在顶部。我正在对我的容器使用 flex。

注意:元素数量是动态生成的。

enter image description here

<div class="container">
  <div class="item">ITEM 1</div>
  <div class="item second">ITEM 2</div>
  <div class="item">ITEM 3</div>
</div>

.container {
    display: flex;
    flex-wrap: wrap;
    flex-direction: row;
 }

 .item {
     width: 45%;
     height: 100px;
     background-color: lightgray;
     margin: 0 20px 20px 0;
     display: flex;
     justify-content: center;
     align-items: center;
 }

 .second {
     height: 150px;
 }

这是我的 plunkr

最佳答案

最简单的方法是将 item-1item-3(同一列的元素)放在一个 div 上,然后应用属性到那个 div 标签。我在下面添加了代码片段。

.container {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
}

.item {
  width: 100%;
  height: 100px;
  background-color: lightgray;
  margin: 0 20px 20px 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.second {
  height: 150px;
}

.column{
  width:45%;
}
.clm-1{
  margin: 0 20px 20px 0;
}
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>
    <div class="container">
    <div class="column clm-1">
      <div class="item">ITEM 1</div>
      <div class="item">ITEM 3</div>
     </div>
     <div class="column clm-2">
      <div class="item second">ITEM 2</div>
     </div>
    </div>
  </body>

</html>

关于CSS Flex 两列布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55235923/

上一篇:css - 如何在 ionic framework v4 和 Angular 7 中调整图标大小

下一篇:jquery - 如何使新创建的元素与现有元素具有相同的功能?

相关文章:

css - 仅使用 CSS 在悬停 div 时更改 body bgcolor

html - 如何使文本输入不可编辑?

css - Internet Explorer 向 flex box 添加了额外的填充。怎么修?

css - 如何使 Flex 中的父容器和其他子容器成长为最长的子容器之一

html - 结果显示在两行?

html - 在隐藏内容左右的空白空间(包装器的边距)之前阻止包装器中的内容收缩

css - css 下拉菜单的 z-index 问题

html - 允许在一个 div 上水平滚动,但不允许在整个 body 上水平滚动

html - ionic 输入的 Ionic 3 键盘问题

javascript - 扩展 Accordion 列表项以保留在 flex 列中