17 Lists

Contents

17.1 Visual formatting of lists

CSS allows authors to control the visual presentation of lists in a number of ways:

An element with a 'display' property value of 'list-item' generates two boxes: a block box that contains its content and an inline box that contains a marker. Only the content box is involved with positioning (e.g, in the normal flow). The position of the marker box (see the 'list-style-position' property) does not affect the position of the content box. CSS properties allow authors to specify the marker type (image, glyph, or number) and its position with respect to the content box (outside the content or at the beginning of content). CSS properties do not allow authors to specify distinct style (colors, fonts, alignment, etc.) for the list marker.

The background properties apply to the content box only; the marker box is transparent.

The declaration in the following HTML program causes LI elements to generate 'list-item' boxes. The 'list-style' value 'none' suppresses preceding markers.

<HTML>
<HEAD>
   <STYLE type="text/css">
      LI { display: list-item; list-style: none }
   </STYLE>
</HEAD>
<BODY>
   <UL>
      <LI> This is the first list item, formatted as a block.
      <LI> This is the second list item.
      <LI> This is the third.
   </UL>
</BODY>
</HTML>

The list might be formatted as follows:

A list with no markers.

The illustration shows the relationship between the current left margin and the margins and padding of the list (UL) box and the list item (LI) boxes. (The lines delimiting the margins and padding are not rendered).

If we change the 'list-style' to "square":

  LI { display: list-item; list-style: square }

each list item will be preceded by a small square. However, the placement of the square does not affect the block formatting of the list item box:

A list with square markers.

The list properties in this chapter do not allow authors to control the separation between a list marker and the content of its list item. Instead, this may be achieved with markers. Note that markers specified with 'display: marker' replace list markers specified with the list properties defined in this chapter.

Note. There is no "list" presentation for other types of list structures (e.g., "definition lists" declared by DL, DT, and DD in HTML). Each part of a definition list is simply a block element.

17.1.1 List properties: 'list-style-type', 'list-style-image', 'list-style-position', and 'list-style'

'list-style-type'
Value:  disc | circle | square | decimal | leading-zero | western-decimal | lower-roman | upper-roman | lower-greek | lower-alpha | lower-latin | upper-alpha | upper-latin | hebrew | armenian | georgian | cjk-ideographic | hiragana | katakana | hiragana-iroha | katakana-iroha | none | inherit
Initial:  disc
Applies to:  elements with the 'display' property set to 'list-item'
Inherited:  yes
Percentages:  N/A
Media:  visual

This property specifies appearance of the list item marker if 'list-style-image' has the value 'none' or if the image pointed to by the URI cannot be displayed. The value 'none' specifies no marker, otherwise there are three types of marker: glyphs, numbering systems, and alphabetic systems.

Glyphs are specified with disc, circle, and square. Their exact rendering depends on the user agent.

Numbering systems are specified with:

western-decimal or decimal
Decimal numbers, beginning with 1.
leading-zero
Decimal numbers 01, 02, 03, ... (or 001, 002, 003, ... if more than 99 items)
lower-roman
Lower case roman numerals (i, ii, iii, iv, v, etc.).
upper-roman
Upper case roman numerals (I, II, III, IV, V, etc.).
hebrew
Traditional Hebrew numbering.
georgian
Traditional Georgian numbering (an, ban, gan, ..., he, tan, in, in-an, ...).
armenian
Traditional Armenian numbering.
cjk-ideographic
Plain ideographic numbers
hiragana
a, i, u, e, o, ka, ki, ...
katakana
A, I, U, E, O, KA, KI, ...
hiragana-iroha
i, ro, ha, ni, ho, he, to, ...
katakana-iroha
I, RO, HA, NI, HO, HE, TO, ...

A user agent that does not recognize a numbering system should use 'western-decimal'.

Note. This document does not specify the exact mechanism of each numbering system (e.g., how roman numerals are calculated). A future W3C Note may provide further clarifications.

Alphabetic systems are specified with:

lower-latin or lower-alpha
Lower case ascii letters (a, b, c, ... z).
upper-latin or upper-alpha
Upper case ascii letters (A, B, C, ... Z).
lower-greek
Lower case classical Greek alpha, beta, gamma, ... (έ, ή, ί, ...)

This specification does not define how alphabetic systems wrap at the end of the alphabet. For instance, after 26 list items, 'lower-latin' rendering is undefined. Therefore, for long lists, we recommend that authors specify true numbers.

