Приглашаем посетить
Черный Саша (cherny-sasha.lit-info.ru)

Understanding Lists

Previous Page
Table of Contents
Next Page

Understanding Lists

There are three types of lists: unordered, ordered, and definitions. They have similar markup but should be used based on their content.

  • Unordered lists are bulleted by default. You can change this bullet to a hollow circle or a square, or you can even replace the bullet with a graphic or an entity such as ~ (tilde).

  • Ordered lists are numbered by default. You can change the numbers to letters of the alphabet or to roman numerals.

  • Definition lists (or nested lists) contain subitems; you might use this type of list for a glossary of terms.

The markup for lists is very simple. Here's the code for an unordered list

<ul>
<li>Gibson</li>
<li>Fender</li>
<li>Rickenbacker</li>
<li>Washburn</li>
</ul>

This unordered list opens with an unordered list (ul) tag, contains a number of list (li) items, and then closes with another ul tag.

An ordered list is very similar except the list tag is ol instead of ul. In an ordered list, each item is sequentially labeled using numbers, letters, or roman numerals, depending on the value of the list-style-type property. You can place the label either outside of the list or within in it using the list-style-position property.

Figure 7.1 shows some examples of different types of unordered and ordered lists.

Figure 7.1. Here are examples of the various sorts of unordered and ordered lists you can create.

Understanding Lists


Styling Lists

Lists are the basis for navigation and menus; after all, navigation elements usually consist of a list of pages you can visita menu is a list of choices. So there is a movement within the CSS community toward styling navigation and menus as lists. One very important advantage of this thinking is that if the user is viewing the page in a user agentperhaps a cell phonethat cannot apply the CSS styles, the XHTML list markup is enough to render the navigation or menus in meaningful ways. Even if that doesn't make sense now, it will by the time you have read through this section. Let's begin by styling a set of navigation links that you might find on the left sidebar of almost any site.

Here's the markup for an unordered list inside of a div, so you can see it in relation to a containing element (in the context of left navigation, the container would be the left column)

<div id="listcontainer">
<ul>
<li>Gibson</li>
<li>Fender</li>
<li>Rickenbacker</li>
<li>Washburn</li>
</ul>
</div>

First, let's display the div so we can see our unstyled list inside it (Figure 7.2).

body {font: 1.0em verdana, arial, sans-serif;}
div#listcontainer {border:1px solid #000; width:160px; font-size:.75em; margin:20px;}

Figure 7.2. Here's an unordered list inside a div with its border on.

Understanding Lists


Second, let's turn on the borders of the ul and the li elements so that we can see the position of the individual elements of the list (Figure 7.3).

ul {border:1px solid red;}
li {border:1px solid green;}

Figure 7.3. With the ul and li borders turned on, it's clear that Safari (top) and Internet Explorer (bottom) have different default styles for lists.

Understanding Lists

Understanding Lists


In Figure 7.3, the ul has a red border and each li has a green border. As you can see, Safari uses padding on the ul to indent the list (the green elements are pushed away from the ul red container) and also adds small top and bottom margins to separate the list from surrounding items. Internet Explorer uses a margin on the ul to indent the list (note the ul is wrapped tight around the li elements and both are moved away from the div), and Internet Explorer only adds a bottom margin, not a top one. These differences can make it hard to have lists look the same across browsers. The only way to overcome these discrepancies is first to reset the margin and padding values on lists to zero and then restyle them.

So let's set the ul and li margins and padding on both types of list elements to zero (Figure 7.4).

ul {border:1px solid red; margin:0; padding:0;}
li {border:1px solid green; margin:0; padding:0;}

Figure 7.4. Now Safari (top) and Internet Explorer (bottom) present the list identically. Note that once the bullets move outside of the ul in Internet Explorer, they are not displayed.

Understanding Lists

Understanding Lists


Now the list looks the same on both Safari and Internet Explorer. Note that the bullets, which belong to the li elements, are now hanging outside of the div. If the div was right against the edge of the browser window, the bullets wouldn't even be visible. So it's clear that we have to apply some minimal amount of left margin or padding to the ul to ensure that the bullets are within the div and are not overlapping another element, or, as is the case in Internet Explorer, are not even displayed. So let's set the left margin (Figure 7.5).

