How do I achieve colspan/rowspan behavior in tableless (e.g. div.table {display: table;} div.tr {display: table-row;} etc.
) table?

- 6,786
- 7
- 47
- 71
-
3Don't be frightened of using tables, they are designed to display data but not ideal for layouts. – nik0lai Jan 20 '11 at 10:43
-
3Put stripes on donkey and you'll get something that looks like a Zebra. But it's not a Zebra. Same way `div` that looks like a table is not a table and will never be. For stuff like `colspan` and `rowspan` simply use real table. – Shadow The GPT Wizard Jan 20 '11 at 10:48
-
1I think there are justifications for usingfor tabular data. How can you achieve drag/drop with– aw crud Aug 31 '11 at 15:15
elements? Sometimes this is a requirement. -
@Renderin: you can. Check out jquery ui drag and drop for instance. – artemave Sep 01 '11 at 21:24
-
Actually this is not really true. Today, there is no need to use real tables - not even for table-like data. A table-tag represents the data *always* in a fixed structure. With responsive design today for all kind of screen sizes, it is important to be able to reposition elements correctly. table-elements can't do this. CSS allows table-like data to be transformed to a non-table-like structure. Also, screen readers for disabled people still have problems with table-tags. Additionally, styling with `position` and `float` is a nightmare and CAN'T create 100% table-like structures (e. g. height). – StanE Nov 21 '16 at 14:54
-
1Possible duplicate of [Colspan/Rowspan for elements whose display is set to table-cell](https://stackoverflow.com/questions/9277661/colspan-rowspan-for-elements-whose-display-is-set-to-table-cell) – GSerg Aug 10 '20 at 10:14
11 Answers
I would imagine that this would be covered by CSS Tables, a specification which, while mentioned on the CSS homepage, appears to currently be at a state of "not yet published in any form"
In practical terms, you can't achieve this at present.

- 914,110
- 126
- 1,211
- 1,335
-
The visual effect is quite easy to achieve, you just don't need to set any kind of colspan. See Nick Zulu's answer below. You don't even need two different mock-tables. Two mock-rows in the same mock-table can contain different classes of mock-td. – Rumi P. Jul 29 '14 at 11:09
-
@mattblang It still has Work in Progress status. Here's a [draft specification](https://drafts.csswg.org/css-tables/) of CSS Table. – naXa stands with Ukraine Oct 18 '16 at 16:08
you can simply use two table divs, for instance:
<div style="display:table; width:450px; margin:0 auto; margin-top:30px; ">
<div style="display:table-row">
<div style="width:50%">element1</div>
<div style="width:50%">element2</div>
</div>
</div>
<div style="display:table; width:450px; margin:0 auto;">
<div style="display:table-row">
<div style="width:100%">element1</div>
</div>
</div>
works great!
-
1Not only does it work, you don't even have to make two mock-tables. It goes all in the same if that's what you want. – Rumi P. Jul 29 '14 at 11:11
-
20This only seems to work if all divs have explicit widths set, which kind of defeats the purpose of `display:table`. – Jannik Jochem Apr 23 '15 at 09:23
-
So basically, you've turned all your <table>
, <tr>
and <td>
elements into <div>
elements, and styled them to work exactly like the original table elements they've replaced?
What's the point in that?
It sounds like someone's told you that you shouldn't be using tables in modern web design, which is sort of right, but not in this way -- what you've done doesn't actually change anything about your code. It certainly hasn't got rid of the table; it's just made it harder to read.
The true meaning of the point about not using tables in modern sites is to achieve the page layout you want without using the kind of layout techniques that involve setting out a grid of table cells.
This is achieved by using position
styles and float
styles, and a number of others, but certainly not display:table-cell;
etc. All of this can be achieved without ever needing colspans or rowspans.
On the other hand, if you are trying to place an actual block of tabular data on the page - for instance a list of items and prices in a shopping basket, or a set of statistics, etc, then a table is still the correct solution. Tables were not removed from HTML, because they are still relevant and still useful. The point is that it is fine to use them, but only in places where you are actually display a table of data.
The short answer to your question is I don't think you can -- colspan
and rowspan
are specific to tables. If you want to carry on using them, you will need to use tables.
If your page layout is such that it relies on tables, there really isn't any point doing a half-way house effort to get rid of the table elements without reworking how the layout is done. It doesn't achieve anything.
Hope that helps.

- 166,037
- 39
- 233
- 307
-
17The point in that is that the semantics of an element and the way it renders are different. If the CSS specification had reached a stage where it expressed everything relating to presentation that was implied by table markup, and browsers had caught up, then it would be an excellent way to get the benefits of table layouts (resizing elements that stick together along specified edges) without the semantic disadvantages (the telling out outright lies about the meaning of the data). – Quentin Jan 20 '11 at 19:15
-
4@Spudley - Even though in most cases using
is better for tabular presentation, there are some cases I've encountered where the CSS way seems more appropriate. For example - if you have multiple, but similar looking forms that you want to display in a nice tabular manner there is no way to achieve that using the tables alone because only one form can be wrapped around one table. You could have multiple tables - one for each form, but they wouldn't look as neat without some CSS adjusting, and the CSS/DIV way is actually a better way to go in those cases.
– techexpert May 21 '12 at 05:29 -
Addionally, unlike "HTML tables", "CSS tables" may have "implied" elements. That is, you can just add a single `display: table-cell;` element without surrounding rows and tables, and it will be well-formed. – WGH Oct 02 '13 at 11:03
-
I totaly agree with this answer but sadly I believe there are few layouts you can't achive without using a table. Maybe I am wrong (I am not HTML/CSS expert) but sometimes it is realy hard to get EXACTLY the layout I have in my mind. For example you can read my answer here > http://stackoverflow.com/questions/20018742/make-navigation-bar-take-up-entire-page-height-in-css/26994674#26994674. You will think you can set that layout as described using 100% height OR fixed position OR absolute position+top:0;bottom:0; but try it and I think it is maybe impossible without tables (or display: table) – Srneczek Nov 18 '14 at 13:39
-
I loved tables. Unfortunately, Google's RWD mandate has defeated their practicality (in favor of the new necessity of web sites that can respond dynamically to arbitrary screen sizes). Therefore, I am (of necessity) phasing tables out of my web design in favor ofs, which can be displayed differently by loading screensize-appropriate stylesheets. So I'm currently working on the perfect reason to need this "colspan=2" attribute on a– Deborah Cole Aug 02 '15 at 20:35tag, and the lack thereof is quite frustrating!
-
for me, my `
`, `
– Tcll Aug 05 '15 at 16:02`, ` ` elements come pre-styled and I can't disable cell padding and spacing without some major JS hacking, so this makes a great workaround. -
Actually this is not really true. Today, there is no need to use real tables - not even for table-like data. A table-tag represents the data *always* in a fixed structure. With responsive design today for all kind of screen sizes, it is important to be able to reposition elements correctly. table-elements can't do this. CSS allows table-like data to be transformed to a non-table-like structure. Also, screen readers for disabled people still have problems with table-tags. Additionally, styling with `position` and `float` is a nightmare and CAN'T create 100% table-like structures (e. g. height). – StanE Nov 21 '16 at 14:53
-
@user25163 Just to be clear, this answer was written nearly 6 yrs ago. The web has changed beyond recognition in that time; be careful about taking web dev advice from posts this old. That said, the answer does represent the ideal position: The whole point of CSS & semantic HTML is to allow us to use tags that represent what they contain. So ` – Spudley Nov 21 '16 at 21:30
-
@Spudley Ok, you are right about the date of your answer (I didn't saw this), but your answer now regarding tables doesn't change the fact, that information in `
` elements cannot be wrapped for responsive design, which makes `
– StanE Nov 22 '16 at 13:47` elements really bad. CSS is meant for visual representation (changing) of HTML, but it can't do it for `
` elements for some aspects. The `
` element, unfortunately, also defines the visual structure of the data. And like others said, it limits interaction / modification through JS. So I (personally) think that real tables are still unnecessary
-
The point is when I use dompdf with rowspaned content the css went wrong in a page break for those rowspan row – Trisno Raynaldy Jan 19 '21 at 05:08
You could always use CSS to simply adjust the width and the height of those elements that you want to do a colspan
and rowspan
and then simply omit displaying the overlapped DIVs. For example:
<div class = 'td colspan3 rowspan5'> Some data </div>
<style>
.td
{
display: table-cell;
}
.colspan3
{
width: 300px; /*3 times the standard cell width of 100px - colspan3 */
}
.rowspan5
{
height: 500px; /* 5 times the standard height of a cell - rowspan5 */
}
</style>

- 2,444
- 3
- 20
- 21
Trying to think in tableless design does not mean that you can not use tables :)
It is only that you can think of it that tabular data can be presented in a table, and that other elements (mostly div's) are used to create the layout of the page.
So I should say that you have to read some information on styling with div-elements, or use this page as a good example page!
Good luck ;)

