One thing I see far more often than is excusable in code that I inherit is improperly used and structured dictionary lists. Certain elements in HTML are limited to having only a few specific children, and dictionary lists are no exception. Putting incorrect elements as direct children can cause display issues, parsing issues, compatibility issues and headaches for future development (whether it’s a new developer or your own updates). When developing, it should be a priority to use the correct—and only the correct—children in parent elements. Here’s how to properly use dictionary lists.
Dictionary Lists (
The dictionary list element is meant to be used for a list of items containing terms and their definitions. That doesn’t mean you’re restricted to using this element only for word lists. For instance, the structure of the element is perfect for frequenty asked questions (FAQs): the question is a term, the answer is a definition. Dialog is another option: the speaker is the term, the words spoken are the definition.
So, let’s repeat: dictionary lists are lists of items containing terms (
<dt>) and their definitions (
A dictionary list can only have two types of child elements: dictionary term (
<dt>) and dictionary definition (
<dd>). Semantically, each definition can have multiple terms, and each term can have multiple definitions. That said, the first child should always be a term and the last child should always be a definition.
<dd>—that’s it. No other immediate children are allowed. Of course, you can put other elements inside of those two child elements. But even they have restrictions.
Dictionary Terms (
Dictionary term elements can only contain inline elements. That means you can use things like
<span>. You can not use
Dictionary Definitions (
Dictionary definition elements can contain any flow elements— inline or block. That means you can use things like
<div>, etc. You can have a dictionary list inside of a dictionary definition. You can have a form inside of a dictionary definition. You can basically have anything that is allowed inside the
<body> element inside of a dictionary definition.
Valid vs. invalid
The following is an invalid list (with shorthand code showing only elements, not required attributes). Don’t do it.
<dl> <form> <dt>Name</dt> <dd><input></dd> ... </form> <dl>
Here’s a valid list.
<dl> <dt>Ginormous</dt> <dt>Gargantuan</dt> <dd>Both are words that mean “enormous,” but sound way cooler.</dd> <dl>
Get it? Good. Go forth and dev.
What tips do you have for using dictionary lists?