views:

110

answers:

3

I have a section of an html page that I want to display in a table-like way. I keep hearing not to use <table> for layout. And I get the general reasoning.

1) mark up becomes bloated, so it takes longer to download,

2) a table is rendered all at once(so it might delay loading until all of it is read)

3) probably a myriad of other reasons.

I'm leaning toward the "don't use tables as the default layout method, but if it makes sense just do it"

So I would like to see how one would accomplish the following without a table.

<table>
  <tr>
    <td>First Name</td>
    <td><input type="text"/></td>
  </tr>
  <tr>
    <td>Last Name</td>
    <td><input type="text"/></td>
  </tr>
  <tr>
    <td>Phone Number</td>
    <td><input type="text"/></td>
  </tr>
</table>

So the requirements are I want all of the inputs to line up vertically, which means the first "column" must stretch to the widest width of all the "rows"

I don't want to explicitly specify the width of any column

I've read quite a few pages that say just how evil a <table> for layout is which I agree with some points, but I think adding a bunch of divs to imitate a table is kind of stupid as well. By this I mean, having a series of divs with a class with a clear:left in it is harder to read.

Anyways, I really would love to "see the light".

UPDATE:

In response to the answers I think the best implementation for me would be this:

css (which basically requires me to only specify a class on topmost div):

div.table
{
    display:table
}

div.table div
{
    display:table-row
}

div.table div div
{
    padding:5px;
    display:table-cell;
}

and then have a markup like this:

<div class="table">
  <div>
    <div>First Name</div>
    <div><input type="text"/></div>
  </div>
  <div>
    <div>Last Name</div>
    <div><input type="text"/></div>
  </div>
  <div>
    <div>Phone Number</div>
    <div><input type="text"/></div>
  </div>
</div>
+3  A: 

A List Apart has a nice article on the issue.

They use

  • <ul></li>
  • <Fieldset> and
  • <label>

elements - semantically very correct, and even the source code looks nice.

Pekka
Not a bad solution there, although it involves assigning a width to the labels, so it's not quite as solid as a table-based layout
Grant Crofton
@Grant true. I don't see anything really wrong with a table here, either.
Pekka
@Pekka - I disagree; When I'm debating the use of a table, I ask myself whether it would be confusing or annoying for a user with a screenreader to hear something like "open table" in that context. While I'll agree there are exceptions to the rule, I don't feel like this is one.
LeguRi
@Richard you have a point.
Pekka
A: 

The general rule is use a table only for tabular data, and you could reasonably say this is tabular data, so I don't think it's a problem. Even the Head First HTML/CSS book says so!

Grant Crofton
I'm not sure how you can say that an implementation of a form is _tabular data_? By that reasoning you are one step away from using tables for layout again.
w3d
Because it inherently consists of rows and columns. But I'll agree it's a bit of a grey area.. None of the solutions are ideal, not with current browser support anyway.
Grant Crofton
@w3d I'd say not really. To me, this is an edge case where it's acceptable to use a table. However, a `<ul><li><label>` based solution would reduce the markup a bit and make more sense semantically.
Pekka
@Grant it's only rows and columns because that's how it's displayed, if you switched to label-above-input that no longer makes sense and wouldn't be achievable with the same table-based markup.
roryf
+2  A: 

If you want the precise layout of a table, then use 'display: table' and its ilk. Toss in a fallback for older versions of IE and you have the best of both worlds.

Alternatively – this is what I'd do – you could use fixed-width labels and floats. An extra-wide label would simply wrap to the next line.

Thom Smith
Or the worst of both worlds, depending on how you look at it..
Grant Crofton
So what's the advantage to using 'display:table' ? Is is just so you can technically say there is no <table> tag in my markup?
Jose
The benefit is that your markup is clear and semantic. The table tag indicates "this is tabular data", a distinction which is respected by a lot of browsing software, particularly screen readers. If the content is not, in fact, tabular data, then it will be misinterpreted by these clients.By using appropriate markup along with "display: table", you get the visual presentation you want in modern graphical browsers without sacrificing usability for everyone else.
Thom Smith