CSS Lists Module Level 3

W3C Working Draft,

This version:
https://www.w3.org/TR/2019/WD-css-lists-3-20190425/
Latest published version:
https://www.w3.org/TR/css-lists-3/
Editor's Draft:
https://drafts.csswg.org/css-lists-3/
Previous Versions:
Issue Tracking:
Inline In Spec
GitHub Issues
Editors:
Elika J. Etemad / fantasai (Invited Expert)
Tab Atkins (Google)
Former Editors:
(Google)
(Formerly of Microsoft)
Suggest an Edit for this Spec:
GitHub Editor
Contributors:
Simon Montagu, AOL-TW/Netscape, smontagu@netscape.com
Daniel Yacob, yacob@geez.org
Christopher Hoess, choess@stwing.upenn.edu
Daniel Glazman, AOL-TW/Netscape, glazman@netscape.com

Abstract

This module contains CSS features related to list counters: styling them, positioning them, and manipulating their value.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, etc.

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

GitHub Issues are preferred for discussion of this specification. When filing an issue, please put the text “css-lists” in the title, preferably like this: “[css-lists] …summary of comment…”. All issues and comments are archived, and there is also a historical archive.

This document was produced by the CSS Working Group.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 1 March 2019 W3C Process Document.

1. Introduction

This specification defines the ::marker pseudo-element, the list-item display type that generates markers, and several properties controlling the placement and styling of markers.

It also defines counters, which are special numerical objects often used to generate the default contents of markers.

For instance, the following example illustrates how markers can be used to add parentheses around each numbered list item:
<style>
li::marker { content: "(" counter(list-item, lower-roman) ")"; }
li { display: list-item; }
</style>
<ol>
  <li>This is the first item.
  <li>This is the second item.
  <li>This is the third item.
</ol>

It should produce something like this:

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

Note: Note that this example is far more verbose than is usually needed in HTML, as the UA default style sheet takes care of most of the necessary styling.

With descendant selectors and child selectors, it’s possible to specify different marker types depending on the depth of embedded lists.

2. Declaring a List Item

A list item is any element with its display property set to list-item. List items generate ::marker pseudo-elements; no other elements do. Additionally, list items automatically increment an implied list-item counter (see §4.2 The Implicit list-item Counter).

3. Markers

The defining feature of the list item display type is its marker, a symbol or ordinal that helps denote the beginning of each list item in a list. In the CSS layout model, list item markers are represented by a marker box associated with each list item. The contents of this marker can be controlled with the list-style-type and list-style-image properties on the list item and by assigning properties to its ::marker pseudo-element.

3.1. The ::marker Pseudo-Element

The marker box is generated by the ::marker pseudo-element of a list item as the list item’s first child, before the ::before pseudo-element (if it exists on the element). It is filled with content as defined in §3.2 Generating Marker Contents.

Marker boxes only exist for list items: on any other element, the ::marker pseudo-element’s content property must compute to none, which suppresses its creation.

Only a limited set of properties can be used on the ::marker pseudo-element. This list is defined in CSS Pseudo-Elements 4 §4.2 List Markers: the ::marker pseudo-element.

In this example, markers are used to number paragraphs that are designated as "notes":
<style>
p { margin-left: 12 em; }
p.note {
  display: list-item;
  counter-increment: note-counter;
}
p.note::marker {
  content: "Note " counter(note-counter) ":";
}
</style>
<p>This is the first paragraph in this document.
<p class="note">This is a very short document.
<p>This is the end.

It should render something like this:

        This is the first paragraph
        in this document.

Note 1: This is a very short
        document.

        This is the end.
By using the ::marker pseudo-element, a list’s markers can be styled independently from the text of the list item itself:
<style>
p { margin-left: 8em } /* Make space for counters */
li { list-style-type: lower-roman; }
li::marker { color: blue; font-weight:bold; }
</style>
<p>This is a long preceding paragraph ...
<ol>
  <li>This is the first item.
  <li>This is the second item.
  <li>This is the third item.
</ol>
<p>This is a long following paragraph ...

The preceding document should render something like this:

       This is a long preceding
       paragraph ...

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

       This is a long following
       paragraph ...

Previously the only way to style a marker was through inheritance; one had to put the desired marker styling on the list item, and then revert that on a wrapper element around the list item’s actual contents.

