I have been meaning to write a post for a while about definition lists but have just not got round to it. Instead of covering just definition lists though, I thought I would make it a little more specific and talk about a very good use for them, styling forms.
It was not that long ago that I hadn’t even heard of a definition list, now this is one of the most versatile tools in the tool box that I use daily. Definition lists are good for a variety of uses, including Faq’s, forms, images with descriptions or replacing unordered lists for drop down menus to name but a few.
First a little introduction to the definition list for those that may not be aware. Definition lists are in the same list family as the un-ordered list (<ul>) and ordered list (<ol>) but they differ a little in their structure. Unlike the un-ordered and ordered list, the definition list does not have <li> tags but is a pairing of a <dt> tag and one, or multiple <dd> tags. These tags are wrapped within the <dl> tag.
An Example
An example of the definition list structure is as so:
<dl>
<dt>Definition</dt>
<dd>A statement of the exact meaning of a word, esp. in a dictionary.</dd>
<dd>The degree of distinctness in outline of an object, image, or sound, esp. of an image in a photograph or on a screen.</dd>
</dl>
As you can see the <dd> tags describe whats in the <dt> tag.
So why do I think that definition lists are the answer for forms? Well, forms are normally made up of two things, a label and an input field. The label is related to the input field, this makes definition lists perfect if you ask me. This may not exactly be the original purpose for a definition list as the dd element is supposed to be a definition description but it works perfectly for forms as the <dt> and <dd> tags are paired, just like the label and input fields are.
Whats the point of adding extra markup when you can just style the label and input tags?
Well adding the extra X/HTML gives the document structure, especially so when there is no style sheet applied. I also find that less CSS styles are required to get a decent looking form, much better than when you have to style all the other form elements.
Ok Ok all these words are hurting my eyes, example please
A simple contact form is normally only about four fields and in its basic, valid markup, looks something like this:
<form method="post" action="#">
<fieldset>
<label for="firstname">First Name:</label>
<input type="text" id="firstname" />
<label for="lastname">Last Name:</label>
<input type="text" id="lastname" />
<label for="email">Email Address:</label>
<input type="text" id="email" />
<label for="comments">Comments:</label>
<textarea rows="5" cols="30" id="comments"></textarea>
<input type="submit" value="Submit" />
</fieldset>
</form>
If you take a look at the definition list - basic form example, you will see how how it looks with no styles added.
Now lets mark-up the form with a definition list
<form method="post" action="#">
<fieldset>
<dl>
<dt><label for="firstname">First Name:</label></dt>
<dd><input type="text" id="firstname" /></dd>
<dt><label for="lastname">Last Name:</label></dt>
<dd><input type="text" id="lastname" /></dd>
<dt><label for="email">Email Address:</label></dt>
<dd><input type="text" id="email" /></dd>
<dt><label for="comments">Comments:</label></dt>
<dd><textarea rows="5" cols="30" id="comments"></textarea></dd>
</dl>
<input type="submit" value="Submit" />
</fieldset>
</form>
Now we have added a little more structure to the form, take a look at the second, definition form example, still with no styles added.
Even without any styles applied to the form, it still looks well structured. All we need to do is add a few styles to the form and we can make it look a little easier on the eye.
If we want to make the form sit a little better all that is required is to add the following code:
form dt{
float:left;
width:150px;
}
Simple, we have floated the <dt> left, enabling the label and input to sit side by side and by adding a width we have added some space between the label and input fields. A very basic but structured form. If you wanted to have a little space between the input fields just add a margin to the bottom of the <dd>.
form dd{
margin-bottom:6px;
}
I’ve also added a width to the form just to make it sit a little better and not sprawl all over the page,definition list - example form with styles.
There it is, a very basic form but with a lot more structure added thanks to the definition list and with minimal CSS to make it look neater, much easier on the eye.
Posted by : Aaron Witherow, in Tutorials |
Comments
1 Andy Smith
Hmmm, An interesting set of points Mr Witherow.
But If I get your meaning Are the Semantics of this block of code are a little amiss?? The DD element should be providing the definition of the Term in the DT. If used here the DD is actually just a container for the user to insert the answer.
On the flip side, perhaps having value attributes on the input elements is semantically correct (although technically not syntactically correct).
To be honest it’s certainly no worse than being forced to contain your form elements in a paragraph tag which is also in my opinion semantically incorrect.
Either way, good thinking Batman!
2 Aaron Witherow
I agree Andy that the use of a definition list to markup a form is not completely its intended use.
But then again the input field does describe the term but when its filled in and not in its basic form.
The problem is that we use X/HTML in a way that it was never intended to be used and we have to balance that with semantics, design, accessibility and function, not to mention the constraints we have placed upon us by browsers.
Should we use an empty span tag to provide an accessible, usable image replacement technique? or should we throw it out the window in the name of semantics. In my opinion we have to balance it and I feel the same about validation.
I think a definition list provides good structure to a form and also compliments CSS based designs as it is still completely customisable from the style sheet.
Some Rules :Only Basic HTML is allowed in comments, keep on topic, leave 5 mins between posts, comments expire in 30 days