Are CSS Tables Better Than HTML Tables?

Steven Bradley

by Steven Bradley
on Thursday, October 13th, 2011
in CSS

«

»

Mention css and tables in the same sentence and controversy is sure to follow. Web designers like myself have been telling you not to use html tables for layouts and now here we have a way to create tables with css alone.

What’s the difference between html tables and css tables? Should we use css tables? If so how?

Once again I want to thank Pedro for emailing me the idea to talk about css tables. I hope I cover what you’re interested in knowing.

Let’s get to the how of css tables first and then tackle the question of whether or not you’d want to use them in practice.

Glass top coffee table with the letters css on the table legs

How to Create CSS Tables

The css table model is based on the html4 table model and has pretty good browser support. In both table models the table structure parallels the visual display of the table itself.

Rows are primary. The row is specified explicitly and columns are derived from how the rows and cells are set up.

I’m sure you’ve worked with html tables before and if you have you shouldn’t have any problem creating css tables.

Each html table element has an equivalent css display value. The only real difference is that there’s no distinction between td and th with the css variety.

Below are the html table elements and their corresponding css display value.

table     { display: table }
tr        { display: table-row }
thead     { display: table-header-group }
tbody     { display: table-row-group }
tfoot     { display: table-footer-group }
col       { display: table-column }
colgroup  { display: table-column-group }
td, th    { display: table-cell }
caption   { display: table-caption }

Captions can be positioned above or below the table with the caption-side property

#caption {caption-side: top}
#caption {caption-side: bottom}

Looking at the above it shouldn’t be too hard to figure out how to set up a css table. Here’s a simple example.

<div id="table">
  <div class="row">
    <span class="cell"></span>
    <span class="cell"></span>
     <span class="cell"></span>
  </div>
  <div class="row">
    <span class="cell"></span>
    <span class="cell"></span>
     <span class="cell"></span>
  </div>
  <div class="row">
    <span class="cell"></span>
    <span class="cell"></span>
     <span class="cell"></span>
  </div>
</div>
#table {display: table;}
.row {display: table-row;}
.cell {display: table-cell;}

If you look only at the html above you can easily see the basic table structure except that I’ve used div and span with ids and classes instead of table, tr, and td.

A relatively small amount of css then presents the divs and spans as the familiar table, table row, and table cell.

In addition to the above the css table model includes an inline-table value, which defines a new table the same as display: table, but does so according to the inline formatting context.

Columns: Temple of a Thousand Warriors

Columns and Column-Groups

While tables cells are always descendants of table rows it makes sense to set some properties on columns. The css table model allows for the following on columns and column-groups

  • border — The usual properties as long as border-collapse has been set to collapse on the table element
  • background — The usual properties as long as both row and cell have background set to transparent
  • width — Sets a max-width on the column
  • visibility — If set to collapse (the only available value) then column cells don’t display, cells spanning into other columns are clipped, and width of the table is adjusted

CSS Table Stacking Context

Different table elements have different stacking contexts for the purpose of adding backgrounds to these different layers.

These layers can be seen in the image below.

  1. table – lowest layer
  2. column group
  3. columns
  4. row group
  5. rows
  6. cells – highest layer

The background of any layer will only be seen if all the layers above it have backgrounds set to transparent.

This can be a nice way to show an empty cell is truly empty by having its background be transparent and letting the background of the row, column, or table show through.

Table layers stacking context

Table Layout Algorithm

The width of css tables can be calculated using one of two algorithms. This can be controlled through the table-layout property and the 2 values below.

  • fixed — The width of the layout is not determined by its content, but rather by the widths set on the table, cells, borders, and/or cell spacing
  • auto — The width of table is set by width of columns and borders

The important thing to consider is that a fixed table-layout is a one-pass calculation and very quick. On the other hand auto requires the same multiple passes of html tables. It’s also the default value.

If you explicitly set a width on the table then the fixed table layout algorithm will be used.

By default the cell height will be the minimum necessary to display the contents of the cell, but you can also explicitly set heights. All cells in a row will be the height of the cell with the maximum minimum necessary height.

The vertical-align property of each table cell determines the cell’s alignment within the row

  • baseline
  • top
  • bottom
  • middle
  • sub, super, text-top, text-bottom, <length>, <percentage>

The last group of values do not apply to cells, but the text within the cells. The cell is aligned at the baseline instead.

CSS Table Borders

There are 3 interesting properties for table borders

  • border-collapse — values can be collapse, separate, or inherit
  • border-spacing — values can be <length> (horizontal), <length> (vertical), or inherit. border-spacing is the distance between cell borders.
  • empty-cells — values can be show, hide, or inherit. If cells are empty or set to visibility: hidden content doesn’t show by default. Setting empty-cells: show on the table will cause backgrounds and borders to display for the empty cell.