3.2. Generating Marker Contents

The contents of a marker box are determined by the first of these conditions that is true:

content on the ::marker itself is not normal
The contents of the marker box are determined as defined by the content property, exactly as for ::before.
list-style-image on the originating element defines a marker image
The 'marker box contains an anonymous inline replaced element representing the specified marker image, followed by a text run consisting of a single space (U+0020 SPACE).
list-style-type on the originating element defines a marker string
The marker box contains a text run consisting of the specified marker string.
otherwise
The marker box has no contents and ::marker does not generate a box.

3.3. Image Markers: the list-style-image property

Name: list-style-image
Value: <image> | none
Initial: none
Applies to: list items
Inherited: yes
Percentages: n/a
Computed value: computed value of the <image>, or none
Canonical order: per grammar
Animation type: discrete

Specifies the marker image, which is used to fill the list item’s marker when its content is normal. The values are as follows:

<image>
If the <image> represents a valid image, specifies the element’s marker image as the <image>. Otherwise, the element has no marker image.
none
The element has no marker image.
The following example sets the marker at the beginning of each list item to be the image "ellipse.png".
li { list-style-image: url("http://www.example.com/ellipse.png") }

3.4. Text-based Markers: the list-style-type property

Name: list-style-type
Value: <counter-style> | <string> | none
Initial: disc
Applies to: list items
Inherited: yes
Percentages: n/a
Computed value: specified value
Canonical order: per grammar
Animation type: discrete

Specifies the marker string, which is used to fill the list item’s marker when its content value is normal and there is no marker image. The values are as follows:

<counter-style>
Specifies the element’s marker string as the value of the list-item counter represented using the specified <counter-style>.

Specifically, the marker string is the result of generating a counter representation of the list-item counter value using the specified <counter-style>, prefixed by the prefix of the <counter-style>, and followed by the suffix of the <counter-style>. If the specified <counter-style> does not exist, decimal is assumed.

<string>
The element’s marker string is the specified <string>.
none
The element has no marker string.
The following examples illustrate how to set markers to various values:
ul { list-style-type: "★"; }
/* Sets the marker to a "star" character */

p.note {
  display: list-item;
  list-style-type: "Note: ";
  list-style-position: inside;
}
/* Gives note paragraphs a marker consisting of the string "Note: " */

ol { list-style-type: upper-roman; }
/* Sets all ordered lists to use the upper-roman counter-style
   (defined in the Counter Styles specification [[CSS-COUNTER-STYLES]]) */

ul { list-style-type: symbols(repeating '○' '●'); }
/* Sets all unordered list items to alternate between empty and
   filled circles for their markers. */

ul { list-style-type: none; }
/* Suppresses the marker entirely, unless list-style-image is specified
   with a valid image. */

3.5. Positioning Markers: The list-style-position property

Name: list-style-position
Value: inside | outside
Initial: outside
Applies to: list items
Inherited: yes
Percentages: n/a
Computed value: keyword, but see prose
Canonical order: per grammar
Animation type: discrete

This property dictates whether the ::marker is rendered inline, or positioned just outside of the list item. The values are as follows:

inside
No special effect. (The ::marker is an inline element at the start of the list item’s contents.)
outside
If the list item is a block container: the marker box is outside the principal block box; however, the position of the list-item marker adjacent to floats is undefined. CSS does not specify the precise location of the marker box or its position in the painting order, but does require that it be placed on the inline-start side of the box, using the writing mode of the box indicated by marker-side. The marker box is fixed with respect to the principal block box’s border and does not scroll with the principal box’s content. A UA may hide the marker if the element’s overflow is other than visible. (This allowance may change in the future.) The size or contents of the marker box may affect the height of the principal block box and/or the height of its first line box, and in some cases may cause the creation of a new line box; this interaction is also not defined.

This is handwavey nonsense from CSS2, and needs a real definition.

If the list item is an inline box: this value is equivalent to inside.

Alternatively, outside could lay out the marker as a previous sibling of the principal inline box.

For example:
<style>
  ul.compact { list-style: inside; }
  ul         { list-style: outside; }
</style>
<ul class=compact>
  <li>first "inside" list item comes first</li>
  <li>second "inside" list item comes first</li>
