← Back to HTML & CSS, page 5

html-css-0090 · Medium mid

Which special pseudo-elements let you style selected text, input placeholders, list markers and the first line, and what are their limits?

::selection styles highlighted text, ::placeholder styles input hints, ::marker styles bullets and numbers, and ::first-line and ::first-letter style the start of a block. Each allows only some properties, and contrast and real labels still matter.

Full answer

Some parts of a page have no element of their own, but CSS can still reach them with pseudo-elements.

  • ::selection styles text that the user highlights. Only a few properties work: color, background-color, text-decoration and text-shadow. Keep enough contrast, because many people use selection to read.
  • ::placeholder styles the hint text in an input. Placeholder text is often low contrast by default. Do not use it in place of a label, and make sure that the colour is still readable.
  • ::marker styles the bullet or number of a list item. Allowed properties include color, font-size and content. Layout properties such as margin have limited effect. It is a clean way to colour bullets without adding extra elements.
  • ::first-line styles the first line of a block as it is laid out. If the window gets narrower, the first line changes, and the style moves with it. Only font, colour, background and some text properties work.
  • ::first-letter styles the first letter. It is used for drop caps with float and a larger font-size.

These are pseudo-elements, so they use two colons. Do not group a selector with one of them and another unsupported selector in the same rule, because the browser may drop the whole rule if it does not understand one part. Write separate rules when you need vendor-specific versions.

The user's selection is yellow with black text. The placeholder is dark grey and fully opaque (Firefox lowers opacity on placeholders by default, so the example sets it). The list bullet is blue and bold. Only the first line of the paragraph is bold.

The input still has a real label, because a placeholder disappears when the user types and is not a good name for the field.

html
<style>
  ::selection { background: #ffe066; color: #000; }
  input::placeholder { color: #595959; opacity: 1; }
  li::marker { color: #0a6cff; font-weight: bold; }
  p::first-line { font-weight: bold; }
  p { width: 14em; font: 16px/1.4 sans-serif; }
</style>
<label for="n">Name</label>
<input id="n" placeholder="Your full name">
<ul><li id="li">Item one</li></ul>
<p id="p">This paragraph is long enough to wrap onto a second line in a narrow box.</p>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • ::selection allows color, background, text-decoration and text-shadow.
  • ::placeholder needs enough contrast and is not a label.
  • ::marker styles list bullets and numbers.
  • ::first-line follows the actual wrapped first line.
  • Pseudo-elements use two colons.

Common mistakes

  • Using ::placeholder as the only label for an input.
  • Setting a light placeholder colour that fails contrast.
  • Expecting layout properties like margin or width to work on ::selection or ::first-line.

Follow-ups

  • Which properties can you set on ::selection?
  • Why is a placeholder not a replacement for a label?

Tips

  • Say that these pseudo-elements allow only a limited set of properties.
  • Mention contrast for both selection and placeholder.