XML technologies / CSS / CSS Cookbook / Lists / Separating list items

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

List items should be visually separated from one another.

Solution

First use the property border-top to create a partial border above each list item (li) to serve as a separator:

li {
 border-top: 1px solid black;
 padding: .3em 0;
}

To display a separator below the last item as well, give the list itself (li) a partial bottom border using the property border-bottom (see the next figure):

ul {
 margin-left: 40px;
 padding-left: 0px;
 border-bottom: 1px solid black;
 list-style: none;
 width: 36%;
}

List items are visually separated

Figure: List items are visually separated.

Discussion

To give the separators between items the correct length, assign an appropriate value to either margin-left or padding-left for the unordered list. Otherwise, the borders for the list items and the list itself will have different lengths. For example, if the list items are indented using padding-left, the bottom border (for the list itself) will be longer than the borders for the individual items (see the next figure):

li {
 border-top: 1px solid black;
 padding: .3em 0;
}
ul {
 margin-left: 0px;
 padding-left: 40px;
 border-bottom: 1px solid black;
 list-style: none;
 width: 36%;
}

The list’s bottom border is longer than the list item borders

Figure: The list’s bottom border is longer than the borders of its individual items.

See also

The recipe Cross-browser indentation for creating list indentation that works across browsers.

  

 

 

 

back to top