</ul>
<hr>
<ul>
  <li>first "outside" list item comes first</li>
  <li>second "outside" list item comes first</li>
</ul>

The above example may be formatted as:

  * first "inside" list
  item comes first
  * second "inside" list
  item comes second

========================

* first "outside" list
  item comes first
* second "outside" list
  item comes second

3.6. Styling Markers: the list-style shorthand property

Name: list-style
Value: <'list-style-position'> || <'list-style-image'> || <'list-style-type'>
Initial: see individual properties
Applies to: list items
Inherited: see individual properties
Percentages: see individual properties
Computed value: see individual properties
Animation type: see individual properties
Canonical order: per grammar

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.

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

Using a value of none in the shorthand is potentially ambiguous, as none is a valid value for both list-style-image and list-style-type. To resolve this ambiguity, a value of none in the shorthand must be applied to whichever of the two properties aren’t otherwise set by the shorthand.

list-style: none disc;
/* Sets the image to "none" and the type to "disc". */

list-style: none url(bullet.png);
/* Sets the image to "url(bullet.png)" and the type to "none". */

list-style: none;
/* Sets both image and type to "none". */

list-style: none disc url(bullet.png);
/* Syntax error */
Although authors may specify list-style information directly on list item elements (e.g., li in HTML), they should do so with care. Consider the following rules:
ol.alpha li { list-style: lower-alpha; }
ul li       { list-style: disc; }

The above won’t work as expected. If you nest a ul into an ol class=alpha, the first rule’s specificity will make the ul’s list items use the lower-alpha style.

ol.alpha > li { list-style: lower-alpha; }
ul > li       { list-style: disc; }

These work as intended.

ol.alpha { list-style: lower-alpha; }
ul       { list-style: disc; }

These are even better, since inheritance will transfer the list-style value to the list items.

3.7. The marker-side property

Name: marker-side
Value: match-self | match-parent
Initial: match-self
Applies to: list items
Inherited: yes
Percentages: n/a
Computed value: specified keyword
Canonical order: per grammar
Animation type: discrete

The marker-side property specifies whether the ::marker is positioned based on the directionality of the list item itself (i.e. its originating element) or the directionality of the list container (i.e. the originating element’s parent). In the first case, the position of the marker can vary across items in the same list, based on the directionality assigned to each list item individually; in the second case they will all align on the same side, as determined by the directionality assigned to the list as a whole.

match-self
The ::marker pseudo-element is positioned using the directionality of the ::marker’s originating element.
match-parent
The ::marker pseudo-element is positioned using the directionality of the ::marker’s originating element’s parent element.
By default, elements or ::marker pseudo-elements position themselves according to their list item’s directionality. However, if the list item is grouped with several other list items which may have different directionality (for example, multiple <li>s with different "dir" attributes in an <ol> in HTML), it is sometimes more useful to have all the markers line up on one side, so the author can specify a single "gutter" on that side and be assured that all the markers will lie in that gutter and be visible.

Both of the following example renderings are generated from the following HTML, with the only difference being the value of marker-side on the list:

<ul>
  <li>english one
  <li dir=rtl>OWT WERBEH
  <li>english three
  <li dir=rtl>RUOF WERBEH
</ul>
match-self match-parent
* english one
     OWT WERBEH *
* english three
    RUOF WERBEH *
* english one
*    OWT WERBEH
* english three
*   RUOF WERBEH

4. Automatic Numbering With Counters

A counter is a special concept used, among other things, to automatically number list items in CSS. Every element has a collection of zero or more counters, which are inherited through the document tree in a way similar to inherited property values. They are created and manipulated with the counter-increment, counter-set and counter-reset properties, and used with the counter() and counters() functions. Counters have a name, an integer value, a creator element, and possibly another counter nested inside themselves.

This section is not fully synchronized with CSS2. Please reference Automatic Counters and Numbering in CSS Level 2: any conflicts or omissions here should be resolved in favor of the behavior specified in CSS2.

4.1. Manipulating Counters: the counter-increment, counter-set and counter-reset properties

Name: counter-reset
Value: [ <custom-ident> <integer>? ]+ | none
Initial: none
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: the keyword none or a list, each item an identifier paired with an integer
Canonical order: per grammar
Animation type: by computed value type

User Agents are expected to support this property on all media, including non-visual ones.

The counter-reset property creates new counters on an element. Its values are defined as follows:

