How to lay out a two-column form with CSS

You can lay out your forms without table. In the markup used to create your form you should wrap each row in a <p> tag>.  The next test file shows this:

 

File test.html

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en-US”>

<head>

<title>How to lay out a two-column form with CSS</title>

<meta http-equiv=”content-type” content=”text/html; charset=utf-8″ />

<link rel=”stylesheet” type=”text/css” href=”test.css” />

</head>

<body>

<form method=”post” action=”test2.html”>

<h1>User registration</h1>

<p>

<label for=”fullname”>Name</label>

<input type=”text” name=”fullname” id=”fullname” />

</p>

<p>

<label for=”company”>Company</label>

<input type=”text” name=”company” id=”company” />

</p>

<p>

<label for=”purpose”>Contact purpose</label>

<select name=”purpose” id=”purpose”>

<option value=”Feedback”>Feedback</option>

<option value=”Technical Support”>Technical Support</option>

<option value=”Other”>Other</option></select>

</p>

<p>

<label for=”subject”>Subject</label>

<input type=”text” name=”subject” id=”subject” />

</p>

<p>

<label for=”message”>Message</label>

<textarea name=”message” id=”message” cols=”30″ rows=”4″ ></textarea>

</p>

<p><input type=”submit” name=”btnSubmit” id=”btnSubmit” value=”Submit” class=”btn” /></p>

</form>

</body>

</html>

You should use the next file for your test:

File test.css

h1 {

font: 1.2em Arial, Helvetica, sans-serif;

}

input.txt {

color: #00008B;

background-color: #FFFFCC;

border: 1px inset #FFCC00;

width: 200px;

}

input.btn {

color: #00008B;

background-color: #FFFFCC;

border: 1px outset #FFCC00;

}

form p {

clear: left;

margin: 0;

padding: 0;

padding-top: 5px;

}

form p label {

float: left;

width: 30%;

font: bold 0.9em Arial, Helvetica, sans-serif;

}

In the code above the <label> tag is used to address our label. Then it floats left and it gives a width value.  As float takes an element out of the document flow, a clear property with the value left should be given to the paragraphs, to ensure that each paragraph starts below the <label> in the preceding paragraph.  A padding-top value is given to the paragraphs, in order to space out the rows.

The next picture shows the result:

Laying out a two-column Web form with CSS

Laying out a two-column Web form with CSS