Diagram showing table spacing, borders, and width

Should You Use CSS Tables?

Are css tables better than html tables? If so what advantages do they have and if not why should we use them at all? Good questions that I don’t have great answers for.

I can say I’ve never used a css table in practice and have no intention of using them any time soon. If a page calls for tabular content it strikes me than an html table is called for and I think we have and will have better techniques for site layout than css tables.

I took a look at an older post I wrote on css vs tables to remind myself of the cons for using html tables for layout over a combination of divs and css.

  • Extra code — html tables require more structural code than divs, but css tables use just as much. If anything css tables use more since ids and classes will likely be added. If html tables use too much code then css tables do too.
  • Rigid structure — html tables are source dependent. The order you structure the cells is the same order in which they’ll display. The same is essentially true of css tables as well.
  • Browser rendering — browsers require multiple passes at the structure of html tables. They should only require one pass to display a css table if the table-layout is set to fixed. They’ll require the same multiple passes if set to auto.

Considering the above css tables aren’t offering enough benefit over html tables to use them for layout.

We could reach and suggest that since the css can be easily changed a css table is less rigid than an html table, but in practice I think they’re going to be just as rigid.

CSS tables do have the advantage of being more semantically correct as we can choose html elements that better describe our content.

Overall it’s hard for me to see much improvement in css tables over html tables, some perhaps, but not enough to justify to myself using them.

I think some of the other css layout modules on the horizon will be better and even our current practice of using floats and positioning for layout are still a better option.

At the same time I can’t say I’ve worked much with css tables. This post is the most time I’ve spend with them since they’ve been introduced and I’m open to someone else’s reasons for why we should use them.

I have a hunch they’ll remain in use to solve some specific problems like vertically centering content or cleverly switching the display order of different elements in a responsive design.

They may also prove to be a good pattern for navigation. I don’t see them being a viable solution to the problem of layouts though.

Infography showing periodic table of OS admin

Summary

CSS tables are pretty simple to understand and use. It’s mostly a matter of remembering the corresponding css display property values for the basic html table elements.

table becomes display: table. td becomes display: table-cell, etc.

There are some nice features of css tables like the ability to collapse one or more columns through the visibility: collapse property and they can be used as solutions to some specific problems.

However they don’t provide enough advantage for me over html tables when it comes to layout. Their advantages seem minor and a bit of a reach. Ultimately I think we have better layout solutions than both html and css tables.

Again though I’m open to being convinced otherwise.

Have you used css tables in practice? If so how? Have you used them for site layout? To present tabular data? To solve a specific problem?

Spread some karma These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • StumbleUpon

Subscribe to TheVanBlog | Email This Post Email This Post

32 Responses to “Are CSS Tables Better Than HTML Tables?”

SD
2011-10-13 09:07:59

nice comparison between css tables and html table….in my career I have used both, but I like using CSS tables the most…thanks

2011-10-16 10:31:13

Is there a reason you like css tables? I still don’t see myself using them in practice except for some limited use cases.

 
 
2011-10-13 10:33:06

I think we shouldn’t compare HTML tables and CSS tables. The purpose of CSS is to separate the content from presentation. CSS Tables are valid technique as floats or positioning. Actually CSS Tables are much more flexible than floats. I built CSS Framework with only 2 lines of code http://www.vcarrer.com/2010/10/two-lines-css-framework.html that can accept multiple fluid columns(auto resize). Here is a demo http://www.allapis.com/Two-Lines-CSS-Framework/demo1.html , making this with floats is almost impossible. I think in the future when IE6/7 finally die, we should give this technique a chance.

2011-10-16 10:32:39

Thanks Vladimir. I think the comparison is fair because the structure of the html ends up being the same. The tag name and the semantics change, but the structure is still essentially the same.

 
 
Chris Subscribed to comments via email
2011-10-13 11:41:14

Vladimir, awesome. Two lines of code for layout in any web measurement…that’s gotta make sites load faster, right?

2011-10-16 10:33:47

Chris, check out the rest of Vladimir’s code at his site. He has plenty of great demos and downloads.

 
 
Chris Subscribed to comments via email
2011-10-13 11:44:22

vertical-align property — hu-ah

 
Don
2011-10-14 10:20:08

I’ve always wondered about this link http://expression.microsoft.com/en-us/dd794430.aspx and now you’re addressing it in a similar vein. What about CSS3 Grids http://dev.w3.org/csswg/css3-grid-align/ ? It seems to me that the days of floating divs are numbered and a better layout approach should be developed.

2011-10-16 10:35:29

Don I plan on looking the the grid module in an upcoming post. Not sure when I’ll publish it, but it’s something I’m working on. There are definitely some exciting things coming in layouts, though we’ll likely still be using floats in the foreseeable future.

 
 
Jason
2011-10-14 10:41:44

