I'm wondering whether it's acceptable to use table
s for forms.
Strictly speaking, name/value pairs are tabular data, aren't they? And a form is just a user customisable set of name/value pairs. So is it right to use table
s in this case? Or should I use div
s styled with CSS?
Using tables for a form is a big no-no. There is absolutely no need to put a form in tables. Both aren't correct.
You can have an entire table inside a form. You can have a form inside a table cell. You cannot have part of a table inside a form. Use one form around the entire table.
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.
The table element is still the correct way to provide tabular data in a semantically correct way in HTML. So if you use it semantically correct it is fine and not outdated per se.
I prefer to break up the fields into logical <fieldset>
s with one <legend>
each, because:
Here's a code example. Note that the labels' for
attribute lets you click that label to move focus to the input specified (it matches the id
attribute).
<form> <fieldset> <legend>Wombat Statistics</legend> <ol> <li> <label for="punchstrength">Punch Strength</label> <input id="punchstrength" name="punchstrength" /> </li> <li> <label for="beverage">Favorite Beverage</label> <input id="beverage" name="beverage" /> </li> </ol> </fieldset> <fieldset> <legend>Questions That Are Too Personal</legend> <ol> <li> <label for="creditcard">What is your credit card number?</label> <input id="creditcard" name="creditcard" /> </li> <li> <label for="gullibility">Did you actually fill that in?</label> <input id="gullibility" name="gullibility" /> </li> </ol> </fieldset> </form>
For a basic layout, you can use something like:
label, input, textarea, select { display: inline-block; vertical-align: top; width: 30%; }
See this article for a more in-depth tutorial.
Both are correct.
I preffer using some div
/li
, as that allows me to make some different layouts, but tables
for forms
are not frowned upon.
Actually, by default, Django gives you table formated forms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With