Colspan/Rowspan用于显示设置为table-cell的元素

Mad*_*iha 104 html css

我有以下代码:

<div class="table">
    <div class="row">
        <div class="cell">Cell</div>
        <div class="cell">Cell</div>
    </div>
    <div class="row">
        <div class="cell colspan2">Cell</div>
    </div>
</div>

<style>
    .table {
        display: table;
    }
    .row {
        display: table-row;
    }
    .cell {
        display: table-cell;
    }
    .colspan2 {
        /* What to do here? */
    }
</style>
Run Code Online (Sandbox Code Playgroud)

非常直截了当.如何为元素添加colspan(或等效的colspan)display: table-cell

Rus*_*ser 49

据我所知,缺乏colspan/rowspan只是其中一个限制display:table.看这篇文章:

http://www.onenaught.com/posts/201/use-css-displaytable-for-layout


F-3*_*000 24

由于OP没有明确规定解决方案必须是纯CSS,我会顽固地投入我今天想到的解决方法,特别是因为它比在表格中有一个表格更优雅.

示例等于<table>,每行有两个单元格和两行,其中第二行中的单元格是td colspan="2".

我用Iceweasel 20,Firefox 23和IE 10对此进行了测试.

div.table {
  display: table;
  width: 100px;
  background-color: lightblue;
  border-collapse: collapse;
  border: 1px solid red;
}

div.row {
  display: table-row;
}

div.cell {
  display: table-cell;
  border: 1px solid red;
}

div.colspan,
div.colspan+div.cell {
  border: 0;
}

div.colspan>div {
  width: 1px;
}

div.colspan>div>div {
  position: relative;
  width: 99px;
  overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)
<div class="table">
    <div class="row">
        <div class="cell">cell 1</div>
        <div class="cell">cell 2</div>
    </div>
    <div class="row">
        <div class="cell colspan">
            <div><div>
                cell 3
            </div></div>
        </div>
        <div class="cell"></div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这里有实时动作(演示).

编辑:我认为代码更适合打印机,因为默认情况下它们会保留背景颜色.我还创建了rowspan-demo,灵感来自这里的最新答案.


Phi*_*e97 15

一个更简单的解决方案,适用于Chrome 30:

Colspan可以通过使用display: table而不是display: table-row行来模拟:

.table {
    display: block;
}
.row {
    display: table;
    width: 100%;
}
.cell {
    display: table-cell;
}
.row.colspan2 {/* You'll have to add the 'colspan2' class to the row, and remove the unused <div class=cell> inside it */
    display: block;
}
Run Code Online (Sandbox Code Playgroud)

唯一的缺陷是堆叠行的单元格不会垂直对齐,因为它们来自不同的表格.


sta*_*igh 8

如果你正在寻找一种直接的CSS方式来模拟colspan,你可以使用display: table-caption.

.table {
  display: table;
}
.row {
  display: table-row;
}
.cell {
  display: table-cell;
  border: 1px solid #000;
}
.colspan2 {
  /* What to do here? */
  display: table-caption;
}
Run Code Online (Sandbox Code Playgroud)
<div class="table">
    <div class="row">
        <div class="cell">Cell</div>
        <div class="cell">Cell</div>
    </div>
    <div class="row">
        <div class="cell colspan2">Cell</div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 使用 display: table-caption 将跨越所有列并将行放置在表格的顶部,就像 html 表格中的标题元素一样,所以这实际上不起作用。仅当您想跨越第一行或最后一行的所有列时。 (2认同)

Cur*_*urt 6

只需使用一个table.

表格只是在用于布局目的时不赞成.

这看起来像表格数据(数据的行/列).因此我建议使用table.

有关详细信息,请参阅此问题的答案:

使用div作为表创建相同的东西

  • 我不明白为什么CSSing的所有内容都像一个表格一样,在某种程度上比使用表更好.不可否认,`tr` /`td`垃圾邮件不是最漂亮的HTML,但这是唯一的原因吗?每个人都说桌子不是"意味着"用于格式化,但html本身并不"意味着"做我们认为今天的标准可接受的一半,包括Web应用程序. (13认同)
  • 问题是,它不是表格数据,所以我不想使用表格:) (11认同)
  • 差不多一年之后,但是 - 很轻微,除了"桌子不合时宜"之外,我与那些无缘无故地避开桌子的人分享你的烦恼.然而,我遇到了一些响应式设计,其中一个表不是一个表是有用的,所以它可以在较低的宽度重新排列.学习我没有divs的colspan有点令人失望. (4认同)

小智 5

这是我根据自己的情况使用的一种在 CSS 中跨列的方法。

https://jsfiddle.net/mb8npttu/

.table {
  display: table;
}

.row {
  display: table-row;
}

.cell {
  display: table-cell;
  border: 1px dotted red;
}

.colspan {
  max-width: 1px;
  overflow: visible;
}
Run Code Online (Sandbox Code Playgroud)
<div class='table'>
  <div class='row'>
    <div class='cell colspan'>
      spanning
    </div>
    <div class='cell'></div>
    <div class='cell'></div>
  </div>

  <div class='row'>
    <div class='cell'>1</div>
    <div class='cell'>2</div>
    <div class='cell'>3</div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 我必须用“white-space: nowrap;”来增强“.colspan”才能得到我想要的结果,但它效果很好。 (3认同)