I’m a big fan of the placeholder
attribute introduced in HTML5. In my book, I described the cowpath it was paving:
- When a form field has no value, insert some placeholder text into it.
- When the user focuses on that field, remove the placeholder text.
- If the user leaves the field and the field still has no value, reinstate the placeholder text.
That’s the behaviour that browsers mimicked when they began implementing the native placeholder
functionality. I think Opera was first. Now all the major browsers support it.
But in some browsers, the details of that behaviour have changed slightly. In Chrome and Safari, when the user focuses on the field, the placeholder text remains. It’s not until the user actually begins to type that the placeholder text is removed.
Now, personally speaking, I’m not keen on this variation. It seems that I’m not alone. In an email to the WHATWG, Markus Ernst describes the problems that he’s noticed in user-testing where users are trying (and, of course, failing) to select the placeholder text in order to delete it before they begin typing.
It seems that a relevant number of users do not even try to start typing as long as the placeholder text remains visible.
But this isn’t so clear-cut. A quick straw poll at the Clearleft showed that opinions were divided on this. Some people prefer the newer behaviour …however it quickly became apparent that the situations they were thinking of were examples of where placeholder
has been abused i.e. attempt to act as a label
for the form field. In that situation, I agree, it would definitely be more useful for the labelling text to remain visible for as long as possible. But that’s not what placeholder
is for. The placeholder
attribute is intended to show a short hint (such as an example value)—it should be used in addition to a label
; not instead of a label
. I tend to use example content in my placeholder value and I nearly always begin with “e.g.”:
<label for="fn">Your Name</label>
<input id="fn" name="fn" type="text" placeholder="e.g. Joe Bloggs">
(Don’t forget: generating placeholders from datalists can be a handy little pattern.)
So if you’re using placeholder
incorrectly as a label
, then the WebKit behaviour is probably what you want. But if you’re using placeholder
as intended, then the behaviour in the other browsers is probably more desirable. If you want to get Safari and Chrome to mimic the behaviour of the other browsers, here’s a handy bit of CSS (from that same thread on the WHATWG mailing list):
[placeholder]:focus::-webkit-input-placeholder {
color: transparent;
}
You can see that in action on search forms at The Session for recordings, events, discussions, etc.
Now, if you do want your label—or input mask—to appear within your form field and remain even when the user focuses on the field, go ahead and do that. Use a label
element with some CSS and JavaScript trickery to get the effect you want. But don’t use the placeholder
attribute.