Form Control Layout using CSS
Prior to CSS, tables were used to control the layout of form control elements. In fact it is not uncommon
today to find form controls organized on a page using tables.However, the table approach is outdated and
does not provide support for users with accessibility issues. An alternative and modern approach is to use
CSS to format and display form controls. The advantage to using CSS is that it requires less code -
elimination of table tags and the line break tag. The code below demonstrates how to layout form controls
using CSS.
<style>
form {padding:10px}
label {width: 120px; float:left; text-align:right;
margin-top: 10px; padding-right:10px;clear:left}
input {margin-top: 10px;display:block}
#mySubmit {margin-top:10px;padding-bottom:10px}
</style>
-----------------------------------------------
<div>
<form>
<label for="FName">
First Name:</label>
<input type="text" id="FName">
<br>
<label for="LName">
Last Name:</label>
<input type="text" id="LName">
<input type="submit" value="Submit Form">
</form>
</div>
Using the CSS method, label element selectors are key to aligning the text - this selector is configured to
on the left with the text aligned to the right. The float causes the input controls to wrap around the right
side of the labels. The input controls are also configured with a top margin. The submit button is styled
using the "mySubmit" id selector. Left margin and bottom padding are applied to the submit button.
A more complex form is shown below. Notice that the same CSS can be used to control the layout of the form.