Source: How to structure an HTML form, by Mozilla Contributors
- To start with, make a local copy of blank template file and the CSS for our payment form in a new directory on your computer.
- First of all, apply the CSS to the HTML by adding the following line inside the HTML
<head>
:
<link href="payment-form.css" rel="stylesheet">
- Next, start your form off by adding the outer
<form>
element:
<form>
</form>
- Inside the
<form>
tags, start by adding a heading and paragraph to inform users how required fields are marked:
<h1>Payment form</h1>
<p>Required fields are followed by <strong><abbr title="required">*</abbr></strong>.</p>
- Next we'll add a larger section of code into the form, below our previous entry. Here you'll see that we are wrapping the contact information fields inside a distinct
<section>
element. Moreover, we have a set of two radio buttons, each of which we are putting inside its own list (<li>
) element. Last, we have two standard text<input>
s and their associated<label>
elements, each contained inside a<p>
, plus a password input for entering a password. Add this code to your form now:
<section>
<h2>Contact information</h2>
<fieldset>
<legend>Title</legend>
<ul>
<li>
<label for="title_1">
<input type="radio" id="title_1" name="title" value="M." >
Mister
</label>
</li>
<li>
<label for="title_2">
<input type="radio" id="title_2" name="title" value="Ms.">
Miss
</label>
</li>
</ul>
</fieldset>
<p>
<label for="name">
<span>Name: </span>
<strong><abbr title="required">*</abbr></strong>
</label>
<input type="text" id="name" name="username">
</p>
<p>
<label for="mail">
<span>E-mail: </span>
<strong><abbr title="required">*</abbr></strong>
</label>
<input type="email" id="mail" name="usermail">
</p>
<p>
<label for="pwd">
<span>Password: </span>
<strong><abbr title="required">*</abbr></strong>
</label>
<input type="password" id="pwd" name="password">
</p>
</section>
- Now we'll turn to the second
<section>
of our form โ the payment information. Here we have three distinct widgets along with their labels, each contained inside a<p>
. The first is a drop down menu (<select>
) for selecting credit card type. the second is an<input>
element of type number, for entering a credit card number. The last one is an<input>
element of typedate
, for entering the expiration date of the card (this one will come up with a date picker widget in supporting browsers, and fall back to a normal text input in non-supporting browsers). Again, enter the following below the previous section:
<section>
<h2>Payment information</h2>
<p>
<label for="card">
<span>Card type:</span>
</label>
<select id="card" name="usercard">
<option value="visa">Visa</option>
<option value="mc">Mastercard</option>
<option value="amex">American Express</option>
</select>
</p>
<p>
<label for="number">
<span>Card number:</span>
<strong><abbr title="required">*</abbr></strong>
</label>
<input type="text" id="number" name="cardnumber">
</p>
<p>
<label for="date">
<span>Expiration date:</span>
<strong><abbr title="required">*</abbr></strong>
<em>formatted as mm/yy</em>
</label>
<input type="text" id="date" name="expiration">
</p>
</section>
- The last section we'll add is a lot simpler, containing only a
<button>
of typesubmit
, for submitting the form data. Add this to the bottom of your form now:
<p> <button type="submit">Validate the payment</button> </p>
This is what the end result should look like: