For A Beautiful Web

Are CSS tables ready for prime time?

I was lucky to be sent a preview copy of Rachel Andrew's soon-to-be-published book Everything You Know About CSS Is Wrong!, published by those nice chaps at SitePoint. I'll be writing a full review later this week, but as the book is largely (almost exclusively) devoted to CSS display : table; properties, I couldn't wait to try out some of the techniques she advocates.

I should confess that, up until now, I have ignored CSS tables, largely because of a lack of support for them in Internet Explorer. Yes I know I normally make a big thing about web designs need not look exactly the same in all browsers, but when it comes to the fundamentals of a page's layout, I'm as straight as the corporate guy. Now that CSS tables have been implemented in Internet Explorer 8, it is definitely time for me to reconsider and to learn more about them.

Making me think differently about CSS tables is Rachel's aim in Everything You Know About CSS Is Wrong! As we'll see, she does a damn fine job.

The stability and reliability of table-based layouts was, for a long time, the main reason some designers chose to stick with HTML tables for layout [...] Using CSS tables for your layout will bring this stability to your CSS layout work. You’ll waste a lot less time fixing mysterious bugs and inexplicable behavior in even the latest browsers.

Support for CSS Tables

CSS tables have already been implemented in the majority of recent browsers including Firefox (from version 2), Safari, Opera 9.5 and now Internet Explorer 8. Of course this is an imperfect world and there is no support for CSS tables in either Internet Explorer 6 or 7. Rachel expertly describes ways to address this important issue and I'll cover more about what she recommends in my full review of Everything You Know About CSS Is Wrong!

Are CSS tables ready for prime time?

To find out whether I think CSS tables are ready for prime time in the way that she describes, I took the layout from a current project, abandoned floats for layout and replaced them with CSS display : table; properties to see what display challenges I faced along the way and where CSS tables could be best utilized. I had two more important rules for my experiment. I would not change my markup by a single character and I would accept only minor negative design differences.

Before you go any further, open two more tabs in your browser and pull up these two files:

(As this project is not yet live I have removed the client's branding and content. Copyright: This design is not a template and must not be reused.)

Dissecting design elements

In the first example, each of these elements were laid out horizontally using floats:

  • The primary navigation
  • The two main content columns in each horizontal block
  • The two latest news items in the third block
  • The <p>, <ul> and <vcard> in the footer

I won't detail all of the markup and CSS I used as I'm sure they will be nothing new to you. Instead I'll take the core float techniques and replace them with CSS tables, starting with the navigation. Not uncommonly the main navigation is given meaning by using an unordered list.


<ul id="nav-main">
<li><a href="#">{Navigation}</a></li>
[...]
</ul>

Each list item is floated to create a standard horizontal navigation design.


#nav-main { 
overflow : hidden; 
width : 940px; 
padding : 10px 0; 
background : #4e7c92 url(bg-grad.jpg) repeat-x 0 0; 
}

#nav-main li { 
float : left; 
width : 185px; 
text-align : center; 
}

Given the well-documented fragility of floats and of course the need for float clearing, using CSS tables makes sense as it cures both of these ills.


#nav-main { 
display : table; 
border-collapse : collapse; 
width : 940px; 
background : #4e7c92 url(bg-grad.jpg) repeat-x 0 0; 
}

#nav-main li { 
display : table-cell; 
width : 185px; 
padding : 10px 0;  
text-align : center; 
}

But what about replacing floats for structural columns, the scaffolding that supports almost every design?


.content { 
overflow : hidden; 
width : 940px; 
}

.content-main { 
float : left; 
width : 540px; 
}

.content-sub { 
float : right; 
width : 340px; 
}

Again, CSS tables solve the challenges of column dropping (due to over-with elements) and clearing but with one other major advantage; the days of struggling with equal height and faux columns are finally behind us.


.content { 
display : table; 
border-collapse : collapse; 
}

.content-main { 
display : table-cell; 
width : 540px; 
padding-right : 30px; 
vertical-align : top; 
}

.content-sub { 
display : table-cell; 
width : 330px; 
background-color : #f8f7f3; 
padding : 15px 15px; 0 15px; 
vertical-align : top; 
}

Compare the two examples again. Using CSS tables the right column now has a grey background that extends to the full height, equal to to the taller left column. Magic. A little added border-radius : 10px; and the page instantly appears better structured.

The same CSS table properties are also implemented in both the latest news and the footer content, proving that CSS tables are reliable for creating columns from all elements, not just divisions.


li.hentry { 
display : table-cell; 
width : 240px; 
padding-right : 60px; 
}

li.hentry:last-child { 
padding-right : 0; }

#siteinfo { 
display : table; 
}

#siteinfo p { 
display : table-cell; 
width : 220px; 
padding-right : 80px; 
}

#siteinfo ul { 
display : table-cell; 
width : 300px; 
}

#siteinfo .vcard { 
display : table-cell; 
width : 340px; 
}

Problems with positioning

If you are paying close attention, you will have noticed a small design difference between the display of the latest news items between my two examples. As Rachel so expertly describes in Everything You Know About CSS Is Wrong!

when setting position: relative; on an element that also has a table-related display value specified, the positioning is ignored. This behavior has previously been documented by Alastair Campbell, who points out in his article that the CSS 2.1 spec is not clear on what browsers should do when an element displaying as a table element is relatively positioned.

I won't steal Rachel's thunder, needless to say that she describes several workarounds for this positioning problem, or you could do as I do and design around the problem.

You will still need floats and positioning

If you do choose to steer your CSS in the direction of CSS tables, you will still need floats and positioning. CSS tables could not transform my unordered list of Flickr thumbnails into the tight grid that I want for this design and so the floated list items remain. That's OK though, as for interface elements like this, floats remain the right tool for the job.

Excited again

I have to say that I'm excited about CSS tables. It's not very often these days that a CSS technique, particularly one that has long been a part of CSS2.1, has me reconsidering how I lay out my pages but Rachel has achieved a lot in only few hours with her book.

I will write a more detailed review of Everything You Know About CSS Is Wrong! in the next few days. But until then, my brain juices are flowing with possibilities. But what about you? Do you think that CSS tables are ready for prime time?

Places on our Visual Web Design Master Class in London on 1st December are selling fast. Don't miss out, book your place before 10th October 2008 and you could win a copy of Transcending CSS: The Fine Art Of Web Design and a copy of the Inspired Design DVD; together worth £68.00.

From the people behind For A Beautiful Web and Stuff and Nonsense. You will find news about our events, links to sites and articles elsewhere that have inspired us, plus original articles and screencasts on the creative and technical sides of web site design and development by Andy Clarke and others.

Blog archives

Subscribe to blog

