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 to target: the bullet or number of a list item, the text a user has highlighted, and the hint text inside an empty input. It assumes you know the difference between a pseudo-class and a pseudo-element (one colon versus two), basic selector syntax, and how to read a rule's specificity.
What these three have in common
Each of the three pseudo-elements styles something the browser draws for you. You can't wrap it in a <span>, so CSS gives you a handle instead.
All three share a restriction: only a small set of properties works on each. The browser limits them for performance and legibility. If a declaration silently does nothing, the property is probably not on the allowed list.
::marker: bullets and numbers
::marker targets the marker box of a list item. That is the bullet in a <ul> or the number in an <ol>. It works on any element whose display is list-item, which includes <li> by default.
li::marker {
color: #c00;
font-weight: bold;
}Before ::marker, changing a bullet's color meant removing it with list-style: none and rebuilding it with ::before. That approach breaks the automatic numbering of ordered lists. ::marker keeps the browser's own counter and bullet and only changes how they look.
What you can change
The spec allows a short list of properties on ::marker:
- Font properties, such as
font-size,font-weight,font-family, andfont-variant-numeric. color.content.white-space,direction,unicode-bidi, andtext-combine-upright.- Animation and transition properties.
Properties such as background, padding, margin, and border are not on the list. Don't expect to draw a colored circle behind a number with them.
Replacing the marker with content
The content property lets you swap the bullet for a string or emoji. Pair it with list-style-type if you want a fallback in browsers that ignore content on markers.
ul.checklist li::marker {
content: "\2713 ";
color: #1a7f37;
}The \2713 escape is a check mark. The trailing spaces add room between the symbol and the text, since you can't use margin on a marker.
Aligning numbers
Numbers in an ordered list line up better when every digit has the same width. Set font-variant-numeric: tabular-nums on the marker:
ol li::marker {
font-variant-numeric: tabular-nums;
font-weight: 600;
}The list is the wrong place to look
The marker belongs to each list item, not to the list. A rule written as ul::marker does nothing, because a <ul> is not a list item. Write li::marker, or ul > li::marker to limit the scope.
If you set display: flex or display: grid on an <li>, it stops being a list-item. Its marker disappears and ::marker has nothing to style. Put the flex or grid layout on an inner wrapper instead.
::selection: highlighted text
::selection styles the part of the document the user has selected with the mouse, keyboard, or a "select all" command.
::selection {
background: #ffe58f;
color: #000;
}Written with no element in front, the rule applies to the whole page. You can scope it to a region, and a more specific rule wins inside that region:
::selection {
background: #ffe58f;
color: #000;
}
pre::selection,
pre ::selection {
background: #264f78;
color: #fff;
}The second rule has two selectors. pre::selection covers text directly inside the <pre>. pre ::selection covers text in its descendants, such as the <code> element inside it. Writing both is a common way to avoid gaps.
Allowed properties
Only a few properties apply:
colorandbackground-color.text-decorationand its related properties.text-shadow.stroke-color,fill-color, andstroke-widthfor SVG text.
A background-image is ignored. Layout properties such as padding, font-size, and border have no effect, because a selection can't change how text is laid out.
Always set both colors
If you set only background, the text color stays whatever it was. That can produce light text on a light highlight. Set color and background-color together, and check the contrast of the pair.
A selection color that the page already uses, such as a brand color with light text, can also make selected text look like a button or error state. Pick a color that reads as a highlight.
Grouping selectors
Older guides tell you to write ::selection and ::-moz-selection in one comma-separated rule. Don't do that. If a browser doesn't recognize one selector in a list, it drops the whole rule. Current Firefox accepts the unprefixed ::selection, so you no longer need the prefixed version.
::placeholder: hint text in empty fields
::placeholder styles the text shown by the placeholder attribute on <input> and <textarea>. The text only appears while the field is empty.
input::placeholder,
textarea::placeholder {
color: #767676;
font-style: italic;
}The accepted properties are those that apply to ::first-line: font properties, color, background, text-decoration, letter-spacing, word-spacing, line-height, and a few more.
Set a color explicitly
Browsers choose their own default placeholder color, and some also reduce its opacity. If you set a color and the result looks fainter than you chose, check for a default opacity. Set opacity: 1 along with your color so the contrast you calculated is the contrast users see.
A placeholder color of #767676 on a white background has a contrast ratio of about 4.5:1, which is the minimum for normal text under WCAG. Anything lighter fails.
::placeholder is not :placeholder-shown
These two look alike but do different jobs:
| Selector | Type | What it matches |
|---|---|---|
| `::placeholder` | Pseudo-element | The hint text itself, so you can style its color and font |
| `:placeholder-shown` | Pseudo-class | The input element, while its placeholder is visible |
Use ::placeholder to change how the hint looks. Use :placeholder-shown to change the field, for example to hide an error border while the field is still empty:
input:required:invalid:not(:placeholder-shown) {
border-color: #c00;
}Placeholders are not labels
A placeholder disappears when the user types, so it can't serve as the only label for a field. Screen readers don't treat it as a reliable name either. Always pair a field with a <label>, and use the placeholder for an example format such as [email protected].
A complete example
Save this as a single HTML file and open it in a browser. Select some text, then click into the input.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Marker, selection, placeholder</title>
<style>
body {
font-family: system-ui, sans-serif;
max-width: 32rem;
margin: 2rem auto;
}
ul.checklist {
padding-left: 1.5rem;
}
ul.checklist li::marker {
content: "\2713 ";
color: #1a7f37;
}
ol li::marker {
font-variant-numeric: tabular-nums;
font-weight: 600;
color: #1a73e8;
}
::selection {
background: #ffe58f;
color: #000;
}
label {
display: block;
margin-top: 1rem;
font-weight: 600;
}
input {
width: 100%;
padding: 0.5rem;
box-sizing: border-box;
}
input::placeholder {
color: #767676;
opacity: 1;
font-style: italic;
}
input:required:invalid:not(:placeholder-shown) {
border-color: #c00;
}
</style>
</head>
<body>
<h1>Setup steps</h1>
<ul class="checklist">
<li>Install the tools</li>
<li>Clone the repository</li>
</ul>
<ol>
<li>Open the project</li>
<li>Run the tests</li>
</ol>
<label for="email">Email address</label>
<input id="email" type="email" required placeholder="[email protected]">
</body>
</html>The check marks replace the bullets, the numbers turn blue, selected text turns pale yellow, and the placeholder shows in gray italics. Type abc into the email field and the border turns red, because the field is required, invalid, and no longer showing its placeholder.
Specificity and browser support
Every pseudo-element adds one point at the type-selector level, so li::marker has specificity (0,0,2) and input::placeholder has (0,0,2). A class on the host raises it, as in .checklist li::marker.
Support is good for ::selection and ::placeholder. Support for ::marker is also broad, but browsers differ in which properties they honor. Safari has historically accepted fewer properties on markers than Chrome and Firefox. Test the content and font-variant-numeric declarations in your target browsers before relying on them.
Common mistakes
- Writing
ul::markerinstead ofli::marker. - Using
display: flexon an<li>and losing its marker. - Setting
::selectionbackground without settingcolor. - Putting
::selectionand::-moz-selectionin one comma list. - Relying on the placeholder instead of a
<label>. - Using a placeholder color with a contrast ratio below 4.5:1.
- Expecting
padding,margin, orbackgroundto work on::marker.
Exercise
A list item's custom marker is not showing up. The CSS is ul::marker { color: red; }, and each <li> has display: flex. Name the two problems and give a fix.
Show answer
First, ::marker belongs to list items, not to the list, so the selector should be li::marker. Second, display: flex on the <li> changes its display from list-item, so it no longer has a marker at all. Remove the flex layout from the <li>, or move it onto an inner wrapper element, and use li::marker { color: red; }.
Exercise
You want selected text on a dark code block to appear white on a dark blue background, while the rest of the page uses black on pale yellow. Write the CSS, and say what could go wrong if you leave out the color declaration.
Show answer
Use ::selection { background: #ffe58f; color: #000; } for the page and pre::selection, pre ::selection { background: #264f78; color: #fff; } for code blocks. Without color, the text would keep its original color, which on a code block may be light gray. That could leave selected text hard to read against the new background.