More CSS answers

by Danny
Published 2 min read

Use the ::placeholder pseudo-element to change placeholder colour and font. Keep it readable, and never use a placeholder in place of a real label.

The short answer

#

Select the placeholder with the ::placeholder pseudo-element. It works on input and textarea elements. Keep the colour readable, and always give the field a real label as well.


_10
input::placeholder,
_10
textarea::placeholder {
_10
color: #6b7280;
_10
font-style: italic;
_10
opacity: 1;
_10
}

Why it happens

#

The placeholder is not a child element you can select with a normal class. The browser draws it inside the field from the placeholder attribute, so CSS exposes it through a pseudo-element, the same way ::before or ::selection expose other parts of the page.

Only a subset of properties work inside ::placeholder: the font properties, color, opacity, letter-spacing, word-spacing, text-decoration, text-transform, line-height and background properties, among a few others. Layout properties like padding and margin do not apply to it. To change spacing, style the input itself.

You may also see vendor prefixes such as ::-webkit-input-placeholder in older code. They were needed many years ago. All current browsers support the unprefixed ::placeholder, so you can drop them.

How to fix it

#

Style the placeholder


_12
.field {
_12
font: inherit;
_12
color: #111827;
_12
padding: 0.625rem 0.75rem;
_12
border: 1px solid #9ca3af;
_12
border-radius: 0.375rem;
_12
}
_12
_12
.field::placeholder {
_12
color: #6b7280;
_12
opacity: 1;
_12
}

Setting opacity: 1 matters. Firefox has historically given placeholders a lower default opacity, so your chosen colour would come out paler than you intended. Set the colour you want and keep opacity at 1.

Keep enough contrast

The Web Content Accessibility Guidelines (WCAG) ask for a contrast ratio of at least 4.5:1 for normal text. Placeholder text is still text people need to read, so aim for that ratio against the field background. Very light greys like #ccc on white fail it. A mid grey such as #6b7280 on white passes.

In dark mode, set a separate value rather than reusing the light theme grey.


_10
@media (prefers-color-scheme: dark) {
_10
.field {
_10
background: #111827;
_10
color: #f9fafb;
_10
}
_10
_10
.field::placeholder {
_10
color: #9ca3af;
_10
}
_10
}

Style the field while the placeholder is showing

::placeholder styles the text. To style the whole input while it is empty and showing its placeholder, use the :placeholder-shown pseudo-class.


_10
.field:placeholder-shown {
_10
border-style: dashed;
_10
}
_10
_10
.field:not(:placeholder-shown):invalid {
_10
border-color: #b91c1c;
_10
}

The second rule is a handy pattern: it only shows an invalid style once the person has typed something.

Use a real label, not a placeholder

A placeholder disappears as soon as someone starts typing, so it cannot do the job of a label.


_10
<label for="email">Email address</label>
_10
<input id="email" type="email" placeholder="[email protected]" autocomplete="email" />

Use the placeholder for a short example of the expected format, if anything. Put instructions that people need while typing in a hint below the field, linked with aria-describedby.

Common pitfalls

#
  • Placeholder as the only label. People forget what the field was for once they start typing, and screen readers do not reliably treat a placeholder as the field's name. Always add a label.
  • Text that looks filled in. A dark placeholder can look like a real value, so people skip the field. Keep it clearly lighter than the input text, while still passing contrast.
  • Trying to pad the placeholder. Padding on ::placeholder does nothing. Add padding to the input.
  • Putting important instructions in it. Password rules or date formats vanish on the first keystroke. Put them in visible helper text.
  • Selector lists with old prefixes. If one selector in a list is invalid in a browser, the whole rule is dropped. Do not group old vendor-prefixed selectors with ::placeholder in the same rule.
#

Was this resource helpful?