none
This element does not create any new counters.
<custom-ident> <integer>?
The element creates one or more new counters. Each <custom-ident> names a new counter to be created.

If an <integer> is provided after an <custom-ident>, the starting value of the new counter is that integer. Otherwise, the starting value of the new counter is 0.

Implementations may have implementation-specific limits on the maximum or minimum value of a counter. If an increment would push the counter’s value beyond these limits, the increment must be ignored, and the counter’s value remain unchanged.

Name: counter-set
Value: [ <custom-ident> <integer>? ]+ | none
Initial: none
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: the keyword none or a list, each item an identifier paired with an integer
Canonical order: per grammar
Animation type: by computed value type

User Agents are expected to support this property on all media, including non-visual ones.

Name: counter-increment
Value: [ <custom-ident> <integer>? ]+ | none
Initial: none
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: the keyword none or a list, each item an identifier paired with an integer
Canonical order: per grammar
Animation type: by computed value type

User Agents are expected to support this property on all media, including non-visual ones.

The counter-set and counter-increment properties manipulate the value of existing counters. They only create new counters if there is no counter of the given name on the element yet. Their values are defined as follows:

none
This element does not alter the value of any counters.
<custom-ident> <integer>?
The element alters the value of one or more counters on it. If there is not currently a counter of the given name on the element, the element creates a new counter of the given name with a starting value of 0 (though it may then immediately set or increment that value to something different).

If an <integer> is provided after an <custom-ident>, it sets the innermost counter of the given name’s value to that integer (for counter-set) or increments the value of the innermost counter of the given name by that integer (for counter-increment). Otherwise, the innermost counter of the given name’s value is set to 0 (for counter-set) or incremented by 1 (for counter-increment).

This example shows a way to number chapters and sections with "Chapter 1", "1.1", "1.2", etc.

h1::before {
    content: "Chapter " counter(chapter) ". ";
    counter-increment: chapter;  /* Add 1 to chapter */
    counter-reset: section;      /* Set section to 0 */
}
h2::before {
    content: counter(chapter) "." counter(section) " ";
    counter-increment: section;
}

Inheriting counters must be done before resetting counters, which must be done before incrementing counters, which must be done before setting counters, which must be done before using counters (for example, in the content property).

The counter properties follow the cascading rules as normal. Thus, due to cascading, the following style sheet:
h1 { counter-reset: section -1 }
h1 { counter-reset: imagenum 99 }

will only reset imagenum. To reset both counters, they have to be specified together:

H1 { counter-reset: section -1 imagenum 99 }

The same principles apply to the counter-set and counter-increment properties.

4.2. The Implicit list-item Counter

List items automatically increment a special list-item counter. Unless the counter-increment property explicitly specifies a different increment for the list-item counter, it must be incremented by 1 on every list item, at the same time that counters are normally incremented. (This has no effect on the computed values of the counter-* properties.)

In the following example, the list is modified to count by twos:
ol.evens { counter-increment: list-item 2; }

A three-item list would be rendered as

2. First Item
4. Second Item
6. Third Item

More definition. See old WG discussions and newer issue discussion.

UAs and host languages should ensure that the list-item counter values reflect the underlying numeric value dictated by host language semantics when setting up list item styling in their UA style sheet and presentational hint style mappings. Not really sure how to word this requirement, but the following example is expected to work.

In the following example, the content property is used to create tiered numbering that hooks into the list-item counter, and thus respects any numbering changes inflicted through HTML:
ol::marker { content: counters(list-item,'.') '.'; }

Nested lists using this rule would be rendered like

1. First top-level item
5. Second top-level item, value=5
   5.3. First second-level item, list start=3
   5.4. Second second-level item, list start=3
        5.4.4. First third-level item in reversed list
        5.4.3. Second third-level item in reversed list
        5.4.2. Third third-level item in reversed list
        5.4.1. Fourth third-level item in reversed list
   5.5. Third second-level item, list start=3
6. Third top-level item

given markup such as

<ol>
  <li>First top-level item
  <li value=5>Second top-level item, value=5
    <ol start=3>
      <li>First second-level item, list start=3
      <li>Second second-level item, list start=3
         <ol reversed>
           <li>First third-level item in reversed list
           <li>Second third-level item in reversed list
           <li>Third third-level item in reversed list
           <li>Fourth third-level item in reversed list
         </ol>
      <li>Third second-level item, list start=3
  <li>Third top-level item
