Lists may seem to be mundane items, but many pages on the Web include at least one. The way you choose to mark up these lists can make a big difference. This article explores several the advantages and disadvantages of several common markup methods. It is taken from chapter one of Dan Cederholm's book Web Standards Solutions: The Markup and Style Handbook (Apress, 2004; ISBN: 1590593812).
Most competent browsers will insert a bullet to the left of a list item when the <li> element is used. One might use Method B to achieve those results, adding the <li> by itself when a bullet is desired. However, some of those same competent browsers wonít display the bullet when an <li> element isnít contained within one of its proper parent, the mighty <ul>. The <li>ís other parent is the <ol> element, for ďordered lists,Ē which Iíll discuss further on in the book.
The bullet does help the wrapping issue to a certain extent. A new grocery item would be signified by a bullet, to its left. If an item wraps to the next line, the absence of a bullet should be enough to distinguish itself from being a whole new item. But there is something else wrong with Method B, aside from its resulting display: Itís not valid.
According to the W3Cís XHTML 1.0 specification, all tags must eventually closeóand if we were to go ahead and open an <li> for each grocery item, without closing it at the other end as in the example, shame on us!
Weíve mimicked the automatic line-breaking that occurs when a proper unordered list is used by adding the <br /> tag at the end. But thereís a better way.
Itís valuable to get used to the idea of writing valid markup, consistently. By ensuring our markup is valid, weíll worry less about problems that may occur because of unclosed or improperly nested elements in the future. Not to mention that if anyone else is looking at our code, itís easier for everyone involved to dive in and understand exactly whatís going on.
Be sure to use the W3Cís online validation tool (http://validator.w3.org/) to validate your files by URI or file upload. Youíll be happy you did in the long run.
This chapter is from Web Standards Solutions: The Markup and Style Handbook by Dan Cederhold (Apress, 2004, ISBN: 1590593812). Check it out at your favorite bookstore today. Buy this book now.