【问题标题】:How to use Flex CSS and simulate a rowspan 2 and colspan 2如何使用 Flex CSS 并模拟 rowspan 2 和 colspan 2
【发布时间】:2016-08-30 13:24:16
【问题描述】:

我想使用 div 构建一个 2 行的响应式布局。

我试试这个 jsbin:http://jsbin.com/jiyanayayi/edit?html,css,output

第一行将有三个单元格,最后一个(单元格 3)必须有一个rowspan = 2

具有colspan = 2 的第二行(单元格4)必须由单元格3 限制。

我尝试了下面的 CSS,但 rowspan 属性不起作用。

如何创建这种响应式布局格式?

.row{
  display: flex;
}
.cell{
  flex: 1;
  background:#eee;
  border: 1px solid #444;
  padding: 15px;
}

HTML:

<div class="table">
    <div class="row">
        <div class="cell">Cell 1 </div>
        <div class="cell">Cell 2 </div>
        <div class="cell rowspan2">Cell 3 </div>
    </div>
    <div class="row">
        <div class="cell colspan2">Cell 4</div>
    </div>
</div>

【问题讨论】:

  • 这应该是什么样子?

标签: html css flexbox


【解决方案1】:

Edit 2021@supports 查询中使用grid CSS 部分,除非您仍然需要支持一些异国情调的浏览器或非常旧的浏览器。如果您不确定,您可以在您想要支持的浏览器中使用jsbin sample(也链接在该答案的末尾)。 - jsbin 应该可以使用旧浏览器进行实时编辑,至少它可以使用 IE11。

grid 放置选项是grid-areagrid-column and grid-row两个链接都是关于 Grid Layout Module Level 2 来自 W3C)。下面的代码使用了grid-area,你会发现grid-columngrid-row的语法被注释掉了。

不是table-layout,这里的问题/答案不是关于 HTML 表格。

结束编辑


您还需要使用flexflex-wrap

.table {
  display: flex;
  border: solid;
}

.row {
  flex: 1;
  display: flex;
}

.row:first-of-type {
  flex-flow: wrap;
}

.row .rowspan2 {
  flex: 1 1 100%;
}

.row div {
  border: solid;
  padding: 1em;
  flex: 1;
}

/* ============================================================== */
/* if display grid and contents is supported then you may use it  */
/* ============================================================== */
/*
@supports (display:grid) and (display:contents) {
  .table {
    display: grid;
    grid-template-columns: 25% 25% 25% 25%;
    grid-template-areas: "cell1 cell2 cell3 cell3" "cell4 cell4 cell3 cell3";
    border: solid;
  }
  .row {
    display: contents/* hide them in the structure. .row respective children become sibblings *//*
  }
  .row:first-child> :first-child {
    grid-area: cell1;
  }
  .row:first-child div {
    grid-area: cell2;
  }
  .row .cell.rowspan2 {
    grid-area: cell3;
    /*grid-row:span 2; no need if grid-template-area is complete*//*
  }
  div div {
    border: solid;
    padding: 1em;
  }
  .colspan2 {
    grid-area: cell4;
    /*grid-column : span 2; no need if grid-template-area is complete*//*
  }
}
*/
<div class="table">
  <div class="row">
    <div class="cell">Cell 1</div>
    <div class="cell">Cell 2</div>
    <div class="cell rowspan2">Cell 3</div>
  </div>
  <div class="row">
    <div class="cell colspan2">Cell 4</div>
  </div>
</div>

jsbin 更新为 @supports 未注释:https://jsbin.com/wexisiyamo/1/edit?html,css,output

【讨论】:

  • @ÂngeloRigo 我建议你去阅读一些关于 flexbox 的教程,并从中获得一些乐趣,以充分了解你可以用它做什么;)(csstricks 和 codepen 应该让这变得简单)跨度>
  • 我看到从单元格 3 到顶部有很多空白。尝试 margin-top 没有用。任何想法让元素更接近单元格 3 的顶部?避免 cell3 和 cell1 和 cell 2 之间有太多空白?
  • @ÂngeloRigo 不太清楚我明白你的意思,这些单元格中只有填充删除它:)
  • 这个版本似乎导致细胞 3 和 4 的位置被交换,而不是问题中描述的。 .colspan2 用于标记,但不用于样式...
  • @emyr HTML 结构未受影响,CSS 网格支持太少,尚未提出,但仍然可以使用 display:grid 更新 sn-p 以适应 100% 的问题;)@987654325 @ (display:contents involved to keep structure untouched Experiment)
猜你喜欢
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
  • 2015-02-02
  • 2023-03-17
相关资源
最近更新 更多