</ol>

4.3. Creating and Inheriting Counters

Every element has a (possibly empty) set of counters. Like many other CSS values, an element can inherit counters from another element. However, unlike other CSS values, the method that counters are inherited is somewhat complex. A counter and its value are inherited separately, possibly from different elements.

If an element has a previous sibling, it must inherit all of the sibling’s counters. Otherwise, if the element has a parent, it must inherit all of the parent’s counters. Otherwise, the element must have an empty set of counters.

The element then inherits counter values from the immediately preceding element in document order. This must be done by examining the set of counters that the immediately preceding element has, and, for every counter that exists in both the element’s set and the preceding element’s set, giving the element’s counter the same value. (If an element is the first element in the document, and thus has no immediately preceding element, it also doesn’t have a parent or a previous sibling, and thus no counters to begin with.)

Take the following code as an example:
<ul style='counter-reset: example 0;'>
  <li id='foo' style='counter-increment: example;'>
    foo
    <div id='bar' style='counter-increment: example;'>bar</div>
  </li>
  <li id='baz'>
    baz
  </li>
</ul>

Recall that "in document order" turns a document tree into an ordered list, where an element comes before its children, and its children come before its next sibling. In other words, for a language like HTML, its the order in which the parser encounters start tags as it reads the document.

In here, the ul element establishes a new counter named "example", and sets its value to 0. The "foo" element, being the first child of the ul, inherits this counter. Its parent is also its immediately preceding element in document order, so it inherits the value 0 with it, and then immediately increments the value to 1.

The same happens with the "bar" element. It inherits the "example" counter from "foo", and inherits the value 1 from it as well and increments it to 2.

However, the "baz" element is a bit different. It inherits the "example" counter from the "foo" element, its previous sibling. However, rather than inheriting the value 1 from "foo" along with the counter, in inherits the value 2 from "bar", the previous element in document order.

This behavior allows a single counter to be used throughout a document, continuously incrementing, without the author having to worry about the nested structure of their document.

Elements can create additional counters on themselves, which can then be passed onto siblings or children. To create a new counter, specify an element that’s creating it, a name, and a starting value. The effect depends on what other counters of that name exist on the element:

Regardless, the value of the new counter is set to the provided starting value.

4.4. Nested Counters

Counters are "self-nesting"; creating a new counter in an element which already has a counter with the same name simply creates a new counter of the same name, nested inside the existing counter. This is important for situations like lists in HTML, where lists can be nested inside lists to arbitrary depth. It would be impossible to define uniquely named counters for each level. As explained in a later section, the counter() function only uses the innermost counter of a given name on the element, but the counters() function uses all counters of a given name that contain the element.

Thus, the following suffices to number nested list items. The result is very similar to that of setting display:list-item and list-style: inside on the LI element:
ol { counter-reset: item }
li { display: block }
li::before { content: counter(item) ". "; counter-increment: item }

In this example, an ol will create a counter, and all children of the ol will refer to that counter.

If we denote the nth instance of the "item" counter by item[n], then the following HTML fragment will use the indicated counters. (We assume the style sheet as given in the example above).

<ol> item[0] is created, set to 0
<li> item[0] is incremented to 1
<li> item[0] is incremented to 2
<ol> item[1] is created, set to 0, nested in item[0]
<li> item[1] is incremented to 1
<li> item[1] is incremented to 2
<li> item[1] is incremented to 3
<ol> item[2] is created, set to 0, nested in item[1]
<li> item[2] is incremented to 1
<li> item[1] is incremented to 4
<ol> item[3] is created, set to 0, nested in item[1]
<li> item[3] is incremented to 1
<li> item[1] is incremented to 5
<li> item[0] is incremented to 3
<li> item[0] is incremented to 4
<ol> item[4] is created, set to 0
<li> item[4] is incremented to 1
<li> item[4] is incremented to 2

4.5. Counters in elements that do not generate boxes

An element that does not generate a box (for example, an element with display set to none, or a pseudo-element with content set to none) cannot set, reset, or increment a counter. The counter properties are still valid on such an element, but they must have no effect.