I’ve used the display:table properties before for something that had 2 equal sized columns with backgrounds. But had to put in a polyfill using the 5-6 nested & floated divs solution if the browser didn’t support display:table, which is still a problem since IE7 is still quite common.

2011-10-16 10:37:29

I’ve seen css tables used for equal sized columns too. Might be one of the use cases for them, though there are other ways to develop columns with equal heights.

 
 
Ade
2011-10-14 13:44:44

It is very important that tabular data should be understood as tabular data. The relationships between those data are what make them suitable for presentation in a table.

While AT and accessibility-layer support for CSS tables is currently so limited and unreliable, I believe that no-one should be presenting tabular data without using table mark-up. Otherwise, the relationships that are vital to understanding the content are lost.

On the other hand, presenting non-tabular content with CSS table properties will, in time, be just as misleading and problematic as misusing table mark-up in the same way.

2011-10-16 10:38:38

I agree about presenting tabular data and as you can tell I’m not sold on using css to create tables for layout either.

 
 
Huh
2011-10-14 15:43:38

It would be more interesting if I didn’t get the vibe that it’s sort of equating two things that shouldn’t be equated.

The two aren’t mutually exclusive. One’s style/skin, one’s semantics.

CSS table display types are a specification for the apparent visual functionality of HTML tables. HTML tables predated the specification, yes. But so did block and inline, they just weren’t frequently called that.

If you need a table you would absolutely NOT use what you’re calling “CSS tables” (which I take to mean “using CSS display types to create the visual appearance of a table without using table, thead, tbody, tfoot, caption, tr, th, or td”). You WOULD use an “HTML table” and you would use CSS to style it.

You can also leverage the CSS table layout model (not “CSS tables”…is this really a thing?) to achieve certain visual effects on non-tabular data.

2011-10-16 10:42:26

They’re different things, but like I said above I think the comparison is fair. Both are creating a similar html structure and in the case of site layout are going to be used in the same way.

I agree that you should use an html table for tabular data and said so in the post. Some have suggested that css tables (and yes it’s probably more correct to say the css table layout model, though it takes more time to type :) ) could overcome the limitations of html tables for layout. I don’t think they overcome those limitations, which is mainly what I was comparing here.

 
 
Devon
2011-10-16 10:13:01

The purpose of “html” tables is to display tabular data. Using other elements and css to replicate table elements is less than ideal. We can use multiple tags and css to replicate the base style of an h1 except have it within an em tag, doesn’t mean that we should.

2011-10-16 10:43:39

Agreed. html tables for tabular data is the way I would go. I can see some limited use cases for css tables, but in general they won’t be commonly used.

 
 
2011-10-18 01:03:20

The question should be – which version is more accessible? And the table structured content is definitely better structured and accessible, then a css based table. Think about screenreader and you will never think about css tables! Perhaps ;o)

Ade
2011-10-18 13:42:18

That’s exactly the biggest problem for me. See my earlier comment from the 14th. This situation may change, but it always takes a long time for that to happen to the point at which method X becomes real-world usable.

 
2011-10-24 10:33:46

I agree. When it comes to presenting tabular data html tables should be used. I think most people look to the css counterpart for layout where the information doesn’t need to be structured as a table.

 
 
2011-10-18 05:09:12

CSS table’s should not be used. If used, there is no way for a (search engine) robot to understand if it is a tabular data or not.

using CSS table are like using instead of .

2011-10-24 10:35:34

If it’s not tabular data why would it matter if search engines can’t interpret it as tabular data. I don’t think search engines care what technique we use to layout a site. They don’t care if our columns are created through floats or through positioning for example.

 
 
d8niel Subscribed to comments via email
2011-10-21 11:07:37

I don’t use css tables per se, but I have used (recently) a container element with ‘display: table’ and set its children to ‘display:table-cell’ to have the elements arrange horizontally w/o having to use floats or absolute positioning.

Of course, this was for a device-specific use (iPad), so I didn’t have to worry about cross-browser issues.

Otherwise, tabular data should be placed in html tables, but the different ‘display’ options can offer interesting solutions to layout probs.

imho

2011-10-24 10:37:25

I think how you used display: table is more how they’ll be used in the future. I can see where css tables will help solve specific problems. I don’t see them being used much for overall layout though.

Yep, completely agree about using html tables for tabular data. Makes more semantic sense.

 
 
Peter
2011-11-01 01:07:56

I’d be curious which methods you had in mind when you wrote “Ultimately I think we have better layout solutions than both html and css tables.”

2011-11-02 08:31:47

Even though both have their faults I think using floats and positioning is a better method for site layout than tables (either html or css).

In the not too distant future we’ll have or may have layout modules like flexbox and templates and grids, which I think will improve things even more.

 
 
Bevan
2012-01-09 10:45:36

