Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:placeholder-shown — a control displaying a placeholder

The :placeholder-shown pseudo-class selects a control currently displaying its placeholder hint. It lets you change the appearance of the whole field rather than just the placeholder text.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

:placeholder-shown

:placeholder-shown is a pseudo-class without arguments. In HTML, it matches input and textarea elements when the value of their placeholder attribute is currently presented to the user. The rule input:placeholder-shown therefore styles the field in that state.

It is not a general test for an empty value. Having a placeholder attribute is not sufficient either: [placeholder] checks attribute presence, whereas :placeholder-shown checks whether the hint is being displayed. An empty field without a placeholder does not match; consequently, :not(:placeholder-shown) does not automatically mean filled or valid.

Distinguish :placeholder-shown from ::placeholder: the pseudo-class selects the control itself, while the pseudo-element represents the placeholder text. Use the former, for example, to change the field's border and the latter to style the hint.

In the example, the empty field displaying the hint Ada has a blue border. Entering a value changes it to gray; deleting the entire value restores the blue border when the hint is displayed again.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Placeholder state</title>
<style>
  input { border: 3px solid #555; padding: 0.5rem; }
  input:placeholder-shown { border-color: #005fcc; }
</style>
<label for="name">Name</label>
<input id="name" type="text" placeholder="Ada" autocomplete="off">
</html>

Result

Empty field showing Ada: blue border (#005fcc). After typing Ada: gray border (#555). After deleting the entire value: blue border again.

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources