Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout out data-entry form in HTML: table or no table?

Tags:

html

I want to create a data-entry form like the following:

Name:    [ Name textbox ]
Age:     [ Age textbox  ]
label n: [ textbox n    ]

Where the labels left-align, and the textboxes left-align. I know I can do this in a table element, but I'm also aware that "tables should only be for tabular data". While I part agree/disagree with that statement - I'd like to know whether my desired layout could/should be considered "tabular data", and what an alternative layout would be to produce the same results without dozens of lines of complicated cross-browser CSS.

I don't do web development much at the moment (strictly WinForms for some time now when I do UI work), so I appreciate there may be an elegant solution. Possibly involving an unordered list with the bullet points turned off and a bit off label->field y position offsetting, perhaps?

like image 884
Neil Barnwell Avatar asked Sep 27 '10 12:09

Neil Barnwell


People also ask

Should I use tables for layout HTML?

HTML tables were originally intended to be used for presenting tabular data, not for layout. The World Wide Web Consortium (W3C®) discourages use of tables for layout because they are striving for a web in which content and structure are completely separate from presentation.

Should I use table for form?

Tables should never be used for design. They are used for tabular data display, only. If you are wanting to display tabular data in a form, then that is ok, but incorrect still. Tables are used to display the result of something, an output of data, whereas forms are used for input of data.

How do you make a table without a table in HTML?

So, when creating a table, all you need to do is, instead of the HTML 'table' tag, merely use the 'div' tag and add the corresponding CSS to display a table.

What can I use instead of a table in HTML?

Replace HTML tables with <div>s. In the era of responsive web design the old trend of building websites using HTML tables can't be used anymore. You have to use div tags and style them as required.


2 Answers

Unordered lists with label elements should be the way to go here. The markup I would use should look something like:

<form id="person" method="post" action="process.php">
    <ul>
        <li><label for="name">Name: </label><input id="name" name="name" type="text" /></li>
        <li><label for="age">Age: </label><input id="age" name="age" type="text" /></li>
        <li><label for="n">N: </label><input id="n" name="n" type="text" /></li>
    </ul>
</form>

And this CSS if to get something similar to want you asked for:

#person ul {
    list-style: none;
}

#person li {
    padding: 5px 10px;
}

#person li label {
    float: left;
    width: 50px;
    margin-top: 3px;
}

#person li input[type="text"] {
    border: 1px solid #999;
    padding: 3px;
    width: 180px;
}

See: http://jsfiddle.net/tZhUQ/1 , which contains some more interesting stuff you can try.

like image 56
Yi Jiang Avatar answered Oct 12 '22 11:10

Yi Jiang


Webcredible have an example, just remove the text-align: right to get what you appear to be looking for.

Smashing Magazine and Design Shack have collated some examples.

like image 28
Quentin Avatar answered Oct 12 '22 12:10

Quentin