Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style and align forms without tables?

I've gotten used to using <table>s for aligning my form fields perfectly. This is how I commonly write my forms:

<table border="0">    <tr>      <td><label for="f_name">First name:</label></td>      <td><input type='text' id='f_name' name='f_name' /></td>      <td class='error'><?=form_error('f_name');?></td>    </tr> </table> 

I know this is bad practice, and I want to use CSS, <label>s, <div>s, or a cleaner method. However, the fact is, <table>s work extremely well for the forms. Everything is aligned exactly right, the spacing is perfect, all errors exactly below each other, etc.

I recently tried using <dt> and <dd> tags for a form, but I ended up reverting back to tables just because they looked so much better.

How can I get this kind of aligned table layout without using <table>s?

like image 320
Ali Avatar asked Feb 26 '09 17:02

Ali


People also ask

How do you properly align a form?

Use the CSS text-align Property to Center a Form in HTML The text-align property takes the values like left , right , center , justify , etc. We can set the value to center to center the form. For example, apply the text-align property to the form tag in the style attribute, and set the property to center .

Can you style a form element?

The only way to style these elements is to use custom controls, which are created using stylable HTML elements such as div , span , etc. For example, when styling <input type="file"> , we can hide the default input and use a custom button.

How do you align a box in HTML without using CSS?

Using the table tag without borders allows one to align textboxes without css.


1 Answers

This might not get a lot of support but here's my two cents:

In some situations tables are easier for layout; such as three columns or forms (albeit there are some great suggestions here for doing a pure css form layout so don't ignore those either.)

Processes and methodologies can make good servants but are poor masters.    - Mark Dowd, John McDonald & Justin Schuh       in "The Art of Software Security Assessment" 

I believe that this quote very strongly applies to this situation. If your table layout is working for you, not causing accessibility issues and isn't broken - then don't fix it.

Phrases like: "you should", "must", "always" - make me scared, because one-size-doesn't-fit-all! Take zealots with a grain of salt.

like image 90
Gavin Miller Avatar answered Sep 21 '22 08:09

Gavin Miller