There have been 15 replies so far

  1. #1Scott

    Oct 8th 2008 • 12:22 am

    very exciting read andy! Definitely going to have to check this book out. Thanks for the examples to go along with it.

  2. #2prisca

    Oct 8th 2008 • 7:07 am

    Andy, thanks for a great post ;)

    well, the word ‘table’ alone in connection with layout tends to make me shudder....
    Though I have played with CSS tables a little - I was never really convinced. Reading your thoughts and especially seeing your examples alongside - it does look like something worth having another look at and exploring further....
    will check out Rachel’s book :)

  3. #3Clive Walker

    Oct 8th 2008 • 7:54 am

    I agree that CSS tables are due for more consideration. I have not read Rachel’s book but I would be interested in reading about the browser work-around for browsers like IE6 and 7 that do not support CSS tables. Go on, give us a clue… are we talking conditional comments here?

  4. #4Andy Clarke

    Oct 8th 2008 • 8:52 am

    @Clive Walker: You’ll have to buy Rachel and Kevin’s book for all of her strategies and workarounds. Needless to say they follow the sort of approach I normally encourage.

  5. #5Gary Barber

    Oct 8th 2008 • 9:05 am

    LOL, prime time when IE8 has market share, then we can all start to consider them, 2012 maybe.. Sitepoint guys have been teasing us this this book on Twitter, they are very cruel! :)

  6. #6Shaun Hare

    Oct 8th 2008 • 9:16 am

    I look forward to Rachel and Kevin’s book. I was thoroughly impressed with the way your demonstration site looked in IE8 and all others I could lay my hands on. The approaches seem great, especially to a developer who often struggles with column heights etc. Bring back the tables! (but the new improved ones please)

  7. #7Egor Kloos

    Oct 8th 2008 • 9:29 am

    I’m not holding my breath until IE8 becomes mainstream. So, until then, CSS tables are irrelevant to me. If it’s not cross browser then it isn’t really anything.

    Having said that, I will keep my eye on this ‘demonstration’ book.

  8. #8Alex Older

    Oct 8th 2008 • 9:48 am

    I look forward to the book. Although I get the feeling that I’ll still adopt the floating instead of displaying tables to try and save myself duplicating work.

    Though it might be a good time to dive in to CSS frameworks.

  9. #9Andrew Ingram

    Oct 8th 2008 • 9:53 am

    sGiven that most of us won’t have the luxury of dropping support for IE6 and IE7 in the near future, the utility of CSS tables are going to be limited to those who are luckier with their demographics, but it’s always worth trying them out and looking for ways to make them useful now.

    I still think the name of the style rules are absurd though. display: table, row, cell etc are absolutely atrocious names for values since they give no indication as to the behaviour they represent. Only someone who comes from a background of old html-table layout will know what the terms mean which raises the entry barrier for new developer. There’s no part in the definition of a tabular cell that suggests that it must fill all vertical space which it is allocated.

    What’s wrong something like display: fill-vertical which is really all we want from things.

    It’s not like the default style for a paragraph is display: paragraph, why on earth should display: table be the default style for a table?

  10. #10Andi Farr

    Oct 8th 2008 • 9:59 am

    Thanks for the writeup Andy - definitely going to be picking this up later. Some really great examples here - and your excitement at the prospect of some new techniques is clearly infectious!

  11. #11Andy Clarke

    Oct 8th 2008 • 1:46 pm

    @Andrew Ingram: “Given that most of us won’t have the luxury of dropping support for IE6 and IE7 in the near future, the utility of CSS tables are going to be limited...”

    That’s partly true. I guess the bigger question is “do CSS tables offer enough advantages to justify the workarounds?”

    “I still think the name of the style rules are absurd though. display: table, row, cell etc are absolutely atrocious names...”

    People do sometimes comment on that. It’s important to remember that display:table; means display ‘like’ a table would display.

    @Alex Older: “Although I get the feeling that I’ll still adopt the floating instead of displaying tables to try and save myself duplicating work.”

    That’s a decision that will depend on the project I suppose. Rachel offers several approaches to avoid simply duplicating CSS tables with floats in an IE6/7 only stylesheet.

    “Though it might be a good time to dive in to CSS frameworks.”

    Don’t get me started.

    @Egor Kloos: “If it’s not cross browser then it isn’t really anything.”

    This from the man who designed the most famous, non-cross-browser CSS Zen Garden design.

  12. #12Egor Kloos

    Oct 8th 2008 • 3:09 pm

    I’m not going to argue my Zen Garden entry (062). However, point taken, I was a bit flippant in my previous comment.

    It’s been discussed before. Webpage layout has been and is still problematic to say the least. Frameworks contain solutions but are in of themselves not solutions so I avoid these like the bubonic plague.
    Your positioning presentation demonstrates that we tend to forget what we already have. I think CSS tables will become very useful in this regard. This book will hopefully push useful CSS and minimal HTML in the right direction. I’ll refrain from further judgement until I’ve read it.

  13. #13Ryan

    Oct 8th 2008 • 7:57 pm

    CSS frameworks certainly aren’t a solution, I’ve not come across any so far that I’d touch with a barge poll. Especially the frameworks that attempt to make layout easier, they make me feel ill.

  14. #14Curtis Cunningham

    Oct 8th 2008 • 9:23 pm

    Great article! I’m just learning CSS and all it’s wonderful quirks, I mean - features, and this definitely helps.

    As a little test, I tried your samples in IE6. As I’m sure you know, the first site works great, the second is broken.

    More importantly though, this site (forabeautifulweb.com) appears very broken through IE6, compared to using Mac Safari - just an FYI.

    Thanks again!
    Curtis.

  15. #15Matthew

    Oct 14th 2008 • 11:51 am

    Thank u r you for your information. It very useful. u r Your blog Is very nice. (Ed says: Digg style misspellings corrected.)

Commenting is not available in this weblog entry.
Stuff and Nonsense Ltd.
The Cow Shed Studio, Eversleigh
Gwaenysgor
Flintshire, North Wales
LL18 6EJ
UK