div#listcontainer {border:1px solid #000; width:160px; font-size:.75em; margin:20px;}
ul {border:1px solid red; margin:0 0 0 1.25em; padding:0;}
li {border:1px solid green; margin:0;}

Figure 7.5. By setting a left margin of 1.25 em on the ul, the bullets are brought back into the div.

Understanding Lists


Note that I used the shorthand style to set all the margins, not just the left margin. If you don't keep the others explicitly set at zero, the default top and bottom margins, which are different for each browser, will reappear. Also, now we have placeholders ready for the other three values in case we want to change them later.

Let's change the space between the list items since they're a little too close together

div#listcontainer {border:1px solid #000; width:160px; font-size:.75em; margin:20px;}
ul {border:1px solid red; margin:0 0 0 1.25em; padding:0;}
li {border:1px solid green; margin:0; padding:.3em 0;}

The obvious way to do this is to set the margin-top or margin-bottom on the li elements, but I prefer to use identical top and bottom padding. This keeps the li elements touching instead of creating space between them, which gives us some more options for styling. To show you why it's better to increase the padding than to change the margin, I'll replace the boxes around the elements with neat horizontal lines between each item (Figure 7.6).

Figure 7.6. Adding the horizontal lines is the first step in giving the list a more designed look.

Understanding Lists


By adding the top and bottom padding to increase the height of the li elements instead of creating space between them, the top and bottom edges of the li elements are exactly halfway between each line of type. Now when you style either their top or bottom edges, you get a rule exactly halfway between them.

Now let's do some more cleanup on this list (Figure 7.7).

  1. Remove the bullets.

  2. Set the margins on the ul element so that the list is better positioned within the div.

  3. Indent the list items so they are not flush with the edge of the rules.

Figure 7.7. The list looks better, but it could use a rule above the first item.

Understanding Lists


Here are the changes to the markup

