XML technologies / CSS / CSS Cookbook / Lists / Custom text markers for list items

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Use your own text-based markers for list items.

Solution

Give the first line of list items a negative indent. Then use the pseudo-element :before and the property content to insert the desired text marker before each item, two angle brackets in our example (see the next figure):

ul {
 list-style: none;
 margin: 0;
 padding: 0 0 0 1em;
 text-indent: -1em;
}
li {
 width: 33%;
 padding: 0;
 margin: 0 0 0.25em 0;
}
li:before {
 content: "\00BB \0020";
}

A text-based list marker

Figure: A text-based list marker.

Discussion

Setting the property list-style to none disables the marker normally displayed to the left of each list item.

Instead of appearing before the list item, the custom marker appears as an inline element together with the item’s other content. To move it outside the item again, shift the first line to the left using a negative value for the property text-indent. A positive value would instead move the marker farther to the right.

ul {
 list-style: none;
 margin: 0;
 padding: 0 0 0 1em;
 text-indent: -1em;
}

Here the pseudo-element :before creates the marker. This makes it easy to insert ordinary keyboard characters:

li:before {
 content: ">> ";
}

Under the CSS 2.1 specification, however, special characters must be specified as Unicode values (ISO 10646). Instead of common HTML4 entities, such as », use the hexadecimal equivalent of the Unicode character for these characters.

Hexadecimal values are escaped with a backslash in CSS:

li:before {
 content: "\00BB \0020";
}

At the time this book went to press, this solution worked in Mozilla, Firefox, Netscape 6+, Safari and Opera, which support generated content. Unfortunately, Internet Explorer for Windows and Macintosh, and thus about 45% of web users, are excluded because they do not support generated content.

For a browser-independent solution, avoid generated content and insert the marker manually before each list item instead:

<ul>
  <li>&#187; I'm not the Same Person I was in the Database</li>
  <li>&#187; Past Breaches of Our Privacy</li>
  <li>&#187; The Best of Intentions</li>
  <li>&#187; Whatever Happened to Automation?</li>
  <li>&#187; The Smart Choice is Not Needing to Make One</li>
</ul>

The main drawback is that disabling CSS and viewing the raw content without styles displays two markers per item: the browser’s marker and the manually inserted character. This is not life-threatening, but adds an unwanted design element to the page.

See also

  

 

 

 

back to top