294

In modern web development I'm coming across this pattern ever more often. It looks like this:

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

And in CSS there is something like:

.table { display: table; }
.row { display: table-row; }
.cell { display: table-cell; }

* (Class name are illustrative only; in real life those are normal class names reflecting what the element is about)

I even recently tried doing this myself because... you know, everyone's doing it.

But I still don't get it. Why are we doing this? If you need a table, then just make a blasted <table> and be done with it. Yes, even if it's for layout. That's what tables are for - laying out stuff in tabular fashion.

The best explanation that I have is that by now everyone has heard the mantra of "don't use tables for layout", so they follow it blindly. But they still need a table for layout (because nothing else has the expanding capabilities of a table), so they make a <div> (because it's not a table!) and then add CSS that makes it a table anyway.

For all the world this looks to me like putting arbitrary unnecessary obstacles in your way and then doing extra work to circumvent them.

The original argument for moving away from tables for layout was that it's hard to modify a tabular layout afterwards. But modifying a "faux-table" layout is just as hard, and for the same reasons. In fact, in practice modifying a layout is always hard, and it's almost never enough to just change the CSS, if you want to do something more serious than minor tweaks. You will need to understand and change HTML structure for serious design changes. And tables don't make the job any harder or easier than divs.

In fact, the only way I see that tables could make a layout difficult to modify, is if you abused them and created an ungodly mess. You can do that with divs too.

So... in an attempt to change this from a rant into a coherent question: what am I missing? What are the actual benefits of using a "faux-table" over a real one?

About the duplicate link: This isn't a suggestion to use another tag or something. This is a question about using a <table> vs display:table.

Panzercrisis
  • 3,145
  • 4
  • 19
  • 34
Vilx-
  • 5,320
  • 4
  • 20
  • 24
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackexchange.com/rooms/22479/discussion-on-question-by-vilx-why-are-people-making-tables-with-divs). –  Apr 01 '15 at 00:13
  • 2
    Back in the day (**cough** Int... **cough** Expl... **cough**), tables were *ridiculously* slow to render vs. pure CSS tables. You would literally wait for many seconds to see anything at all on-screen with actual `` being used. With CSS, there was no such problem.
    – Juha Untinen Apr 01 '15 at 09:59
  • 8
    @JuhaUntinen: that... sounds unlikely. Source? – Paul D. Waite Apr 01 '15 at 13:35
  • 5
    @PaulD.Waite - It's actually still true today, I think. Read the other answers. Unless the table has `table-layout:fixed`, it will need to load entirely before it can be displayed. With modern broadband this effect is simply less noticeable. – Vilx- Apr 01 '15 at 15:48
  • 5
    @JuhaUntinen: That's not entirely accurate. The table rendering engine was slower when you used percentages for column widths as the entire table had to be downloaded before the browser could start figuring out how big to make everything. This was over complicated by nested tables. However, there were then (and still exist) ways to make table rendering FAR FAR faster. Just very few devs bother to learn their craft well enough and instead jump on bandwagons. – NotMe Apr 01 '15 at 15:58
  • @Vilx-: what answers in particular are you talking about? I can’t see one that mentions a difference in layout performance in IE between using actual tables, and `display:table`. (Regarding the entire table needing to be loaded before it can be displayed, I don’t see how IE would be slower than any other browser at doing that.) – Paul D. Waite Apr 01 '15 at 16:16
  • 1
    This question makes me sad to think about all of the human productivity that has been wasted on this issue in the past 15-20 years. – whatsisname Apr 01 '15 at 16:32
  • 5
    Back in the even older days we used to mimic divs with tables so there. – Wyatt Barnett Apr 01 '15 at 17:57
  • 1
    @WyattBarnett: and also the glorious `frame` :) those were some innocent days... – Juha Untinen Apr 02 '15 at 11:14
  • There are a couple other reasons you should avoid tables for non-table content: (1) your HTML should have the highest semantic value possible and (2) tables add a lot of extra markup for cases where the data can be formatted sufficiently without a table. – Sildoreth Apr 03 '15 at 16:12
  • @Sildoreth - the extra markup is minimal and IMHO actually clarifies what is going on. As for the highest semantic value - why? – Vilx- Apr 03 '15 at 23:02
  • 4
    Somebody read they weren't supposed to use tables for layout and took it to mean they weren't supposed to use tables at all. I detest this trend. – Casey Apr 04 '15 at 18:45
  • 1
    "Back in the day" is especially not true when talking about the tables. Netscape 7.0 is still the fastest table renderer, period. Netscape will render a large table in a split second, while the current FF, Opera or IE will be hung for a long time. I call this phenomenon "software obesity epidemic". – ajeh Mar 30 '16 at 19:28
  • @ajeh - But how **correctly** will it do it? It's not hard to render a table fast if you start ignoring some of the constraints the developer placed on it. – Vilx- Mar 30 '16 at 22:24
  • @Vilx- Can you test and see? – ajeh Mar 31 '16 at 21:30
  • @ajeh - Maybe, but I'm a bit lazy to check such an antiquated browser. :) Also, I'm not even sure what standards I should try and hold it to. If you're comparing to the contemporary FF/IE/Chrome, then it's obviously not going to support a LOT of the features that these browsers have, so it wouldn't be a fair comparison. In fact, I'm not even sure it's possible to fairly compare them. Even though the test might not include the newer features, the modern browsers will have code to support them and that might slow them down. Yet it's good that they have these features, even with the slowdown. – Vilx- Mar 31 '16 at 22:32
  • You are totally missing the point. Netscape 7 is ways faster than modern FF on the same bare table with many rows. Keeping in mind that modern FF uses hardware acceleration that Netscape is not even aware of. – ajeh Apr 01 '16 at 16:23
  • @ajeh - You're missing the point too. Sure, it's faster, I've no doubt about that. But it's because it's a lot simpler and doesn't support all the same stuff that FF does. – Vilx- Apr 01 '16 at 18:00
  • @ajeh - Wait, what are we debating about again? :D – Vilx- Apr 01 '16 at 18:05
  • Surprised to see that no one answered with a simple: It could be tabular data but it's quite hard if not impossible to fix the table header and make the rows scrollable, whereas ith divs this is not so difficult. – iharob Nov 10 '19 at 05:40
  • This UI library made me wonder this question and search for an answer: https://material-ui.com/components/tables/#data-table They made a data-table out of divs and it confused me. They claim that "The Table component has a close mapping to the native elements. This constraint makes building rich data tables challenging." I think we should ask them why exactly.
    – ADJenks Apr 22 '21 at 17:04