- 1,244
- 1
- 12
- 24
In order to get "colspan" functionality out of div based tabular layout, you need to abandon the use of the display:table | display:row styles. Especially in cases where each data item spans more than one row and you need different sized "cells" in each row.

- 3,250
- 1
- 30
- 46
<div style="clear:both;"></div>
- may do the trick in some cases; not a "colspan" but may help achieve what you are looking for...
<div id="table">
<div class="table_row">
<div class="table_cell1"></div>
<div class="table_cell2"></div>
<div class="table_cell3"></div>
</div>
<div class="table_row">
<div class="table_cell1"></div>
<div class="table_cell2"></div>
<div class="table_cell3"></div>
</div>
<!-- clear:both will clear any float direction to default, and
prevent the previously defined floats from affecting other elements -->
<div style="clear:both;"></div>
<div class="table_row">
<!-- the float is cleared, you could have 4 divs (columns) or
just one with 100% width -->
<div class="table_cell123"></div>
</div>
</div>

- 19
- 2
-
4The "colspan" row causes the first cell above it to expand. http://jsfiddle.net/EGx2H/159/ – allicarn Feb 01 '13 at 15:08
I've achieved this by separating them in different , e.g.:
<div class="table">
<div class="row">
<div class="col">TD</div>
<div class="col">TD</div>
<div class="col">TD</div>
<div class="col">TD</div>
<div class="col">TD</div>
</div>
</div>
<div class="table">
<div class="row">
<div class="col">TD</div>
</div>
</div>
or you can define different classes for each tables
<div class="table2">
<div class="row2">
<div class="col2">TD</div>
</div>
</div>
From the user point of view they behave identically.
Granted it doesn't solve all colspan/rowspan problems but it does answer my need of the time.

