Quiz Time

 < Day Day Up > 



Which of the following would be best for marking up a grocery list?

Method A: The <br /> Breakdown

 Apples<br /> Spaghetti<br /> Green Beans<br /> Milk<br /> 

Method A is certainly one that's been used for years, heavily, on perhaps millions of web pages. In fact, I'm sure we're all guilty of using this approach at one time or another, right? We'd like each item in the list to be on its own line, and by inserting a break tag (using the valid XHTML, self-closing version here, <br />) a line break will be added after each item. That's about all it does, and it seems to work.

However, what if we wanted to style the grocery list differently from other elements on the page? For instance, what if we would like this list to have red links instead of the default blue, or a different font size from the rest of the text on the page? We really can't. We're stuck with whatever default font styles we've set for the entire document (if there are any at all), and since there's no surrounding element for the list, we can't assign it any unique CSS rules.

It's a Wrap

Let's also say that we added a particularly long grocery item to the list: "Five Foot Loaf of Anthony's Italian Bread". Depending on where this list is placed in the layout of the page, long items may run the risk of wrapping to the next line if there isn't enough horizontal space, or if the user's browser window width is narrow.

It would be also be nice to take into account the possibility of low-vision users increasing their default text size to gain readability. Line items that we thought fit just great in a narrow column, as in Figure 1-1, now break in unpredictable places, as in Figure 1-2, throwing off the design when the text size is increased by the user.


Figure 1-1: An example with default text size


Figure 1-2: The same example with increased text size

Hmm. Now, I know I'm supposed to buy bread, but the two lines that precede it in Figure 1-2 are a bit confusing.

A similar wrapping dilemma rears its ugly head when long lines are viewed on the small screen of a device such as a phone or Personal Digital Assistant (PDA). The ultimate technophile may stroll into the supermarket with Palm Pilot in hand, rather than the traditional sheet of paper for their shopping list, yet they eventually wander aimlessly, looking up and down the aisles for "Anthony's Italian."

I'm essentially proving a point here—that using Method A doesn't take into account the fluidity that web pages can have depending on variables that are outside the designer's control.

Method B: The Bullet that Bites

 <li>Apples<br /> <li>Spaghetti<br /> <li>Green Beans<br /> <li>Milk<br /> 

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.

Validation, Please

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.

Method C: Getting Closer

 <li>Apples</li> <li>Spaghetti</li> <li>Green Beans</li> <li>Milk</li> 

Method C brings us closer to a preferable solution, but fails miserably in one potentially obvious way: It's still not valid markup.

We've closed each <li> tag properly, and since they are block-level elements, using them eliminates the need for a <br /> tag, putting each list item on its own line. But, we're missing an outer layer of structure, lacking a containing element that denotes "This group of items is a list!"

It's also important to view this from a semantic angle as well—that the list is a group of items that belong together, therefore they should be denoted as such. Furthermore, using proper list tags says very clearly to the browser, software, or device, "This group of items is a list!" A good example of how semantic markup is about structuring items for what they are.

start sidebar

Block level vs. inline: HTML elements can inherently be either block level or inline. Block-level elements begin on their own line, followed by a line break, while inline elements are rendered on the same line as other inline elements. Block-level elements can contain other block-level or inline elements, while inline elements can't contain block-level elements.

Some examples of block-level elements include <div>, <h1>-<h6>, <form>. Some examples of inline elements include <span>, <strong>, <em>, <q>.

end sidebar

If we were to look at our grocery list in purely an XML sort of way, we might choose to mark it up as shown in this example:

 <grocerylist>   <item>Apples</item>   <item>Spaghetti</item>   <item>Green Beans</item>   <item>Milk</item> </grocerylist> 

The entire list has a containing element, <grocerylist>, that all of the grocery items belong to. Grouping the items in this manner will make life easier for XML-based applications that may want to extract the items from the list.

For instance, a developer could author an XSLT style sheet that would transform this list of items into XHTML, plain text, or even a PDF document. Because of the predictable nature of a group of list items, software will have an easy time taking the information and doing something useful with it.

While I'm not dealing with XML in this book directly, the principles are carried over to the world of XHTML. Providing a meaningful structure to our markup gains flexibility later on. Whether it be the increased ease of adding CSS to properly structured documents or the improved manageability of making changes to markup that is easy to understand—providing that structure will make for less work later on down the road.

Let's take a close look at Method D and see how this all fits together—providing a structure that the most browsers and devices can read, while also allowing us to style our list in several different ways.

Method D: Wrapper's Delight

 <ul>   <li>Apples</li>   <li>Spaghetti</li>   <li>Green Beans</li>   <li>Milk</li> </ul> 

So what makes Method D so special? First and foremost, it's completely valid. A proper unordered list has a containing <ul> element, with each item within wrapped in opening and closing <li> elements. Now just when you think all we're going for here is demonstrating how to be valid for valid's sake, we'll take a look at it in action.

Because we've properly marked up our grocery list, each item will be on a separate line (due to the block-level nature of the <li>) and most visual browsers will render a bullet next to each item, as well as indent any wrapping lines that may occur (see Figure 1-3).


Figure 1-3: Default rendering of an unordered list

Users of PDAs, phones, or other small-screened devices will also be able to view the list in a similar, clearly organized fashion. Because we've told the device what the data is (a list in this case), it can best decide how to display it according to its capabilities.

If a long line wraps due to increased text size or a narrow browsing window, the wrapped line will appear indented to line up with the text above it. It'll be darn clear to distinguish between items no matter what the browsing circumstances.



 < Day Day Up > 



Web Standards Solutions. The Markup and Style Handbook
Web Standards Solutions: The Markup and Style Handbook (Pioneering Series)
ISBN: 1590593812
EAN: 2147483647
Year: 2003
Pages: 119
Authors: Dan Cederholm

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net