9

Possible Duplicate:
Why not use tables for layout in HTML?

I know everyone is all about DIV's and css and against tables now days, I have always used tables because they are so easy for me to visually see what I am doing while building, I am just now ventruing into the DIV world.

So my question are tables completely replaced by div's generally? I notice on the source of stackoverflow it is mostly DIV's but still uses tables as well, so I am guessing that tables must be used sometimes?

Below is an image of something I am trying to accomplish, most of it is coded but some of it is added in on photoshop, so far I have it all done in div's however what I have is only the background cells which are a list item and the photos, what I am missing from my code is all the text areas and it would be super easy for me to position the text areas within a table cell but since like 80% of the object is done with just css and divs I am not sure if I should just try to finsih it with just div's or if a table inside the comment div's would be the way to go.

I noticed on this page that the ansers are a table inside of a DIV alt text http://img2.pict.com/e0/4e/de/1486585/0/screenshot2b15.png

Community
  • 1
  • 1
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 2
    You may find this article interesting: http://www.smashingmagazine.com/2009/04/08/from-table-hell-to-div-hell/ – RaYell Aug 18 '09 at 13:07
  • I have updated my post with w a photo and description of what I am trying to do, I am not sure if should use tables or 100% div's/css – JasonDavis Aug 18 '09 at 13:32
  • Using CSS is the correct way to control the display and look of the page, but div's should neither be used solely for design purposes. I've seen pages with thousands of nested divs and those are just as bad to maintain as those with thousands of nested tables. Div should be used to group content that go together. (Note: I am using the verb *should* as I am also guilty of bending those rules if the semantic correct way is hard or impossible to achieve) – Mike Aug 18 '09 at 14:52
  • I was able to finsih my project with just css so I am happy about that, it wasn't too hard either and I think it is better then having gone the table route – JasonDavis Aug 18 '09 at 21:11
  • See this question. http://stackoverflow.com/questions/83073/why-not-use-tables-for-layout-in-html – David Basarab Aug 18 '09 at 13:08

14 Answers14

18

To be semantically correct, tables should only used for tabular data and not for laying out a page.

David Dorward brought something to my attention in a comment. According to the HTML 4.01 Specification's section on Tables in HTML Documents:

Tables should not be used purely as a means to layout document content as this may present problems when rendering to non-visual media. Additionally, when used with graphics, these tables may force users to scroll horizontally to view a table designed on a system with a larger display. To minimize these problems, authors should use style sheets to control layout rather than tables.

Community
  • 1
  • 1
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
  • 1
    "Tables should not be used purely as a means to layout document content" - HTML 4.01: http://www.w3.org/TR/html4/struct/tables.html#h-11.1 – Quentin Aug 18 '09 at 13:10
  • LIke would the answers on this page be considered tabular data? – JasonDavis Aug 18 '09 at 13:12
  • 1
    I don't believe so no. As I put in my answer if you wouldn't use Excel it shouldn't be in a table: http://stackoverflow.com/questions/1293698/are-tables-replaced-by-divs/1293748#1293748 – ahsteele Aug 18 '09 at 13:15
  • David - thanks for that link. I'll add it. I was unaware that it was in the official HTML docs. – Thomas Owens Aug 18 '09 at 13:37
  • The answers themselves wouldn't be tabular data. You could represent each answer, the number of votes it has, the time it was added, and the user who added it as tabular data — but they are more usefully represented as sections with headings (which SO doesn't do, ho hum). – Quentin Aug 18 '09 at 16:08
9

Theres a lot of fervent zealotry regarding this notion of semantic content, which is fine and all but the only problem is that it's hopelessly naive.

Fact: there are some things that can be done trivially with tables that either can't be done in "pure" CSS, are extremely difficult in "pure" CSS, have some nasty side effects in "pure" CSS or have serious cross-browser issues.

I did my CSS3 Wish List and in compiling that list I realized some things I've been able to do with tables since HTML 3.2 a decade ago I still can't do with divs.

I'm all for having a semantic layout. Nice ideal. But until it can do everything it's trying to replace then the thing it's trying to replace will have valid use cases.