For example, with the following style sheet, H2s with class "secret" do not increment count2.
h2 { counter-increment: count2; }
h2.secret { display: none; }

Other methods of "hiding" elements, such as setting visibility to hidden, still cause the element to generate a box, and so do not apply here.

4.6. Outputting Counters: the counter() and counters() functions

Counters have no visible effect by themselves, but their values can be used with the counter() and counters() functions, which represent the counter values as strings or images. Their syntax is:

<counter> = <counter()> | <counters()>
counter()  =  counter( <custom-ident>, [ <counter-style> | none ]? )
counters() = counters( <custom-ident>, <string>, [ <counter-style> | none ]? )

For both functions, the first argument represents the name of a counter, and if the last argument is omitted it defaults to decimal.

The used value of counter() is obtained as follows:

H1::before        { content: counter(chno, upper-latin) ". " }
/* Generates headings like "A. A History of Discontent" */

H2::before        { content: counter(section, upper-roman) " - " }
/* Generates headings like "II - The Discontent Part" */

BLOCKQUOTE::after { content: " [" counter(bq, decimal) "]" }
/* Generates blockquotes that end like "... [3]" */

DIV.note::before  { content: counter(notecntr, disc) " " }
/* Simply generates a bullet before every div.note */

P::before         { content: counter(p, none) }
/* inserts nothing */

The used value of counters() is obtained as follows:

The following example shows a simple use of the counters() function:
<ul>
  <li>one</li>
  <li>two
    <ul>
      <li>nested one</li>
      <li>nested two</li>
    </ul>
  </li>
  <li>three</li>
</ul>
<style>
li::marker { content: '(' counters(list-item,'.') ') '; }
</style>

The preceding document should render something like this:

(1) one
(2) two
   (2.1) nested one
   (2.2) nested two
(3) three
Because counters inherit to siblings as well, they can be used to number headings and subheadings, which aren’t nested within each other. Unfortunately, this prevents the use of counters() as counters from siblings don’t nest, but one can create multiple counters and manually concatenate them instead:
<h1>First H1</h1>
...
<h2>First H2 in H1</h2>
...
<h2>Second H2 in H1</h2>
...
<h3>First H3 in H2</h3>
...
<h1>Second H1</h1>
...
<h2>First H2 in H1</h2>
...
<style>
body { counter-reset: h1 h2 h3; }
h1   { counter-increment: h1; counter-reset: h2 h3;}
h2   { counter-increment: h2; counter-reset:    h3; }
h3   { counter-increment: h3; }
h1::before { content: counter(h1,upper-alpha) ' '; }
h2::before { content: counter(h1,upper-alpha) '.'
                      counter(h2,decimal) ' '; }
h3::before { content: counter(h1,upper-alpha) '.'
                      counter(h2,decimal) '.'
                      counter(h3,lower-roman) ' '; }
</style>

The preceding document should render something like this:

A First H1
...
A.1 First H2 in H1
...
A.2 Second H2 in H1
...
A.2.i First H3 in H2
...
B Second H1
...
B.1 First H2 in H1
...
Counters are sometimes useful for things other than printing markers. In general, they provide the ability to number elements in sequence, which can be useful for other properties to reference. For example, using order to put an element between two other specific elements currently requires you to explicitly put order on every element before and/or after the desired insertion point. If you can set the order value of everything to a counter, tho, you can more easily insert an element into an arbitrary spot between two others.

Other use-cases involve nested or sibling elements with transforms that are meant to be slightly different from each other. Today you have to use a preprocessor to do this in a reasonable way, but a counter would make it work well in "plain" CSS.

(You can built up successive values in the nested case today by using custom properties and stacking up nested calc()s, but this is a *little bit* clumsy, and doesn’t work for siblings.)

Suggestion is to add a counter-value(<custom-ident>) function, which returns the value of the named counter as an integer, rather than returning a string.

See Issue 1026.

5. Appendix A: Sample Style Sheet For HTML

This section is informative, not normative. HTML itself defines the actual default properties that apply to HTML lists.

/* Set up list items */
li {
  display: list-item;
  /* counter-increment: list-item; (implied by display: list-item) */
}

/* Set up ol and ul so that they reset the list-item counter */
ol, ul {
  counter-reset: list-item;
}

/* Default list style types for ordered lists */
ol {
  list-style-type: decimal;
}