For example, the following HTML document:

<HTML>
   <HEAD>
     <STYLE type="text/css">
          OL { list-style-type: lower-roman }   
     </STYLE>
  </HEAD>
  <BODY>
    <OL>
      <LI> This is the first item.
      <LI> This is the second item.
      <LI> This is the third item.
    </OL>
  </BODY>
</HTML>

might produce something like this:

  i This is the first item.
 ii This is the second item.
iii This is the third item.

Note that the list marker alignment (here, right justified) depends on the user agent.

Note. Future versions of CSS may provide more complete mechanisms for international numbering styles.

'list-style-image'
Value:  <uri> | none | inherit
Initial:  none
Applies to:  elements with the 'display' property set to 'list-item'
Inherited:  yes
Percentages:  N/A
Media:  visual

This property sets the image that will be used as the list item marker. When the image is available, it will replace the marker set with the 'list-style-type' marker.

The following example sets the marker at the beginning of each list item to be the image "ellipse.png".

  UL { list-style-image: url(http://png.com/ellipse.png) }
'list-style-position'
Value:  inside | outside | inherit
Initial:  outside
Applies to:  elements with the 'display' property set to 'list-item'
Inherited:  yes
Percentages:  N/A
Media:  visual

This property specifies the position of the marker box with respect to line item content box. Values have the following meanings:

outside
The list item marker is outside the box that is generated for the list item.
inside
The list item marker is the first inline box generated for the list item. The list item's contents flow after the marker box.

For example:

<HTML>
  <HEAD>
    <STYLE type="text/css">
      UL         { list-style: outside }
      UL.compact { list-style: inside }
    </STYLE>
  </HEAD>
  <BODY>
    <UL>
      <LI>first list item comes first
      <LI>second list item comes second
    </UL>

    <UL class="compact">
      <LI>first list item comes first
      <LI>second list item comes second
    </UL>
  </BODY>
</HTML>

The above example may be formatted as:

Difference between inside and outside list style position

In right-to-left text, the markers would have been on the right side of the box.

'list-style'
Value:  [ <'list-style-type'> || <'list-style-position'> || <'list-style-image'> ] | inherit
Initial:  not defined for shorthand properties
Applies to:  elements with the 'display' property set to 'list-item'
Inherited:  yes
Percentages:  N/A
Media:  visual

The 'list-style' property is a shorthand notation for setting the three properties 'list-style-type', 'list-style-image', and 'list-style-position' at the same place in the style sheet.

  UL { list-style: upper-roman inside }  /* Any UL*/
  UL > UL { list-style: circle outside } /* Any UL child of a UL*/

Although authors may specify 'list-style' information directly on list item elements (e.g., LI in HTML), they should do so with care. The following rules look similar, but the first declares a descendant selector and the second a (more specific) child selector.

  OL.alpha LI  { list-style: lower-alpha } /* Any LI descendant of an OL */
  OL.alpha > LI  { list-style: lower-alpha } /* Any LI child of an OL */

Authors who use only the descendant selector may not achieve the results they expect. Consider the following rules:

<HTML>
  <HEAD>
    <STYLE type="text/css">
      OL.alpha LI  { list-style: lower-alpha }
      UL LI        { list-style: disc }
    </STYLE>
  </HEAD>
  <BODY>
    <OL class="alpha">
      <LI>level 1
      <UL>
         <LI>level 2
      </UL>
    </OL>
  </BODY>
</HTML>

The desired rendering would have level 1 list items with 'lower-alpha' labels and level 2 items with 'disc' labels. However, the cascading order will cause the first style rule (which includes specific class information) to mask the second. The following rules solve the problem by employing a child selector instead:

    OL.alpha > LI  { list-style: lower-alpha }
    UL LI   { list-style: disc }

Another solution would be to specify 'list-style' information only on the list type elements:

    OL.alpha  { list-style: lower-alpha }
    UL        { list-style: disc }

Inheritance will transfer the 'list-style' values from OL and UL elements to LI elements. This is the recommended way to specify list style information.

A URI value may be combined with any other value, as in:

  UL { list-style: url(http://png.com/ellipse.png) disc }

In the example above, the 'disc' will be used when the image is unavailable.

With a value of 'none', both 'list-style-type' and 'list-style-image' are set to 'none':

  UL { list-style: none }

The result is that no list-item marker is displayed.