This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The :placeholder-shown
CSS pseudo-class represents any <input>
or <textarea>
element that is currently displaying placeholder text.
/* Selects any element with an active placeholder */ :placeholder-shown { border: 2px solid silver; }
:placeholder-shown
<input placeholder="Type something here!">
input { border: 2px solid black; padding: 3px; } input:placeholder-shown { border-color: silver; }
On narrow screens such as smartphones, the width of search boxes and other form fields can get drastically shortened. This can result in their placeholder text getting cropped in an undesirable way. It is often useful to alter this behavior with the text-overflow
property.
<input placeholder="Enter something into this field, if you please!">
input:placeholder-shown { text-overflow: ellipsis; }
<input placeholder="Type something here!">
input:placeholder-shown { color: red; font-style: italic; }
The following example highlights the Branch and ID code fields with a custom style.
<form id="test"> <p> <label for="name">Enter Student Name:</label> <input id="name" placeholder="Student Name"/> </p> <p> <label for="branch">Enter Student Branch:</label> <input id="branch" placeholder="Student Branch"/> </p> <p> <label for="sid">Enter Student ID:</label> <input type="number" pattern="[0-9]{8}" title="8 digit ID" id="sid" class="studentid" placeholder="8 digit id"/> </p> <input type="submit"/> </form>
input { background-color: #E8E8E8; color: black; } input.studentid:placeholder-shown { background-color: yellow; color: red; font-style: italic; }
Specification | Status | Comment |
---|---|---|
Selectors Level 4 The definition of ':placeholder-shown' in that specification. | Working Draft | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 47 | No
|
51
|
10
|
34 | 9 |
Support on non-type="text" elements (such as type="number" or type="time" )
|
? | No | Yes | No | ? | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 51 | ? | No
|
51
|
? | 9.2 | ? |
Support on non-type="text" elements (such as type="number" or type="time" )
|
? | ? | No | Yes | ? | Yes | ? |
::placeholder
pseudo-element styles the placeholder itself.<input>
, <textarea>
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/:placeholder-shown