Custom list markers with ::marker in CSS
10 Replace the markers, keep the list
fairGive a list your own markers — a tick for one list, a formatted number for another — without losing the fact that it is a list.
Stuck? Three hints, smallest first
- 1.
::markeracceptscontent,colorand font properties, and keeps list semantics. - 2.
list-style-typetakes a string in modern CSS:list-style-type: "—". - 3.
list-style: nonecan drop the list role in Safari, so pair it withrole="list".
<ul class="ticks" role="list">
<li>Ticket printed</li>
<li>Bag tagged</li>
<li>Seat assigned</li>
</ul>
<ol class="steps">
<li>Queue</li>
<li>Pay</li>
<li>Board</li>
</ol>
/* list-style: none is what drops the role in Safari, which is why the markup
carries role="list" as well. */
.ticks {
list-style: none;
margin: 0 0 1rem;
padding: 0;
}
.ticks li::before {
content: '\2713\00a0'; /* tick, then a non-breaking space */
color: #17734b;
font-weight: 700;
}
.steps {
margin: 0;
padding-left: 2.2rem;
}
/* ::marker styles the number the browser already generates, so the counter and
the list semantics stay the browser's job. */
.steps li::marker {
content: 'Step ' counter(list-item) '. ';
color: #3055d8;
font-weight: 600;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
test('the tick list drops the default marker', /list-style\s*:\s*none/.test(css), true);
test('and gets role="list" back, which Safari needs', /role="list"/.test(html), true);
test('the tick is drawn with a pseudo-element', /::before/.test(css), true);
test('the numbered list keeps its real counter', /counter\(list-item\)/.test(css), true);
test('styled through ::marker rather than replaced', /::marker/.test(css), true);
test('an ol is used where the order counts', hasTag(html, 'ol'), true);
test('and a ul where it does not', hasTag(html, 'ul'), true);
test('six items in total', countTag(html, 'li'), 6);
Output
Two routes, and they are not equivalent. ::marker restyles the marker the browser already generates, so the numbering, the list role and the item count all stay intact — reach for it first. list-style: none plus ::before gives you more freedom but throws the marker away, and in Safari with VoiceOver it also throws away the list role, so “list, three items” stops being announced. role="list" puts that back. counter(list-item) is the browser's own counter, not one you have to maintain.