Create a custom bullet list in CSS
11 A custom bullet list
easyReplace the default list markers with your own, without breaking the list for a screen reader.
Stuck? Three hints, smallest first
- 1.
list-style: noneplus a::markeror::beforegives you control. - 2.
::markeris the modern, purpose-built pseudo-element and keeps list semantics intact. - 3. In Safari,
list-style: nonealone can remove the list role; addingrole="list"restores it.
<ul class="ticks" role="list">
<li>Air conditioning</li>
<li>Reclining seats</li>
<li>Onboard toilet</li>
</ul>
.ticks {
list-style: none;
margin: 0;
padding: 0;
}
.ticks li {
padding-left: 24px;
margin-bottom: 6px;
position: relative;
}
/* ::before draws the marker. content is decorative, so it is hidden from
assistive technology with an empty alt via the aria-hidden trick below. */
.ticks li::before {
content: '\2713'; /* a tick */
position: absolute;
left: 0;
color: #1e9e5a;
font-weight: 700;
}
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 default markers are removed', declares(css, 'list-style', 'none'), true);
test('a marker is drawn with a pseudo-element', /::before/.test(css), true);
test('it has content', /content\s*:/.test(css), true);
test('space is left for it', /padding-left\s*:\s*24px/.test(css), true);
test('it is positioned rather than floated', declares(css, 'position', 'absolute'), true);
test('a real list is used', hasTag(html, 'ul'), true);
test('three items', countTag(html, 'li'), 3);
test('role=list restores semantics Safari drops', /role="list"/.test(html), true);
Output
The last assertion is the non-obvious one. In Safari with VoiceOver, list-style: none removes the list role along with the bullets, so a screen reader stops announcing "list, three items". Adding role="list" back is a known, widely used workaround. If you only take one thing from this exercise, that is it.