/* Default list style types for unordered lists up to 3 deep */
ul { list-style-type: disc; }
ul ul { list-style-type: square; }
ul ul ul { list-style-type: circle; }
/* Alternately, if Values & Units Level 3 is supported, replace
   the above three lines with: */
ul { list-style-type: disc; }
ul ul { list-style-type: cycle(disc, square, circle); }

/* The type attribute on ol and ul elements */
ul[type="disc"] { list-style-type: disc; }
ul[type="circle"] { list-style-type: circle; }
ul[type="square"] { list-style-type: square; }
ol[type="1"] { list-style-type: decimal; }
ol[type="a"] { list-style-type: lower-alpha; }
ol[type="A"] { list-style-type: upper-alpha; }
ol[type="i"] { list-style-type: lower-roman; }
ol[type="I"] { list-style-type: upper-roman; }

/* The start attribute on ol elements */
ol[start] {
  counter-reset: list-item calc(attr(start integer, 1) - 1);
}

/* The value attribute on li elements */
li[value] {
  counter-set: list-item attr(value integer, 1);
  counter-increment: none; /* Turn off default increase */
}

/* Handling reversed lists */
ol[reversed] {
  counter-reset: list-item calc(attr(start integer, **magic**) + 1);
  /* Where **magic** is the number of child <li> elements. */
}
ol[reversed] > li {
  counter-increment: list-item -1;
}

/* Box Model Rules */
ol, ul {
  display: block;
  margin: 1em 0;
  marker-side: list-container;
}

ol:dir(ltr), ul:dir(ltr) {
  padding-left: 40px;
}
ol:dir(rtl), ul:dir(rtl) {
  padding-right: 40px;
}

ol ol, ol ul, ul ul, ul ol {
  margin-top: 0;
  margin-bottom: 0;
}

li {
  text-align: match-parent;
}

li::marker {
  unicode-bidi: isolate;
  font-variant-numeric: tabular-nums;
}

Acknowledgments

The following people and documentation they wrote were very useful for defining the numbering systems: Alexander Savenkov, Arron Eicholz, Aryeh Gregor, Frank Tang, Jonathan Rosenne, Karl Ove Hufthammer, Musheg Arakelyan, Nariné Renard Karapetyan, Randall Bart, Richard Ishida, Simon Montagu (Mozilla, smontagu@smontagu.org)

Changes

This section documents the changes since previous publications.

Changes since the 20 March 2014 WD

Changes From CSS Level 2

As described in the introduction section, there are significant changes in this module when compared to CSS2.1.

  1. The ::marker pseudo-element has been introduced to allow styling of the list marker directly.
  2. list-style-type now accepts a <string> as well as the extended <counter-style> values from [css-counter-styles-3]..
  3. The list-item predefined counter identifier has been introduced.
  4. The counter-set property has been added.
  5. Allowed for inline-level list items, as introduced in [css-display-3].

Conformance

Document conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.

Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

Requirements for Responsible Implementation of CSS

The following sections define several conformance requirements for implementing CSS responsibly, in a way that promotes interoperability in the present and future.

Partial Implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported property values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

Implementations of CR-level Features