8 Answers8

161

The question is if the data is semantically a table (i.e. a set of data or units of information logically organized in two dimensions) or you just use the grid for visual layout, e.g. because you want a sidebar to expand or something like that.

If your information is semantically a table, you use a <table>-tag. If you just want a grid for layout purposes, you use some other appropriate html elements and use display:table in the style sheet.

When is data semantically a table? When the data is logically organized along two axes. If it makes sense with headers for the rows or columns, then you might have a semantic table. An example of something which is not semantically a table is presenting a text in columns like in a newspaper. This is not semantically a table, since you would still read it linearly, and no meaning would be lost if the presentation was removed.


OK, why not use <table> for everything rather than only for something that is semantically a table? Visually it is obviously the same (since the table element just have display:element in the default style sheet).

The difference is that the semantic information can help alternative user agents. For example a screen reader might allow you to navigate in two dimensions in the table, and read the headers for a cell for both axes if you forget where you are. This would just be confusing if the table was not semantically a table but just used for visual layout.

The <table> versus display:table discussion is just a case of the more general principle of using semantic markup. See for example: Why would one bother marking up properly and semantically? or Why is semantic markup given more weight for search engines?

In some places you might actually be legally required to use semantic markup for accessibility reasons, and in any case there is no reason to purposefully make your page less accessible.

Even if you don't care for disabled users, having presentation separate from content gives you benefits. E.g. your three column layout could be presented in a single columns on a mobile using an alternative stylesheet.

