54

I know that tables are for tabular data, but it's so tempting to use them for layout. I can handle DIV's to get a three column layout, but when you got 4 nested DIV's, it get tricky.

Is there a tutorial/reference out there to persuade me to use DIV's for layout?

I want to use DIV's, but I refuse to spend an hour to position my DIV/SPAN where I want it.

@GaryF: Blueprint CSS has to be the CSS's best kept secret.

Great tool - Blueprint Grid CSS Generator.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
roman m
  • 26,012
  • 31
  • 101
  • 133
  • Do you need CSS and html for a 4 column layout? If you do let me know, I already have it here. – Mauro Sep 14 '08 at 10:38
  • @Mauro: i wasn't talking about 4 column layout, i meant deep DIV nesting – roman m Sep 14 '08 at 20:18
  • 1
    You should be aware that there is a certain degree of controversy surrounding Blueprint: http://jeffcroft.com/blog/2007/nov/17/whats-not-love-about-css-frameworks/ http://jeffcroft.com/blog/2007/nov/18/follow-up-css-frameworks/ Just something to be aware of. :) – Charles Roper Sep 15 '08 at 10:25
  • This debate is covered quite well here also - http://stackoverflow.com/questions/30251/tables-instead-of-divs#30271. Templates like Blueprint are great, but struggling with building your own layouts(maybe not on commercial projects) is a great way to learn CSS. – Sam Murray-Sutton Sep 15 '08 at 13:24
  • Also consider: if you have tabular data, there's nothing wrong with using a table. You just don't need it to do site layout. – Robert P Oct 24 '08 at 20:57

13 Answers13

24

There's the Yahoo Grid CSS which can do all sorts of things.

But remember: CSS IS NOT A RELIGION. If you save hours by using tables instead of css, do so.

One of the corner cases I could never make my mind up about is forms. I'd love to do it in css, but it's just so much more complicated than tables.

You could even argue that forms are tables, in that they have headers (labels) and data (input fields).

Stewartside
  • 20,378
  • 12
  • 60
  • 81
Matthias Winkelmann
  • 15,870
  • 7
  • 64
  • 76
  • 2
    Strictly speaking, a form is (almost) never a table. What would be the column heading for the data - "input fields"? By that argument, you could claim anything is a table: a paragraph is a single-celled table... Form support is pretty nasty, mainly because browser-manufacturers disagree on stuff – Bobby Jack Sep 14 '08 at 13:29
  • I'd argue the labels are the headers. There is even an argument to th to specify a horizontal arrangement IIRC – Matthias Winkelmann Sep 14 '08 at 14:25
  • 1
    OK, but a set of data with a single row of headers and a single row of data is semantically closer to a definition list than a table. – Bobby Jack Sep 14 '08 at 14:45
  • There are a number of table-less ways to markup forms, all of which produce leaner markup which also works better in screen readers. Check out http://wufoo.com/ for some examples. – Sam Murray-Sutton Sep 15 '08 at 13:21
16

In the UK and in US there is a legal requirement for favouring CSS layouts over Tables. Both Section 508 (US) and the Disability Discrimination Act (UK) cover accessibility standards for users with limited vision.

In the UK the legislation extends so far as to actually make it illegal to commercially produce a site that impedes the ability of a partially sighted user in the same way that it is now illegal to have a shop with a step to enter it and no way for a wheelchair user to get in - admittedly there have been no prosecutions over website accessibility yet. However I would always go with CSS as it means that your site design is so much easier to maintain in the longer term.