Once a specification reaches the Candidate Recommendation stage, implementers should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec, and should avoid exposing a prefixed variant of that feature.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at https://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS-CONTENT-3]
Elika Etemad; Dave Cramer. CSS Generated Content Module Level 3. 2 June 2016. WD. URL: https://www.w3.org/TR/css-content-3/
[CSS-COUNTER-STYLES-3]
Tab Atkins Jr.. CSS Counter Styles Level 3. 14 December 2017. CR. URL: https://www.w3.org/TR/css-counter-styles-3/
[CSS-DISPLAY-3]
Tab Atkins Jr.; Elika Etemad. CSS Display Module Level 3. 28 August 2018. CR. URL: https://www.w3.org/TR/css-display-3/
[CSS-FLEXBOX-1]
Tab Atkins Jr.; et al. CSS Flexible Box Layout Module Level 1. 19 November 2018. CR. URL: https://www.w3.org/TR/css-flexbox-1/
[CSS-IMAGES-4]
Tab Atkins Jr.; Elika Etemad; Lea Verou. CSS Image Values and Replaced Content Module Level 4. 13 April 2017. WD. URL: https://www.w3.org/TR/css-images-4/
[CSS-OVERFLOW-3]
David Baron; Elika Etemad; Florian Rivoal. CSS Overflow Module Level 3. 31 July 2018. WD. URL: https://www.w3.org/TR/css-overflow-3/
[CSS-POSITION-3]
Rossen Atanassov; Arron Eicholz. CSS Positioned Layout Module Level 3. 17 May 2016. WD. URL: https://www.w3.org/TR/css-position-3/
[CSS-PSEUDO-4]
Daniel Glazman; Elika Etemad; Alan Stearns. CSS Pseudo-Elements Module Level 4. 25 February 2019. WD. URL: https://www.w3.org/TR/css-pseudo-4/
[CSS-VALUES-3]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 31 January 2019. CR. URL: https://www.w3.org/TR/css-values-3/
[CSS-VALUES-4]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 4. 31 January 2019. WD. URL: https://www.w3.org/TR/css-values-4/
[CSS-VARIABLES-1]
Tab Atkins Jr.. CSS Custom Properties for Cascading Variables Module Level 1. 3 December 2015. CR. URL: https://www.w3.org/TR/css-variables-1/
[CSS-WRITING-MODES-4]
Elika Etemad; Koji Ishii. CSS Writing Modes Level 4. 24 May 2018. CR. URL: https://www.w3.org/TR/css-writing-modes-4/
[CSS2]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 7 June 2011. REC. URL: https://www.w3.org/TR/CSS2/
[CSS3-IMAGES]
Elika Etemad; Tab Atkins Jr.. CSS Image Values and Replaced Content Module Level 3. 17 April 2012. CR. URL: https://www.w3.org/TR/css3-images/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119
[SELECTORS-4]
Elika Etemad; Tab Atkins Jr.. Selectors Level 4. 21 November 2018. WD. URL: https://www.w3.org/TR/selectors-4/

Informative References

[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/

Property Index

Name Value Initial Applies to Inh. %ages Anim­ation type Canonical order Com­puted value
counter-increment [ <custom-ident> <integer>? ]+ | none none all elements no n/a by computed value type per grammar the keyword none or a list, each item an identifier paired with an integer
counter-reset [ <custom-ident> <integer>? ]+ | none none all elements no n/a by computed value type per grammar the keyword none or a list, each item an identifier paired with an integer
counter-set [ <custom-ident> <integer>? ]+ | none none all elements no n/a by computed value type per grammar the keyword none or a list, each item an identifier paired with an integer
list-style <'list-style-position'> || <'list-style-image'> || <'list-style-type'> see individual properties list items see individual properties see individual properties see individual properties per grammar see individual properties
list-style-image <image> | none none list items yes n/a discrete per grammar computed value of the <image>, or none
list-style-position inside | outside outside list items yes n/a discrete per grammar keyword, but see prose
list-style-type <counter-style> | <string> | none disc list items yes n/a discrete per grammar specified value
marker-side match-self | match-parent match-self list items yes n/a discrete per grammar specified keyword

Issues Index

This is handwavey nonsense from CSS2, and needs a real definition.
Alternatively, outside could lay out the marker as a previous sibling of the principal inline box.
This section is not fully synchronized with CSS2. Please reference Automatic Counters and Numbering in CSS Level 2: any conflicts or omissions here should be resolved in favor of the behavior specified in CSS2.
More definition. See old WG discussions and newer issue discussion.
Not really sure how to word this requirement, but the following example is expected to work.
Counters are sometimes useful for things other than printing markers. In general, they provide the ability to number elements in sequence, which can be useful for other properties to reference. For example, using order to put an element between two other specific elements currently requires you to explicitly put order on every element before and/or after the desired insertion point. If you can set the order value of everything to a counter, tho, you can more easily insert an element into an arbitrary spot between two others.

Other use-cases involve nested or sibling elements with transforms that are meant to be slightly different from each other. Today you have to use a preprocessor to do this in a reasonable way, but a counter would make it work well in "plain" CSS.

(You can built up successive values in the nested case today by using custom properties and stacking up nested calc()s, but this is a *little bit* clumsy, and doesn’t work for siblings.)

Suggestion is to add a counter-value(<custom-ident>) function, which returns the value of the named counter as an integer, rather than returning a string.

See Issue 1026.