It seems people are confused about the difference between CSS tables and HTML tables.

CSS tables, for lack of a better term, were never designed to be used for displaying tabular data. They were created to provide easy to implement site layout control only. If you need to display tabular data then use a table, an HTML table. To suggest otherwise, or even compare the two in this manner is absurd.

Therefore the only reason to compare HTML tables to CSS tables would be to compare their suitability for layout control. Again, this is absurd since using HTML tables for layout isn’t what they were designed for.

So what have we learnt? CSS tables are like a can opener and HTML tables are like a knife. They are different things for different purposes. Not interchangeable depending on your mood.

Floats are also a pain. Yes they work, or can be convinced to work here and there. All my sites heavily rely on them. They do cause problems with other elements however and their implementation for even a typical 3 column layout is not a trivial task. The layout complexity soon increases as you have to work around other problems that arise. Floats for site layout become very counter intuitive very quickly. Floats were not designed for site layout to the extent they are been relied upon today. CSS tables are designed for this purpose however.

So why bother with CSS tables? Because they provide an “easy” to understand, logical, predictable tabular layout for a page while remaining semantically innocuous. Perhaps there is no benefit over HTML tables in terms of effort but HTML tables when used for layout are semantically incorrect. That is enough of a reason right there to use CSS tables instead of HTML tables.

Naturally this brings us to the following quote from the above article:

“Considering the above css tables aren’t offering enough benefit over html tables to use them for layout.”

Again I’m confused with this comment. HTML tables shouldn’t be used for layout. Never! This renders the above quote pointless and irrelevant. Two paragraphs further down the article answers the question as to why you’d use CSS tables. To paraphrase, “Because CSS tables are semantically correct”. In other words, you’d use CSS tables because they are the correct tool for the job. HTML tables are not! That is the only reason you need. Using HTML tables for layout is unacceptable and wrong.

I’m also not convinced the argument that CSS tables are rigid is particularly valid. What is the alternative? Floats often have to be placed in a specific order or the combination of layout constraints will be broken. If you want pure control over a page then you’ll need to use absolute positioning which has its own drawbacks, especially with fluid sites and other design requirements.

So why use CSS tables? Because using HTML tables is wrong. Using the other methods requires complex solutions, hacks, tricks and at the end of it the code is being coerced to behave in ways it wasn’t designed for. Therefore you could also argue much of the alternative techniques are also wrong. CSS tables to the rescue. Great! I Can’t wait for the appropriate support of it.

Until a better alternative becomes available and realistically viable, CSS tables are the nearest viable solution to the fundamental layout problems that have plagued designers since the beginning of it all. They are a solution, not a hack and not a workaround, designed specifically for the task.

I’m sure there are at least a few good reasons why you’d use CSS tables over HTML tables. I’m not saying they are perfect either. They are however a great step forward. Once CSS tables are sufficiently supported that is.

2012-01-09 11:34:07

Did you catch the first couple of paragraphs of this post? I agree that html tables shouldn’t be used for layout and said as much. 80% of this post is simply walking through how to use css tables. Nowhere do I advocate html tables for layout or suggest that css tables and html tables are the same thing.

Still it’s natural that some people will want to compare the two based on the word table. And like it or not there are people who still think it appropriate to use html tables for layout. I’m not one of those people, but they do exist.

Also keep in mind that this is an intro post to the topic of css tables and until someone has an understanding at the intro level they aren’t going to be able to determine if css and html tables are the same or different or how they might differ.

After looking through the specs for css tables and playing around with them I don’t think they offer a good solution for site layout. Sure floats can be a pain at times, but I still think they’re a better solution. There are better solutions that floats coming, but css tables aren’t one of them.

To show why I simply referred to an older post where I point out why I think html tables are bad for layout. I think some of the same arguments apply here. In the older post I’m saying A, B, and C are reasons why html tables shouldn’t be used for layouts. Here I’m looking at css tables in terms of A, B, and C and saying if css tables also have those same issues then they aren’t a good solution to layout for the same reason html tables aren’t good.

The point isn’t to say css tables and html tables are the same thing. It was simply to use the the same arguments against one for the other. Maybe I didn’t do the best job getting that across and if so my bad.

I understand that people who’ve worked with both will see they aren’t the same and aren’t meant to be used for the same thing, but keep in mind that not everyone has worked with both.

 
 

What others are saying about this post

Are CSS Tables Better Than HTML Tables?
Are CSS Tables Better Than HTML Tables? | Design News
HTML : Faire un tableau avec des div - Scoco
Las tablas que no son tablas siguen siendo tablas - La Isla Buscada


Name (required)
E-mail (required - never shown publicly)
URI
Your Comment

Subscribe

Search TheVanBlog

Recommended

Small Business Forum Teaching Sells Free Report

SEO Book Training

Popular Posts

Proud member of the Smashing Network