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
?
CSS
HTML
Code
It can be done just with pure CSS and centering the text across the "fake" colspan.
The trick is to set the rows to
position:relative
, then to place "empty divs" in therow
where you want to make thecolspan
(they must haveheight
in order to work), set thecell
where the content is in asdisplay:grid
, and finally, applyingposition:absolute
to the element inside the cell (and center it as you may center any other absolute element).Even if this is an old question, I would like to share my solution to this problem.
Here is a fiddle. It's not really a span, and the solution is a bit hacky, but it is usefull in some situations. Tested on Chrome 46, Firefox 31 and IE 11.
In my case, I had to present some non-tabular data in a tabular way, keeping the width of the columns and giving title to sub-sections of the data.
As far as I know, the lack of colspan/rowspan is just one of the limitations of
display:table
. See this post:http://www.onenaught.com/posts/201/use-css-displaytable-for-layout
If you're looking for a straight CSS way to simulate a colspan, you could use
display: table-caption
.You can't achieve this at present.
AFAIK this would be covered by CSS Tables, a specification which appears to currently be at "work in progress" state.