css - Flexbox CSS - 如何以更简单的方式实现网格布局?

标签 css flexbox

目前只能使用嵌套的 display: flex 容器来做到这一点。 有没有一种方法可以用一个 flex 容器实现这种布局,并且当屏幕调整大小时,将元素堆叠在一列中而不是“仅仅”缩小它们?

.container {
  width: 100%;
  display: flex;
  flex-flow: column wrap;
}

.row {
  display: flex;
  flex-wrap: wrap;
  flex: 1 100%;
  max-height: 100px;
}

.row>* {
  border: dashed 1px red;
  margin: 10px;
}

.bc,
.act {
  flex: 1 auto;
}

.summary {
  flex: 3 auto;
}

.short {
  flex: 1 0;
}

.widget {
  border: dashed 1px red;
  margin: 10px;
  max-height: 100px;
  flex: 3 100%;
}
<div class="container">
  <div class="row">
    <div class="bc">50%</div>
    <div class="act">50%</div>
  </div>
  <div class="row">
    <div class="summary">75%</div>
    <div class="short">25%</div>
  </div>
  <div class="widget">100%</div>
  <div class="widget">100%</div>
</div>

最佳答案

通过使用 flex wrap 并将方向更改为行和合适的宽度,您可以使用一个容器。要使它们在较小的屏幕尺寸上成为一列,只需添加一个使 div 全部为 100% 宽度的媒体查询

* {
  box-sizing: border-box;
}

.container {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}
.container>div {
  border: 1px dashed red;
  margin:10px;
}
.quarter {
  width:calc(25% - 20px);
}
.half {
  width:calc(50% - 20px);
}
.three-quarters {
  width:calc(75% - 20px);
}
.full {
  width:calc(100% - 20px)
}
<div class="container">
  <div class="half">50%</div>
  <div class="half">50%</div>
  <div class="three-quarters">75%</div>
  <div class="quarter">25%</div>
  <div class="full">100%</div>
  <div class="full">100%</div>
</div>

关于css - Flexbox CSS - 如何以更简单的方式实现网格布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51856064/

相关文章:

css - 如何通过单击其子项(angular2/ts)将样式应用于 div 元素

javascript - 控制台中的混合内容与 PrestaShop 网站

jquery - 来自一个 div 的特定元素的不同 jquery 效果

html - 如何删除我的html页面两边的白边

html - 使未知数量的元素列表拉伸(stretch)到容器的整个宽度,响应

jquery - 背景没变

css - 我的 CSS 菜单在 Wordpress 中出现 Overflow-X

html - 使用 flexbox,我可以有 1 个固定宽度的列和 1 个占据其余宽度的列吗?

html - Flexbox适合 child 的宽度

angular - 使用 flex 布局将 div 居中到页面 - Angular Material 2