Contents
Elements with a 'display' property value of 'list-item' are formatted visually like other block-level elements, only each list item is preceded by a marker. The type of marker and its placement is determined by the list properties described below.
The following rule applies to list item (LI) elements in HTML. The 'display' property declares the presentation of the LI element to be a "list-item", and the 'list-style' property means that no marker will appear next to list items:
LI { display: list-item; list-style: none } <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>
The list might be formatted as follows:
The illustration shows the relationship between the current left margin and the margins and padding of the list element (UL) and the list items (LI). (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 content:
Note.
Property name: | 'list-style-type' |
---|---|
Value: | disc | circle | square | decimal | lower-roman | upper-roman | lower-alpha | upper-alpha | none |
Initial: | disc |
Applies to: | elements with the 'display' property set to 'list-item' |
Inherited: | yes |
Percentage values: | N/A |
This property is used to determine the appearance of the list item marker if 'list-style-image' is 'none' or if the image pointed to by the URL cannot be displayed.
The possible values have the following meanings:
For example, the following HTML document:
<STYLE> OL { list-style-type: lower-roman } </STYLE> <BODY> <OL> <LI> This is the first item. <LI> This is the second item. <LI> This is the third item. </OL> </BODY>
might produce something like this:
i This is the first item. ii This is the second item. iii This is the third item.
Property name: | 'list-style-image' |
---|---|
Value: | <url> | none |
Initial: | none |
Applies to: | elements with the 'display' property set to 'list-item' |
Inherited: | yes |
Percentage values: | N/A |
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) }
Property name: | 'list-style-position' |
---|---|
Value: | inside | outside |
Initial: | outside |
Applies to: | elements with the 'display' property set to 'list-item' |
Inherited: | yes |
Percentage values: | N/A |
The value of 'list-style-position' determines how the list item marker is drawn with regard to the content.
In either case, the placement of the marker does not affect the relationship between the list item's box and the pertinent margin (depending on script direction).
For example:
<STYLE type="text/css"> UL { list-style: outside } UL.compact { list-style: inside } </STYLE> <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>
The above example may be formatted as:
In right-to-left text, the markers would have been on the right side of the box.
Property name: | 'list-style' |
---|---|
Value: | <'list-style-type'> || <'list-style-position'> || <'list-style-image'> |
Initial: | not defined for shorthand properties |
Applies to: | elements with the 'display' property set to 'list-item' |
Inherited: | yes |
Percentage values: | N/A |
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 contextual selector and the second a (more specific) parent-child selector.
OL.alpha LI { list-style: lower-alpha } /* Any LI descendent of an OL */ OL.alpha ~ LI { list-style: lower-alpha } /* Any LI child of an OL */
Authors who only use the contextual selector may not achieve the results they expect. Consider the following rules:
<STYLE type="text/css"> OL.alpha LI { list-style: lower-alpha } UL LI { list-style: disc } </STYLE> <BODY> <OL class=alpha> <LI>level 1 <UL> <LI>level 2 </UL> </OL> </BODY>
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 parent-child selector instead:
<STYLE type="text/css"> OL.alpha ~ LI { list-style: lower-alpha } UL LI { list-style: disc } </STYLE>
Another solution would be to specify 'list-style' information only on the list type elements:
<STYLE type="text/css"> OL.alpha { list-style: lower-alpha } UL { list-style: disc } </STYLE>
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 URL value can 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.