cletus
  • 616,129
  • 168
  • 910
  • 942
  • How about trying to do a side-by-side Italian/English libretto for an opera? It's not really "tabular data", and semantically it would be better for the Italian text to appear as a group and the English text to appear as another group, but using tables with one row per line of verse or each area of descriptive text will keep things lined up even if some lines of verse are long enough to wrap. The librettos I've seen which don't use this approach generally get misaligned if text wraps. – supercat Sep 13 '14 at 17:48
3

divs are used instead of tables in most of the sites. But you can use tables in some situations where design using div will be complicated in a cross browser way.

For eg vetical aligning contents inside div will be a big problem as compared to that in table cells.

In this page also you can find table tags being used.

Tables will only be rendered to the screen after all the cells are finished processing.

Take a look at the following questions also.

Why not use tables for layout in HTML?

DIV’s vs Tables or CSS vs. Being Stupid

Yet Another Divs vs Tables Question: Forms

Community
  • 1
  • 1
rahul
  • 184,426
  • 49
  • 232
  • 263
  • 1
    Don't down-vote just because of the no-tables dogma. CSS still has weak support for creating certain kinds of layouts. I'm using DIVs myself, but I reckon the frustration to not using them in certain situations. And the vertical align example is one of them. – Ionuț G. Stan Aug 18 '09 at 13:19
1

I think a lot of people will argue that "tabular" data, or data that can best be expressed in rows and columns, should be kept in a table, but that divs were invented to replace tables as large layout elements. In my personal opinion, tables were always used as layout elements in a way that went beyond their intended purpose. That's not to say that people don't still misuse divs, for example

<div align="center">To replace a <center> tag</div>

I'd say check out A List Apart, specifically Their section on layout for tips on how to use modern compliant css-based design.

edit : My point was that this is the INCORRECT use of a div tag. In this example, you would use a style such as "text-align:center" or apply that style to the tag itself, but in this case there is no reason to wrap your text in a block-level element, because text by default is inline, so you would be better off with something more like this...

<p class="center">This is a centered paragraph</p> 

and then in your stylesheet

.center { text-align:center; }

Thus, the following:

<div>Monday | Tuesday | Wednesday | Thursday | Friday</div>
<div>Work | Work | Work | Work | Play</div>

May be compliant but it looks terrible, and you'd be better off doing :

<table>
<th>
  <td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td><td>Friday</td>
</th>
<tr>
  <td>Work</td><td>Work</td><td>Work</td><td>Work</td><td>Play</td>
</tr>
</table>
NateDSaint
  • 1,524
  • 2
  • 16
  • 35
0

Tables are appropriate for tabular data, but divs + CSS is preferred for general page layout.

http://css-discuss.incutio.com/?page=TablesVsDivs has many good reasons why, as well as some counter arguments.

Korey
  • 929
  • 1
  • 7
  • 14
0

Like Korey and Thomas said, it's better to use table to represent tabular data.

If you want to make website layout in pure CSS, you can take a look on these CSS frameworks, which ease the task :

There are a lot of css frameworks out these, just find the one which fit your needs..

Update : Here is a more complete list of css frameworks.

Philippe
  • 1,206
  • 3
  • 13
  • 19
0

Tables should only be used for tabular data. To figure out if what I am dealing with is tabular I ask myself "would I put this in Excel or Word if it?"

Bill Merikallio & Adam Pratt wrote a funny and informative article Why tables for layout is stupid. They also detail where tables should be used and when.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
  • The reason I asked about the answers on this page being ok for a table is because in the source of this page, they are using some tables inside the answer div's – JasonDavis Aug 18 '09 at 13:40
  • Agreed, there's great discussion on that very topic on MetaSO: http://meta.stackexchange.com/questions/3110/when-did-so-start-using-tables-for-layout – ahsteele Aug 18 '09 at 13:58
0

Tables as means of describing the structure of tabular data have not been replaced.

Tables as means for describing presentation have been replaced, but not with divs. They have been replaced with stylesheets in associate with whatever element best describes the semantics of the content. A div has no semantics associated with it, so it is used if there is nothing better.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

It certainly depends. The Golden Rule you must remember is that the XHTML/HTML document file it's meant to describe content and flow.