- 19
- 1
you just use the :nth-child(num) in css for add the colspan like
<style>
div.table
{
display:table;
width:100%;
}
div.table-row
{
display:table-row;
}
div.table-td
{
display:table-cell;
}
div.table-row:nth-child(1)
{
display:block;
}
</style>
<div class="table>
<div class="table-row">test</div>
<div class="table-row">
<div class="table-td">data1</div>
<div class="table-td">data2</div>
</div>
</div>

- 1,901
- 20
- 23
You can do this ( where data-x has the appropriate display:xxxx set ):
<!-- TH -->
<div data-tr>
<div data-th style="width:25%">TH</div>
<div data-th style="width:50%">
<div data-table style="width:100%">
<div data-tr>
<div data-th style="width:25%">TH</div>
<div data-th style="width:25%">TH</div>
<div data-th style="width:25%">TH</div>
<div data-th style="width:25%">TH</div>
</div>
</div>
</div>
<div data-th style="width:25%">TH</div>
</div>
<!-- TD -->
<div data-tr>
<div data-td style="width:25%">TD</div>
<div data-th style="width:50%">
<div data-table style="width:100%">
<div data-tr>
<div data-td style="width:25%">TD</div>
<div data-td style="width:25%">TD</div>
<div data-td style="width:25%">TD</div>
<div data-td style="width:25%">TD</div>
</div>
<div data-tr>
...
</div>
</div>
</div>
<div data-td style="width:25%">TD</div>
</div>

- 1,260
- 11
- 10
-
How can data-td have an appropriate display set? this doesn't match any usage of html that I've ever seen, are you sure there isn't a javascript library involved? – Ryan Leach Feb 27 '17 at 04:46
-
looks a lot like some tool usage to me, such as bootstrap or angular etc. – Tuncay Göncüoğlu Aug 08 '19 at 13:49