[Meta-note:] I was browsing the question page, getting really tired of "DIVS vs Tables" "When to use tables vs DIVS" "Are Divs better than Tables" "Tables versus CSS" and all the questions that ask THE SAME THING OMG PEOPLE but I would like to see all the ways people tackle the translation of the canonical example of "why you should give up and use tables":
<table>
<tr>
<td> Name </td>
<td> <input> </td>
</tr>
<tr>
<td> Social Security Number </td>
<td> <input> </td>
</tr>
</table>
Question: How to best (semantically, simply, robustly, fluidly, portably) implement the above without tables. For starters, I guess a naive implementation uses a fixed column width for the first column, but that can have iffy results for dynamically generated content. Including strengths/weaknesses of your approach in the answer would be nice.
P.S. Another one I wonder about a lot is vertical centering but the hack for that is covered pretty well at jakpsatweb.cz
EDIT: scunlife brings up a good example of why I didn't think out the problem that carefully. Tables can align multiple columns simultaneously. The Question still stands (I'd like to see different CSS techniques used for alignment/layout) - although solutions that can handle his? more involved example definitely are preferred.
Because a div element marks up only one block at a time, the code base is much smaller than that of a table-based structure. Less code is code that is more readable, easier to maintain, faster to develop, less buggy, smaller in size, you get the point. You want as little code as possible to do the job right.
<form> is what you use to create a form for people to fill in. Div is used to divide sections of the web page up.
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. This feature of the HTML Cleaner offers you a simple way to replace all table tags with div tags having the correct classes.
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.
What I usually do is :
<form>
<label for="param_1">Param 1</label>
<input id="param_1" name="param_1"><br />
<label for="param_2">Param 2</label>
<input id="param_2" name="param_2"><br />
</form>
and in a CSS :
label,input { display: block; float: left; margin-bottom: 1ex; }
input { width: 20em; }
label { text-align: right; width: 15em; padding-right: 2em; }
br { clear: left; }
Of course, you'll have to define the width according to your actual data :-)
display: block
, so that it can be assigned a size and be lined up.float: left
because Explorer does things a bit differentlybr
so that there's a clear: left
somewhere, and I remember that putting it on the label didn't work on some browser.Plus, with the br
you get a nice formatting even if the browser does not support CSS :-)
The trick is when the form gets more complicated than your sample, you realize that tables enable a "flexible grid" that no other elements do.
e.g. what if the "input" is more complicated than a text box? e.g. a bunch of radio buttons, each with their own label:
Color: [____Red___][v]
Hood: [*]
Size: (_) Small
(_) Medium
(_) Large
(*) X-Large
If all you need are simple forms, CSS is great, but as soon as you need a grid, things get interesting...
If you really want to do this, I would check out The Man In Blue's Solution, it works pretty well and is very clean.
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