tags:

views:

375

answers:

6

Is it ok to use tables to make web forms or should I use div's? I know how to use tables, but how should I make form with div's or is it better to use tables?

<form method="post">
    <table>
        <tr>
            <td>
                <label for="username">Username:</label>
            </td>
            <td>
                <input type="text" name="username" id="username"/>
            </td>
        </tr>
        <tr>
            <td>
                <label for="password">Password:</label>
            </td>
            <td>
                <input type="password" name="password" id="password"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="cancel" value="Cancel"/>
            </td>       
            <td>
                <input type="submit" name="send" value="Send"/>
            </td>
        </tr>
    </table>
</form>

Possible Duplicate: Why-not-use-tables-for-layout-in-html

+5  A: 

Don't use tables, that's a very brittle, non-semantic layout, use proper CSS layout. Below are some presentations that explain some good approaches to form layout, including usability considerations. The first link is more about the code, and the second more about design and usability considerations:

RedFilter
yes divs are easy to use, and valid, and also better... +1
ahmet2106
There are good reasons to use div-based layouts, but they're *way* more brittle than tables. Tables are much more predictable, controllable and consistent across browsers.
Draemon
@Draemon: I disagree, e.g., if you have your labels to the left of inputs, and are asked to put them above the inputs instead, you need to reconstruct your whole table, which is a huge PITA. With CSS, you can make a one line change and you are done.
RedFilter
And if you're asked to put them on the right? Or beneath? In general you can't make such layout changes in CSS alone. If your source is formatted sanely it's a 2 minute job to rearrange in any decent editor, with either method, definitely not a "huge PITA".
Draemon
+2  A: 

There is no such hard and fast rule or better way of doing forms in HTML. If you want to use div's in an easy way, its better to choose a CSS framework to make things easy like blueprint

Teja Kantamneni
A: 

A rule to live by: Only use tables to display tabular data.

That has always worked well for me....

Ian P
Aren't forms just tabular data where some of the cells/fields are for you to fill in?
Draemon
Eh.. Take it or leave it, I'm just saying it's always worked well for me :)
Ian P
To clarify -- I do not consider a form as being tabular data.
Ian P
Tabular implies repetition of a business data type, such as Customer. A table has many Customer objects. A name-value pair is a view (view model maybe) data type, not a business data type.
ProfK
+2  A: 

The absolutely best format for forms in my opinion is to use unordered lists inside fieldsets spiced up with labels. That's the most semantically correct way, anyways:

<form method="post" action="foo.php">
    <fieldset>
        <legend>Some fields</legend>
        <ul>
            <li>
                <label for="foo">Foobar</label>
                <input type="text" name="foo" id="foo" />
            </li>
        </ul>
    </fieldset>
</form>

The fieldsets aren't mandatory but can liven up an otherwise dull form. The basic CSS to get an ul look like a form might be something like this:

form ul {
    list-style: none;
    margin: 0;
}

form ul li {
    margin-bottom: 10px;
}

form ul li label {
    display: block;
    float: left;
    width: 150px;
    line-height: 24px;
}
Tatu Ulmanen
Doesn't that get awkward (or lose some of its elegance) if/when there are multiple columns?
RedFilter
Forms shouldn't have multiple columns, that's not very user friendly. You can easily put several inputs next to each other for example for first and last name, though.
Tatu Ulmanen
Please tell my clients that "forms shouldn't have multiple columns". HA!
Diodeus
A: 

For best HTML/CSS practices in general, I recommend to have a look at A List Apart. With regard to forms, here's an article which suits your need: Prettier Accessible Forms. For other examples, just google with keywords "semantic html form".

BalusC
A: 

Tables are not for layout, tables are for data period, css is the way to go, that is best practice.

James Campbell