body {font:1em verdana, arial, sans-serif;}
div#listcontainer {border:1px solid #000; width:160px; font-size:.75em; margin:20px;}
ul {border:0; margin:10px 30px 10px 1.25em; padding:0; list-style-type:none;}
li {border-bottom:2px solid #069; margin:0; padding:.3em 0; text-indent:.5em}

The most notable modification made at this step was using the list-style-type property, with a value of none, to remove the bullets. The text-indent property moves the text in slightly from the left edge of the rule, and the new margins on the ul position the list nicely within the container.

The list would certainly look better if we added a line across the top of the first item (Figure 7.8 on the next page). The ideal solution is to add a border-top to just the first list item. There is a simple way to do that with the CSS pseudo-class :first-child. Sadly, IDWIMIEInternet Explorer does not understand that pseudo-class.

Figure 7.8. Here we've added a top line using the :first-child pseudo-class.

Understanding Lists


This means that we either add the :first-child pseudo-class and accept, zen-like, that it will not appear in Internet Explorer, or we come up with a work-around. Let's use the pseudo-class first, and then think about a work-around for our less-compliant but popular friend Internet Explorer. Here's the code

body {font:1em verdana, arial, sans-serif;}
div#listcontainer {border:1px solid #000; width:150px; font-size:.75em; margin:20px;}
ul {border:0; margin:12px 20px 10px 1.25em; padding:0; list-style-type:none;}
li {border-bottom:2px solid #069; margin:0; padding:.3em 0; text-indent:.5em}
li:first-child {border-top:2px solid #069;}

Now take a look at a simple fix for Internet Explorer. When the bullet was removed, the ul element shrank down to the same width as the list items. We can create our top rule by applying the style to the top of the ul, which contains all the list items. Instead of using the markup I just listed, you could use the following, which makes the top line appear in Internet Explorer too


body {font:1em verdana, arial, sans-serif;}
div#listcontainer {border:1px solid #000; width:150px; font-size:.75em; margin:20px;}
ul {border:0; margin:12px 20px 10px 1.25em; padding:0; list-style-type:none; border-top:
Understanding Lists 2px solid #069}
li {border-bottom:2px solid #069; margin:0; padding:.3em 0; text-indent:.5em}

Sometimes you can find easy work-arounds like this for Internet Explorer, and sometimes you have to accept that not everyone is going to get the same experience. As long as everyone gets an acceptable experience, that's OK.

We now have something that looks a lot like a set of navigation links. All we need to do is turn what are now lines of text into links and we'll have an attractive and functional navigation element (Figure 7.9). This is quite simple using this code

<div id="listcontainer">
<ul>
<li><a href="gibson.htm">Gibson</a></li>
<li><a href="fender.htm">Fender</a></li>
<li><a href="rickenbacker.htm">Rickenbacker</a></li>
<li><a href="washburn.htm">Washburn</a></li>
</ul>
</div>

Figure 7.9. With links added into the markup, the text now appears underlined.

Understanding Lists


Note how the link tags are closest to the content and nested inside the list items.

Basic Link Styling

Now let's give your links some styles. First, let's remove the underlining from them in their normal, "sitting-there-waiting-for-something-to-happen" state, and then, when the users rolls the cursor over the link, let's have it change color.

Understanding Lists

It's good practice to differentiate onscreen elements or states (such as the active/not active links) with more than just color alone. In this case, you change the cursor from an arrow pointer to a hand pointer as it moves over the link as a secondary way to convey the clickable nature of the link.


Also, since this is the last step to complete your navigation component, you should do some clean up: you need to adjust the ul bottom margin, add context to the selectors so that only the elements within your listcontainer div are affected by the styles, and add a hack (actually, two hacks) to make the line above the first list item appear in Internet Explorer for Windows.

Here's the final code for your list-based navigation component

body {font:1em verdana, arial, sans-serif;}
div#listcontainer {border:1px solid #000; width:150px; font-size:.75em; margin:20px;}
div#listcontainer ul {border:0; margin:12px 20px 12px 1.25em; padding:0; list-style-type
Understanding Lists:none;}
div#listcontainer li {border-bottom:2px solid #069; margin:0; padding:.3em 0; text-indent:
Understanding Lists.5em}
div#listcontainer li:first-child {border-top:2px solid #069;}
div#listcontainer a {text-decoration:none; color:#069;}
div#listcontainer a:hover {color: #FCC;}
html div#listcontainer ul {border-top:2px solid #069;}        <-- a

(a)A hack for Internet Explorer for Windows

Figure 7.10. Here's the finished menu, complete with a rollover highlight.

Understanding Lists


The Star Hack and the Backslash Hack

A hack is the term for using CSS in ways other than the way it was intended to be used. Hacks are used to enable CSS to be targeted to, or hidden from, specific browsers. A very common use of a hack is to provide alternate code for Internet Explorer.

The Star Hack

You have seen that the great-granddaddy of all ancestor elements is the html element; all elements are its descendants. However, Internet Explorer is unique in that it has an unnamed element that is a parent of the html element, so by referencing this element in a selector, you create a rule that is only read by Internet Explorer. Because this element has no name, you reference it with * (known as star), the wildcard CSS selector. The most common way to use the star selector is like this

div * ul {...some CSS...}

Here, the ul is not selected if it is a child of the div, but it is if it is a grandchild. You use the * to say that it doesn't matter what the in-between child element is.

So, to create a rule that is only read by Internet Explorer, you write

* html ...more specific selectors... {...some CSS...}

For example

div#box {border:1px solid blue;}
* html div#box {border:1px solid red;}

In this example, all browsers set the border to blue, except Internet Explorer for Windows and Internet Explorer for Mac, which read the second rule also, and display the box border in red.

However, the more compliant Internet Explorer for Mac can interpret both the * selector and some CSS that Internet Explorer for Windows cannot interpret. So you need some way to ensure that only Internet Explorer for Windows reads the star hack rule. You do this by putting the star hack rule inside a pair of comments, which are written in a special way to take advantage of a strange, unique behavior of Internet Explorer for Mac.

The Backslash-Comment Hack

If you write a comment with a backslash right before the closing *, like this

/* this a comment \*/

Internet Explorer for Mac does not think the comment has ended and ignores all the CSS that follows until it encounters a comment that is closed in a normal way. (If you like to add comments after, or on the same line as, your selectors, don't do it here, since the positioning of the comments is what makes this hack work.)

For example,

/* a hack for IE Win only \*/
* html div#listcontainer ul {border-top:2px solid #069;}
/*end of hack */

Here, Internet Explorer for Mac ignores the line with the * html selector, even though it is perfectly capable of reading it; it thinks the first comment doesn't close until the end of the second one, so the * html selector is hidden from it. The combination of the star and backslash hacks gives you a rule that is only read by Internet Explorer for Windows.


    Previous Page
    Table of Contents
    Next Page