【问题标题】:Flexible Flexbox Grid [closed]灵活的 Flexbox 网格 [关闭]
【发布时间】:2017-01-16 02:38:49
【问题描述】:

我正在寻找一种方法来制作这种动态 flexbox 网格:

项目数量均匀:标准 2 列网格

项目数量不均:最后一项应在右列使用两行

(例外:如果只有两个项目可用)

【问题讨论】:

  • 希望您至少尝试自己编写代码。 Stack Overflow 不是代码编写服务。我建议你做一些additional research,无论是通过谷歌还是通过搜索,尝试一下。如果您仍然遇到问题,请返回您的代码并解释您尝试过的方法以及为什么它不起作用。

标签: html css flexbox


【解决方案1】:

设法使用flexboxes 创建您的 2 列网格

查看以下代码:

$('input[type=button]').click(function() {
  $('.wrapper').append('<div></div>');
});
body {
  margin: 0;
}
* {
  box-sizing: border-box;
}
.wrapper {
  display: flex;
  flex-wrap: wrap;
}
.wrapper > div {
  border: 1px solid red;
  flex: 0 1 50%;
  height: 100px;
}
.wrapper > div:nth-last-child(2):nth-child(even) {
  height: 200px;
}
.wrapper > div:nth-last-child(2):nth-child(even) + div {
  margin-top: -100px;
}
input[type=button] {
  position: fixed;
  top: 20px;
  left: 20px;
}
.wrapper > div:nth-last-child(2):first-child {
  height: 200px;
}
.wrapper > div:nth-last-child(2):first-child + div {
  height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <div></div>
  <div></div>
</div>
<input type="button" value="Add Element" />

这实际上是一个hack,可以针对heightmargin 的框进行调整。

让我知道您对此的反馈。谢谢!

【讨论】:

  • @fabuchao 进行了一些编辑以调整 2 items 案例的高度...查看并告诉我您的想法...
  • 正是我想要的。谢谢你!完美运行
猜你喜欢
  • 2019-09-08
  • 1970-01-01
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多