JacquesB
  • 57,310
  • 21
  • 127
  • 176
  • 1
    OK, but **why** not use a `` for layout? It has properties that nothing else has. If you're trying to make a good responsive/fluid layout, a `
    ` (or `display: table`) is often your only choice. So why must you jump through hoops and use `display:table` when a simple `
    ` will do and be much more legible on top of it?
    – Vilx- Mar 30 '15 at 18:27
  • 14
    @Vilx- why use `` and `` rather than `` and ``? Because `` and `` are not about the semantics of the tag. `` has *semantic* meaning. Using it for something that *isn't* a table makes it harder to understand the semantic meaning of the document.
    –  Mar 30 '15 at 18:54
  • 2
    @MichaelT - another thing I've never understood. I just use `` and ``. But nobody cares about that. As for semantics - the only case that I'm aware of where semantics matter are search engine spiders. However things like ``, `` and `` are quite unlikely to confuse those.
    – Vilx- Mar 30 '15 at 19:14
  • 23
    @Vilx- `The book Peoplesoft is the best book on the subject.` Putting `` around best would be wrong because that means something different than the `` around the book title. For more, see [Why are the `` and `` tags deprecated?](http://programmers.stackexchange.com/q/255366/40980) and [What's the difference between `` and ``, `` and ``?](http://stackoverflow.com/q/271743/289086) –  Mar 30 '15 at 19:18
  • 1
    @MichaelT - I still don't get it. Why would I care what my content _means_? I care only about 2 things: the experience of my users (which includes visual, aural and search engine experience), and the experience of my fellow developers. Now, granted, I've never made a website which was usable by screen readers, so I don't know if using tables over divs has any negative difference there (and if it can be overcome by the proper aria roles or something). However I'm pretty sure that using divs over tables has neutral to negative consequences on all other points. – Vilx- Mar 30 '15 at 21:26
  • 1
    @MichaelT - It's an especially evil thing do to your fellow developers who now need to do the mental math and bookkeeping to keep track of which element is a row, which is a cell, and where "invisible" elements are automatically inserted as per CSS rules. At least a `` lets you spell it all out.
    – Vilx- Mar 30 '15 at 21:30
  • 4
    @Vilx- of course, that means you've never done `
    text
    ` and other joys of nested tables for formatting. Because the mental math of which table you are in isn't hard either. That said, I really try to avoid using all of that and would much rather use something like bootstrap.
    –  Mar 30 '15 at 21:35
  • 2
    @MichaelT - Yes, I have, but (a) - such cases are pretty rare (remember - I'm not advocating using tables for EVERYTHING. I'm arguing against `display: table`); and (b) - it would look just as bad if you replaced all the table/tr/td with div. – Vilx- Mar 30 '15 at 21:39
  • 19
    If you don't care what your content means, I strongly suggest you stop using any elements except for forms and divs. Just add classes to apply styling and behavior. – Rich Remer Mar 30 '15 at 22:45
  • 9
    @Vilx-: When you say you care about the experience of your users you seem to only mean a subset of your users. The main reason for using markup correctly in the way you are describing is for accessibility, which directly relates to the user experience of disabled users. These are the people that benefit from you doing thing the right way and ignoring these conventions means likely making their web experience harder. – rooby Mar 30 '15 at 22:55
  • 1
    @user10042 - More or less true, yes. I use elements according to their functionality, not their semantics. Mind you, "produces a certain effect for screen readers" or "gives a certain hint to search engines" is also a piece of functionality in my book, rather than some abstract "semantics". However we digress. The question was about the specific `` vs `display:table` and not semantics in general.
    – Vilx- Mar 30 '15 at 22:57
  • 1
    @rooby - So, does using a `` for layout present some aural drawbacks compared to using a bunch of `
    ` which give the same visual effect? Assume that all the aria stuff has been applied properly.
    – Vilx- Mar 30 '15 at 23:00
  • 7
    Those "abstract semantics" are exactly what helps search engines, screen readers, and even graphical browsers... – HorusKol Mar 30 '15 at 23:18
  • 2
    Well a screen reader reads a table as tabular data so it doesn't make sense when the table is not full of tabular data. I would assume that a large proportion of screen reader users would be able to still understand what's on the page but why make it harder for them if you don't have to. Also, a lot of businesses and government agencies now have specific accessibility requirements so if you're making a site for someone else you might be forced to make it WCAG2 AA compliant or something. – rooby Mar 30 '15 at 23:31
  • 31
    @Vilx-, yes, a screen reader does treat a very differently from a
    .
    s are mostly ignored and read sequentially. Inside a
    it has to provides different navigation keystrokes/commands ("Jump to cell to the right", "Read column and row title", and so on), and voices different location information (when the reading stream enters a table it goes something like "Table 3, row 1 column 1, *Lorem Ipsum dolor...*")
    – Euro Micelli Mar 31 '15 at 00:12
  • @rooby: Is there any nice way to format a side-by-side translation in a manner that will permit either side to be read or copied in top-down order, but will can also add blank space on either side as needed to keep a correspondence between things on the left and right side that word-wrap differently? – supercat Mar 31 '15 at 22:59
  • @supercat: Possibly but I don't have the details of the top of my head. It is really outside the scope of this question. However it seems like you might be able to legitimately use a table for that kind of thing depending on your specific requirements. – rooby Apr 01 '15 at 03:35
  • What I see, frequently, is people not using tables even for tabular data. I don't understand it. – Casey Apr 04 '15 at 18:47
  • I recently saw people using divs for data in this component library: https://material-ui.com/components/tables/#data-table It confused me and I wondered if I should use it or not, so here I am. They claim that "The Table component has a close mapping to the native elements. This constraint makes building rich data tables challenging." I think we should ask them to explain this further.
    – ADJenks Apr 22 '21 at 17:06