• google plus

How to Create a Form without Tables Using CSS

By using CSS to apply styles to the form, fieldset, and legend elements, you can layout a form without using tables. Here's how to do it:

  1. Create a new HTML file and paste in the following markup:
    
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="UTF-8">
    <link type="text/css" rel="stylesheet" href="../reset-meyer.css">
    <title>Job Application</title>
    </head>
    <body>
    <h1>Job Application</h1>
    <form method="post" action="apply.php">
    	<fieldset id="jobInfo">
    		<legend>Job Information</legend>
    		<ol>
    			<li>
    				<label for="JobOfInterest">Job of Interest:</label>
    				<select name="JobOfInterest" id="JobOfInterest" title="Job of Interest">
    					<option>Please select...</option>
    					<option>Account Executive</option>
    					<option>Marketing Director</option>
    					<option>Receptionist</option>
    					<option>Sales Manager</option>
    				</select>
    			</li>
    			<li>
    				<label for="ReferralSource">Referral Source:</label>
    				<select name="ReferralSource" id="ReferralSource" title="Referral Source">
    					<option>Please select...</option>
    					<option>Advertisement</option>
    					<option>Employee</option>
    					<option>Employement Agency</option>
    					<option>Website</option>
    				</select>
    			</li>
    			<li>
    				<label for="Availability">Availability:</label>
    				<select name="Availability" id="Availability" title="Availability">
    					<option>Please select...</option>
    					<option>Full-Time</option>
    					<option>Part-Time</option>
    					<option>Seasonal</option>
    					<option>Temporary</option>
    				</select>
    			</li>
    			<li>
    				<label for="Location">Location:</label>
    				<select name="Location" id="Location" title="Location">
    					<option>Please select...</option>
    					<option>California</option>
    					<option>Kansas</option>
    					<option>New York</option>
    					<option>Texas</option>
    					<option>Pennsylvania</option>
    				</select>
    			</li>
    		</ol>
    	</fieldset>
    	<fieldset id="personalInfo">
    		<legend>Personal Information</legend>
    		<ol>
    			<li>
    				<label for="FirstName">First Name:</label>
    				<input type="text" size="20" name="FirstName" id="FirstName" title="First Name">
    			</li>
    			<li>
    				<label for="LastName">Last Name:</label>
    				<input type="text" size="20" name="LastName" id="LastName" title="Last Name">
    			</li>
    			<li>
    				<label for="Address">Street Address:</label>
    				<input type="text" size="20" name="Address" id="Address" title="Address">
    			</li>
    			<li>
    				<label for="City">City:</label>
    				<input type="text" size="20" name="City" id="City" title="City">
    			</li>
    			<li>
    				<label for="State">State:</label>
    				<input type="text" size="20" name="State" id="State" title="State">
    			</li>
    			<li>
    				<label for="PostalCode">Postal Code:</label>
    				<input type="text" size="20" name="PostalCode" id="PostalCode" title="Postal Code">
    			</li>
    			<li>
    				<label for="Telephone">Telephone:</label>
    				<input type="tel" size="20" name="Telephone" id="Telephone" title="Telephone">
    			</li>
    			<li>
    				<label for="Email">Email Address:</label>
    				<input type="email" size="20" name="Email" id="Email" title="Email Address">
    			</li>
    			<li>
    				<label for="Citizenship">Citizenship:</label>
    				<input type="text" size="20" name="Citizenship" id="Citizenship" title="Citizenship">
    			</li>
    			<li>
    				<span class="label">Have you ever been convicted of a felony?:</span>
    				<label><input type="radio" name="Felony" id="FelonyYes" title="Felony Yes"> Yes</label>
    				<label><input type="radio" name="Felony" id="FelonyNo" title="Felony No"> No</label>
    			</li>
    			<li>
    				<span class="label">Are you willing to relocate?:</span>
    				<label><input type="radio" name="Relocate" id="RelocateYes" title="Relocate Yes"> Yes</label>
    				<label><input type="radio" name="Relocate" id="RelocateNo" title="Relocate No"> No</label>
    			</li>
    		</ol>
    	</fieldset>
    	<fieldset id="employmentHistory">
    		<legend>Employment History</legend>
    		<p>Provide your employment history starting with the company for which you have most recently worked.</p>
    		<fieldset>
    			<legend>Company 1</legend>
    			<ol>
    				<li>
    					<label for="CompanyName1">Company Name:</label>
    					<input type="text" size="20" name="CompanyName1" id="CompanyName1" title="Company Name">
    				</li>
    				<li>
    					<label for="JobTitle1">Job Title:</label>
    					<input type="text" size="20" name="JobTitle1" id="JobTitle1" title="Job Title">
    				</li>
    				<li>
    					<label for="StartDate1">Start Date:</label>
    					<input type="date" size="20" name="StartDate1" id="StartDate1" title="Start Date">
    				</li>
    				<li>
    					<label for="EndDate1">End Date:</label>
    					<input type="date" size="20" name="EndDate1" id="EndDate1" title="End Date">
    				</li>
    				<li>
    					<label for="Responsibilities1">Responsibilities:</label>
    					<textarea name="Responsibilities1" id="Responsibilities1" title="Responsibilities" cols="50" rows="3"></textarea>
    				</li>
    				<li>
    					<label for="ReasonForLeaving1">Reason for Leaving:</label>
    					<textarea name="ReasonForLeaving1" id="ReasonForLeaving1" title="Reason for Leaving" cols="50" rows="3"></textarea>
    				</li>
    			</ol>
    		</fieldset>
    		<fieldset>
    			<legend>Company 2</legend>
    			<ol>
    				<li>
    					<label for="CompanyName2">Company Name:</label>
    					<input type="text" size="20" name="CompanyName1" id="CompanyName2" title="Company Name">
    				</li>
    				<li>
    					<label for="JobTitle2">Job Title:</label>
    					<input type="text" size="20" name="JobTitle2" id="JobTitle2" title="Job Title">
    				</li>
    				<li>
    					<label for="StartDate2">Start Date:</label>
    					<input type="date" size="20" name="StartDate2" id="StartDate2" title="Start Date">
    				</li>
    				<li>
    					<label for="EndDate2">End Date:</label>
    					<input type="date" size="20" name="EndDate2" id="EndDate2" title="End Date">
    				</li>
    				<li>
    					<label for="Responsibilities2">Responsibilities:</label>
    					<textarea name="Responsibilities2" id="Responsibilities2" title="Responsibilities" cols="50" rows="3"></textarea>
    				</li>
    				<li>
    					<label for="ReasonForLeaving2">Reason for Leaving:</label>
    					<textarea name="ReasonForLeaving2" id="ReasonForLeaving2" title="Reason for Leaving" cols="50" rows="3"></textarea>
    				</li>
    			</ol>
    		</fieldset>
    		<fieldset>
    			<legend>Company 3</legend>
    			<ol>
    				<li>
    					<label for="CompanyName3">Company Name:</label>
    					<input type="text" size="20" name="CompanyName3" id="CompanyName3" title="Company Name">
    				</li>
    				<li>
    					<label for="JobTitle3">Job Title:</label>
    					<input type="text" size="20" name="JobTitle3" id="JobTitle3" title="Job Title">
    				</li>
    				<li>
    					<label for="StartDate3">Start Date:</label>
    					<input type="date" size="20" name="StartDate3" id="StartDate3" title="Start Date">
    				</li>
    				<li>
    					<label for="EndDate3">End Date:</label>
    					<input type="date" size="20" name="EndDate3" id="EndDate3" title="End Date">
    				</li>
    				<li>
    					<label for="Responsibilities3">Responsibilities:</label>
    					<textarea name="Responsibilities3" id="Responsibilities3" title="Responsibilities" cols="50" rows="3"></textarea>
    				</li>
    				<li>
    					<label for="ReasonForLeaving3">Reason for Leaving:</label>
    					<textarea name="ReasonForLeaving3" id="ReasonForLeaving3" title="Reason for Leaving" cols="50" rows="3"></textarea>
    				</li>
    			</ol>
    		</fieldset>
    	</fieldset>
    	<fieldset id="buttons">
    		<input type="reset" id="Reset" name="Reset" value="Start Over">
    		<input type="submit" id="Submit" name="Submit" value="Submit">
    	</fieldset>
    </form>
    </body>
    </html>
    	
  2. Use a CSS reset to remove browser-specific styles from the form. Eric Meyer's Reset CSS works well:
    /*
    	This is Eric Meyer's Reset CSS, which he has made available for free at http://meyerweb.com/eric/tools/css/reset/index.html.
    */
    /* v1.0 | 20080212 */
    
    html, body, div, span, applet, object, iframe,
    h1, h2, h3, h4, h5, h6, p, blockquote, pre,
    a, abbr, acronym, address, big, cite, code,
    del, dfn, em, font, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var,
    b, u, i, center,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td {
    	margin: 0;
    	padding: 0;
    	border: 0;
    	outline: 0;
    	font-size: 100%;
    	vertical-align: baseline;
    	background: transparent;
    }
    body {
    	line-height: 1;
    }
    ol, ul {
    	list-style: none;
    }
    blockquote, q {
    	quotes: none;
    }
    blockquote:before, blockquote:after,
    q:before, q:after {
    	content: '';
    	content: none;
    }
    
    /* remember to define focus styles! */
    :focus {
    	outline: 0;
    }
    
    /* remember to highlight inserts somehow! */
    ins {
    	text-decoration: none;
    }
    del {
    	text-decoration: line-through;
    }
    
    /* tables still need 'cellspacing="0"' in the markup */
    table {
    	border-collapse: collapse;
    	border-spacing: 0;
    }
    
    
  3. Open the web page in a browser. It will look like the following:
    unstyled form
  4. Let's start by looking at the complete style sheet and then we'll dissect it:
    
    body {
    	background-color: #ccc;
    }
    
    h1 {
    	width: 555px;
    	margin: 5px auto;
    	padding-bottom: 10px;
    	border-bottom: 1px solid #930;
    	font-size: xx-large;
    	color: #930;
    }
    
    /*Begin Form Styles*/
    form {
    	width: 555px;
    	margin: auto;
    	padding-bottom: 20px;
    	color: #930;
    }
    
    fieldset {
    	margin-bottom: 10px;
    	border-bottom: 1px solid #930;
    	background-color: #eec;
    }
    
    legend {
    	padding-left: 10px;
    	font-size: 1.2em;
    	font-weight: bold;
    	letter-spacing: .5em;
    	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    	font-variant: small-caps;
    	color: #460;
    }
    
    fieldset fieldset {
    	margin-top: 10px;
    	border-bottom: none;
    }
    
    fieldset fieldset legend {
    	font-size: 1em;
    	font-weight: bold;
    	letter-spacing: .4em;
    	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    	color: #333;
    }
    
    form ol {
    	padding: 20px;
    }
    
    fieldset fieldset ol {
    	margin-top: -10px;
    }
    
    form p {
    	margin: 10px;
    	font-size: .8em;
    }
    
    form li {
    	list-style-type: none;
    	border-bottom: 1px dotted #000;
    	padding-bottom: 2px;
    	margin-bottom: 2px;
    }
    
    label {
    	width: 200px;
    	float: left;
    	font-weight: bold;
    }
    
    span.label {
    	font-weight: bold;
    }
    
    li.radioContainer label {
    	display: block;
    	float: none;
    	font-weight: normal;
    }
    
    select, input[type="text"] {
    	width: 200px;
    }
    
    textarea {
    	width: 400px;
    	height: 50px;
    }
    
    #buttons {
    	text-align: center;
    }
    
    #buttons input {
    	height: 50px;
    	width: 150px;
    	font-size: 1.5em;
    	color: #930;
    	font-variant: small-caps;
    	margin: 10px;
    }
    /*End Form Styles*/
    

    Most of the CSS should be pretty easy to follow. Here are some things to notice:

    1. The rule below takes away the bottom border from fieldsets that are nested within other fieldsets:
      
      fieldset fieldset {
      	margin-top: 10px;
      	border-bottom: none;
      }
      
    2. The selector fieldset fieldset legend only applies to legends inside of fieldsets that are nested within other fieldsets. This is to make the "headings" of form subsections appear differently from the headings of the main sections.
    3. The rule below removes the numbers from the list items, adds a dotted line beneath each "question", and creates some vertical space between the "questions."
      
      form li {
      	list-style-type: none;
      	border-bottom: 1px dotted #000;
      	padding-bottom: 2px;
      	margin-bottom: 2px;
      }
      
    4. The float: left; declaration changes the element from inline to block, which will bring its associated form control up to the right of the label:
      
      label {
      	width: 200px;
      	float: left;
      	font-weight: bold;
      }
      
    5. This rule turns off the float property for labels that appear in list items with the "radioContainer" class. Since the label is not floated anymore it will revert back to displaying inline. To keep it displayed on a separate line we add in the display:block; setting:
      
      li.radioContainer label {
      	display: block;
      	float: none;
      	font-weight: normal;
      }
      

      Remember, these are the label elements that surround the radio button and its presentation value, like this:
      
      <li class="radioContainer">
      <span class="label">Have you ever been convicted of a
      felony?:</span>
      <label><input type="radio" name="Felony" id="FelonyNo" ti »»
      tle="Felony Yes" /> Yes</label>
      <label><input type="radio" name="Felony" id="FelonyYes" ti »»
      tle="Felony No" /> No</label>
      </li>
      	
    6. This rule sets a width for the select and text input elements:
      
      select, input[type="text"] {
      	width: 200px;
      }
      	
    7. This rule sets a height and width for the textareas, overriding the cols and rows values:
      
      textarea {
      	width: 400px;
      	height: 50px;
      }
      
    8. And these last two rules style the buttons:
      
      #buttons {
      	text-align:center;
      }
      #buttons input {
      	height: 50px;
      	width: 150px;
      	font-size: 1.5em;
      	color: #930;
      	font-variant: small-caps;
      	margin: 10px;
      }
      

      Although there are other ways to layout the HTML for forms, we like the use of an ordered list, as it makes sense semantically and doesn't require extraneous tags used for formatting purposes only.

    Styled form
  5. Save the finished CSS file as style.css and link it to the HTML page:
    <link type="text/css" rel="stylesheet" href="style.css">
    	
  6. Open the web page in a browser. The completed and styled form looks like the following:
    Styled form
Author: Chris Minnick
Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.
About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, XML, Windows, Java, Adobe, HTML5, JavaScript, Angular, and much more. Check out our complete course catalog.