Investing time in learning CSS (I used W3C schools and .Net Magazine http://www.netmag.co.uk) will pay off.

Mauro
  • 4,531
  • 3
  • 30
  • 56
9

Why tables for layout is stupid: problems defined, solutions offered.

huseyint
  • 14,953
  • 15
  • 56
  • 78
6

In my opinion, the bias should be in favour of CSS over IE6 - i.e. unless there's an insanely good reason (e.g. your site is only targetted at people using IE6, which would be weird), it's better to 'alienate' people using IE6 rather than people with poor vision and/or automated user agents. Usage of IE6 is decreasing; the latter group is increasing in number. Even if your site doesn't look perfect in IE6, it will probably be easy for those users to read it than a table-based layout will for those who can't see it.

This is a very general question, so it's difficult to answer with specifics. The two books that are excellent resources are:

If you only have to spend an hour designing your overall site layout, that's not bad going.

Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
5

CSS may not be a religion, but it is how browsers interpret HTML for layout. Like it or not, all modern browsers use (some version) of the W3C box model. To continue to rely on tables is continue to rely on a methodology that is just plain wrong in the eyes of the people who design web rendering technology.

I know CSS can seem awfully complicated at times, but I believe it is a necessity in this day and age (trust me, your clients are going to want it).

If you don't feel comfortable taking the time really learn CSS (so it takes you seconds or minutes to position elements...not an hour), then you need to pass the layout work on to someone who knows really knows the front-end.

Yes, there are a lot of problems with the current browser implementations of CSS, but nothing so drastic that you should ever feel the need to return to table based layout. Just sit down and take the time to learn it, like you would any other language or framework.

The best online reference resource I've found is this one: http://reference.sitepoint.com/css

But it might not hurt to look at a book like Designing With Web Standards which goes a long way in helping you to understand why this stuff is important.

Bryan M.
  • 17,142
  • 8
  • 46
  • 60
4

I was also thinking Blueprint was great until I saw YAML (Yet Another Multicolumn Layout). There is an online builder tool which is fantastic. I can get a cool looking multicolumn layout within 5 mins.

cnu
  • 36,135
  • 23
  • 65
  • 63
2

After a while you don't even think about it. Using divs with CSS seems like the easier option imo. Plus, you have more freedom when using frameworks such as jQuery. I couldn't imagine doing some of the cool jQuery stuff without using css or divs. If you use tables for style and layout I feel like you miss out on a lot of new technologies and stay stuck in the 90's.

Spencer Cooley
  • 8,471
  • 16
  • 48
  • 63
1

You use CSS for layout because not only is it semantically correct but because tables have multiple drawbacks.

Tables are horrible for accessibility because they break almost all screen readers, which in turn gives the visually impaired worthless information because of the way the tables are read.

They also render much slower than their CSS counterparts. Tables have to be drawn twice, once for the layout, and again for the content. This can mean that if you have a remote image or two on a server with a slow connection that your ENTIRE LAYOUT will not render.

Would you use an array to store a dictionary when you have a hashmap? No. And you shouldn't use a table when there's something out there which works better.

1

This may be unhelpful but I somehow don't understand all these problems related to CSS. If a newspaper designer would try to embed a movie in the ad page, everybody would agree that he's a bit crazy. But still those same people pine after three-column layouts in HTML. HTML is just not apt to handle this kind of layout well at the moment. Furthermore, multi-column layouts are generally not really well-suited for reading on computer monitors. Aren't there enough viable alternatives?

And by the way, even tables don't offer a good way of implementing a fluent column layout so this is no reason at all to resort to such hacks. Assuming a halfway modern browser (i.e. > MSIE 6), tables don't offer any advantages over clean HTML + CSS that I know of.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    Except HTML can handle it. What can't handle it is CSS. – Matt Mitchell Sep 14 '08 at 12:09
  • 1
    Can you be more specific? Exactly what can't CSS handle? – Bobby Jack Sep 14 '08 at 13:01
  • 1
    Konrad mentioned "three-column layouts" cannot be achieved in HTML. Tables very easily achieve this. It is a similar story with 100% height elements and vertical alignment. CSS tends to fail hard in these situations and rather than working the standard to how people work they criticise the design. – Matt Mitchell Sep 14 '08 at 13:17
  • 1
    I believe the standard **is** being enhanced - the "advanced layout module" of css3 (http://www.w3.org/TR/css3-layout/) will give us much of what we require here. Granted, it will be some time before CSS3 is widely supported. K only said that 3-column layouts cannot be achieved "WELL" in HTML. – Bobby Jack Sep 14 '08 at 13:27
  • 1
    CSS can handle multi column layouts very well. You people assume that multi columns means "a single article, split up into multiple columns" (just like your daily newspaper), but that's not it. The standard page design is, nav|content|etc - that's multiple columns as well and CSS can handle that. – Till Sep 15 '08 at 05:01
  • Yes, but CSS can't handle what people assume well. CSS shortsightedness is always explained away by blaming the developer. CSS is always nicer when it works, but I don't like advocates blaming the developer for trying to do something that users want. – Matt Mitchell Sep 15 '08 at 06:43
  • 1
    Graphain, my point was *exactly* that CSS can handle the same as tables. Granted, multi-column layouts require some hacks but are possible. However, both CSS and tables fail at breaking continuous text into multiple columns (although there are hacks in CSS/JS to do even this). – Konrad Rudolph Sep 15 '08 at 16:37
  • http://www.youtube.com/watch?v=UaMdYrNVLG0 - this newspaper's designer "is a bit crazy"... – Vili Dec 01 '09 at 06:28
  • @Vili: They’re crazy all right. They are using it for ads! … and this just goes to show how quickly established truths become obsolete in technology. Duh. Someone hand me a new example please … how ’bout Flash games in newspapers? – Konrad Rudolph Dec 01 '09 at 10:12
1

I would just use the table.

In my experience, using a table for layout will work the same in all browsers and the CSS will not (especially if you're trying to support IE6). It's just not worth the hours and hours of coding to get a layout to work in CSS when it can be done in 10 minutes using a table.

The other advantage to using tables is that your layout can very easily dynamically size itself to content. Trying to get that done with CSS is a huge nightmare.

17 of 26
  • 27,121
  • 13
  • 66
  • 85
1

I find there are lots of limitations to CSS that just seem to hint the specification designers don't make websites for a living.

Use HTML tables if you can't do it easy in CSS.

Having said that, some of the frameworks do help and it always nicer to do in CSS if you can manage it.

Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
1

You might be able to find some inspiration here: http://blog.html.it/layoutgala/

erlando
  • 6,736
  • 4
  • 24
  • 29
1

A List Apart is a great reference for using semantic HTML, the Holy Grail article is probably one of the best examples. Also, check out CSS Zen Garden for some inspiration on the topic or read Dave Shea's excellent book "The Zen of CSS Design."

Eric DeLabar
  • 320
  • 1
  • 11