Sample Form

The elements <fieldset> and <legend> allow you to layout and organise a large form with many different areas of interest in a logical way without using tables. The <fieldset> tag can be used to create boxes around selected elements and the <legend> tag will give a caption to those elements. In this way form elements can be grouped together into identified categories.

The tags <label for> and <id> can be used to associate a text or graphic label with a specific form input (or control). The tags can be used with any of the input elements, for example text boxes, text area, checkboxes and radio buttons; however, the id attribute for each element (checkbox, text field etc) in a document must be unique.

While it is usual for the form label or prompt and the control to be adjacent to each other, or at least in close proximity, it is possible to associate a label with a form control that is placed anywhere on the same Web page.

A <tabindex> can be included, allowing the user to easily move from one form element to the next per the predetermined <tabindex> order established in the code. Tab order ascends from lowest to highest number.

Sample Form

Sample Form

HTML Code

form class="validate" id="sample_form" name="sample_form">
<fieldset class="login">
<legend>Login Details</legend>
<div>
<label for="username">User Name</label> <input id="username" name="username" type="text" />
</div>
<div>
<label for="password">Password</label> <input id="password" name="password" type="password" />
</div>
<div>
<label for="password2">Retype Password</label> <input id="password2" name="password2" type="password" />
</div>
</fieldset>
<fieldset class="contact">
<legend>User Details</legend>
<div>
<label for="firstname">First Name</label> <input id="firstname" name="firstname" type="text" />
</div>
<div>
<label for="lastname">Last Name</label> <input id="lastname" name="lastname" type="text" />
</div>
<div class="radio">
<fieldset>
<legend><span>Gender</span></legend>
<div>
<input id="male" name="gender" value="male" type="radio" /> <label for="male">Male</label>
</div>
<div>
<input id="female" name="gender" value="female" type="radio" /> <label for="female">Female</label>
</div>
</fieldset>
</div>
<div>
<label for="email">Email</label> <input class="email" id="email" name="email" type="text" />
</div>
</fieldset>
<button class="submitBtn" value="submit"><span>Submit</span></button>  
</form>

 CSS Code

#sample_form form {
	background:#fff;
	padding:1em;
	border:1px solid #eee;
}
#sample_form fieldset div {
	margin:0.3em 0;
	clear:both;
}
#sample_form form {
	margin:1em;
	width:27em;
}
#sample_form label {
	float:left;
	width:10em;
	text-align:right;
	margin-right:1em;
}
#sample_form legend {
	color:#0b77b7;
	font-size:1.2em;
}
#sample_form legend span {
	width:10em;
	text-align:right;
}
#sample_form input {
	padding:0.15em;
	width:10em;
	border:1px solid #ddd;
	background:#fafafa;
	font:bold 0.95em arial, sans-serif;
	-moz-border-radius:0.4em;
	-khtml-border-radius:0.4em;
}
#sample_form input:hover, input:focus {
	border-color:#c5c5c5;
	background:#f6f6f6;
} 
#sample_form fieldset {
	border:1px solid #ddd;
	padding:0 0.5em 0.5em;
}
#sample_form .date input {
	background-image:url(../images/calendar-small.gif);
	background-repeat:no-repeat;
	background-position:100% 50%;
}
#sample_form .date fieldset label {
	float:none;
	display:block;
	text-align:left;
	width:auto;
}
#sample_form .date fieldset div {
	float:left;
	clear:none;
	margin-right:0.2em;
}
#sample_form .radio, #sample_form .date {
	position:relative;
}
#sample_form .radio fieldset, #sample_form .date fieldset {
	border:none;
	width:auto;
	padding:1px 0 0 11em;
}
#sample_form .radio legend, #sample_form .date legend {
	font-size:1em;
	color:#000;
}
#sample_form .radio legend span, #sample_form .date legend span {
	position:absolute;
	left:0;
	top:0.3em;
	width:10em;
	display:block;
}
#sample_form .radio label, #sample_form .radio input {
	vertical-align:middle;
	display:inline;
	float:none;
	width:auto;
	background:none;
	border:none;
}
#sample_form .radio div {
	float:left;
	white-space:nowrap;
	clear:none;
}
#sample_form .email {
	width:14em;
}
#sample_form input.default {
	color:#bbb;
}
#sample_form button {
  color: #036;
  font-weight: bold;
}

 

Scroll Up