I have the following code:
<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>
Pretty straightforward. How do I add a colspan (or the equivalent of colspan) for elements with display: table-cell
?
You can set the position of colspan content as "relative" and the row as "absolute" like this:
Use nested tables to nest column spans...
Or use 2 tables where the column span covers the whole row...
Simply use a
table
.table's are only frowned upon when being used for layout purposes.
This seems like tabular data (rows/columns of data). Therefore I would recommend using a
table
.See my answer to this question for more information:
creating the same thing with divs as tables
There is a solution to make the colspan the widht of the entire table. You can not use this technique to colspan a part of the table.
Code:
Explanation:
We use position absolute on the colspan to make it the full width of the table. The table itself needs position relative. We make use of a dummycell to maintain the height of the rows, position absolute does not follow the flow of the document.
Of course you can also use flexbox and grid to tackle this problem these days.
You can try this solution, where you can find how to apply colspan using div https://codepen.io/pkachhia/pen/JyWMxY
HTML:
CSS:
Here's one way to span columns in CSS I used for my own situation.
https://jsfiddle.net/mb8npttu/