Sub topic 13
::marker, ::selection, and ::placeholder
This chapter covers three pseudo-elements that style parts of the page with no element of their own: list markers, selected text, and placeholder text. It assumes you know CSS rule syntax, the difference between a pseudo-class and a pseudo-element (two colons), and how to select list items and inputs.
::marker: the bullet or number
Every li in a list has a marker. It is the bullet in a ul or the number in an ol. The ::marker pseudo-element targets it.
li::marker {
color: #c00;
font-weight: bold;
}
ol li::marker {
content: counter(list-item) ") ";
}Only a small set of properties works on ::marker. Those include color, font-* properties, white-space, content, and unicode-bidi. Properties such as background, margin, and padding have no effect. If you need those, remove the native marker with list-style: none and build your own with ::before.
The marker inherits from its li, so setting color on the li also colors the marker. Use ::marker when you want the marker to differ from the text.
::selection: highlighted text
::selection styles text the user has highlighted with the mouse or keyboard.
::selection {
background: #ffe58f;
color: #000;
}
code::selection {
background: #1a73e8;
color: #fff;
}Only a few properties are allowed: color, background-color, text-decoration and its related properties, and text-shadow. Other properties are ignored.
Set color and background together. If you set only one, the other comes from the browser default or from the element's own styling, and the pair can end up unreadable.
::placeholder: hint text in inputs
::placeholder styles the hint text shown in an empty input or textarea that has a placeholder attribute.
input::placeholder,
textarea::placeholder {
color: #767676;
font-style: italic;
}The placeholder disappears once the user types. It is not a label. Keep a real label for each field and use the placeholder only for an example value or format hint.
Browsers give placeholder text a default lighter color. If you override it, keep enough contrast against the field background. The common WCAG minimum for body text is 4.5:1, and #767676 on white just meets it.
A worked example
This page fragment uses all three.
<style>
li::marker {
color: #1a73e8;
}
::selection {
background: #ffe58f;
color: #000;
}
input::placeholder {
color: #767676;
}
</style>
<label for="email">Email</label>
<input id="email" type="email" placeholder="[email protected]">
<ul>
<li>Blue bullet</li>
<li>Select this text to see the yellow highlight</li>
</ul>Common errors
- Setting
backgroundormarginon::markerand seeing nothing happen. The property is not in the allowed set. - Grouping
::selectionwith other selectors in one comma-separated list. Browsers drop the whole rule if any selector in the list is invalid for them, so keep vendor-specific forms in separate rules. - Using the placeholder as the only label. It vanishes on input and is not a reliable accessible name.
- Choosing a placeholder color too faint to read.
Exercise
You set li::marker { background: yellow; padding: 1em; } and nothing changes. Why, and what is one way to get a yellow-backed marker?
Show answer
::marker only supports a small set of properties (such as color, font-*, and content), so background and padding are ignored. Set list-style: none on the list and add a li::before with content, background, and padding, which accept the full range of properties.