Everything that is design should be (whenever it's possible, there are some certainly some case scenarios where we can't control it, like when CMS appear on the game) controlled using a CSS file.

Now, how can we reach the most semantically and pragmatical result? As with everything, it depends. Using XHTML/HTML tag elements instead on just relying on for correct content display it's the recommended way.

Notice that I've said tag elements. DIV is just one of them, but just replacing with whenever a or / tags exists is not enough. In fact, that will push you over "divitis" (the useless employ of divs for everything!) and WILL make your job hard. Try checking most tags and use them whenever seems correct.

Sometimes it's a pretty subjective matter as to what qualifies as a content or another tag. Just in this question someone asked if this content would be considered tabulated data (the one that tables are supposed to be used for), but I think that whatever content you must order and filter (and you can copy and paste on Excel without worries) it's worthy material.

Everythin else, it's mostly always just some interesting layout display that should be worked on with CSS and other tags.

Some people will say it's too much work and not worth it. I disagree. Though learning how to work with CSS/divs nuances it's somewhat different at first, you'll soon learn the twist of it.

Good luck and remember that we are always learning new stuff, so don't worry on question everything.

Alessandra Pereyra
  • 2,640
  • 18
  • 22
0

My recommendation would be to really learn HTML. Use the element that actually relates to the content. If it is a list of items, use one of the list tags. If it is data entry, use a fieldset tag. There aren't that many tags to chose from yet so many are neglected. If you simply replace all your table layout formatting with DIVs, your tag soup might be a little less chunky but you can still choke on it.

Josh
  • 1,001
  • 9
  • 18
0

You should check Elastic CSS Framework you can layout an unlimited combination of columns very easily and position them with its helpers, check out the documentation.

cheers

0

In terms of performance side, table(s) will only get rendered once the end tag () is reached, so if it is a table contains 100s of rows, you will see that table appears in the browser little later. This is not true for DIVs.

asyncwait
  • 4,457
  • 4
  • 40
  • 53
0

I posted on meta-SO about their tables: https://meta.stackexchange.com/questions/3110/when-did-so-start-using-tables-for-layout/3547#3547

In short, I think it's fine since it is tabular data.

Community
  • 1
  • 1
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
  • The structure described by the tables does not match any tabular relationships between the bits of data. They use layout tables, which isn't fine. – Quentin Aug 18 '09 at 16:10
  • How is it not tabular data? As mentioned on the above post, the table data is votes|answer. Why is that not tabular data? – DisgruntledGoat Aug 19 '09 at 09:44
-1

CSS is great and all, but I mainly use it for styling, not layout. I and countless other developers still use tables every day for building web pages, and will for the foreseeable future.

If you want to use absolute and relative positioning for layouts, go for it. If not, you are not evil or stupid for not doing so. The main thing to look out for when using tables is preventing table nesting hell IMO.

Chrisb
  • 729
  • 1
  • 5
  • 11
  • Keep in mind that the "countless other developers" who use tables for layout do so chiefly because they haven't had time or simply don't want to learn to do layouts with divs and other semantic CSS tags. It does take a little more thought, but there are real benefits to using CSS, like better download and rendering speeds. And no, you're not evil or stupid if you don't use CSS layouts--just less efficient. – Klay Aug 18 '09 at 13:29
  • 1
    Tables shouldn't be used for layouts. They break accessibility and are a PITA to make changes to. There is no excuse to not use divs and CSS for layouts other than sheer laziness. – Corey D Aug 18 '09 at 13:30
  • Ah yes, the laziness argument. Pffft. – Chrisb Aug 18 '09 at 13:41
  • 2
    You don't have a foreseeable future if you use tables for layout. BTW, it's very rare that one needs to use absolute and relative positioning for layout, either. You should learn how to do this stuff before dismissing it. – NickFitz Aug 18 '09 at 15:18
  • I never dismissed it. The "Tables are the devil" zealotry is funny, thats all. There is more than one way to skin a cat. Calling people lazy, or suggesting that they aren't going to have a job soon because they use tables is silly. – Chrisb Aug 18 '09 at 15:59