Definition and Usage
The ::placeholder pseudo-element selector is used to style the placeholder text of HTML text input controls that have the placeholder attribute.
With the ::placeholder pseudo-element, you can customize the placeholder text's color, font, size, weight, horizontal alignment, and more.
Note:
When you add the placeholder attribute to an HTML text input control, it provides a visual hint as text inside the input field to indicate what kind of input is expected when the field is empty. This hint text is called the placeholder text.
Basic Example
<input type="text" placeholder="placeholder text">
input::placeholder {
color: red;
font-size: 1.2em;
font-style: italic;
font-weight: bold;
text-align: center;
}
Note:
The default color of placeholder text is usually a light gray in most browsers.
The ::placeholder pseudo-element only styles the placeholder text while it is visible. In the example above, the placeholder text is also horizontally centered. Once the user enters text into the input field, the ::placeholder styling no longer applies.
The syntax for the ::placeholder pseudo-element is as follows:
Syntax
::placeholder {
/* CSS properties */
}
Supported CSS Properties for ::placeholder
Caution:
The list of supported CSS properties is not exhaustive and may vary between browsers. Because browser implementations differ, the CSS specification does not strictly define which properties must be supported on ::placeholder.
- Font-related properties
color- Background-related properties
text-alignword-spacingletter-spacingopacity- And others...
Opacity Adjustment in Firefox
Some browsers, especially Firefox, set the placeholder text's default opacity value to less than 1, making it partially transparent. To make the placeholder text fully opaque, explicitly set the opacity value to 1:
::placeholder {
opacity: 1;
}
Browser compatibility
| Selector |
Desktop Chrome
|
DesktopDesktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
::placeholder
|
57 | 79 | 51 | 10.1 |
Specifications
| Specification | |
|---|---|
::placeholder
|
CSS Pseudo-Elements Module Level 4 #placeholder-pseudo |