CSS ::placeholder
Pseudo-Element
The ::placeholder
pseudo-element styles the placeholder text inside form input fields. It allows for the customization of color, font, size, opacity, and more.
1. Syntax
✅ This makes placeholder text gray and italic.
2. Example – Styling Input Placeholders
✅ This makes the placeholder dark gray, 16px, italic, and 70% transparent.
3. Cross-Browser Compatibility
Different browsers require vendor prefixes for full support:
✅ Ensures placeholders look the same across all browsers.
4. ::placeholder
vs :placeholder-shown
Feature | ::placeholder | :placeholder-shown |
---|---|---|
Purpose | Styles placeholder text | Target input fields when a placeholder is visible |
Example | input::placeholder { color: gray; } | input:placeholder-shown { border: 2px solid red; } |
✅ Use ::placeholder
for styling text and :placeholder-shown
for detecting empty fields.
5. Customizing Placeholder Styles
✅ This makes the placeholder blue, bold, spaced out, and uppercase.
6. Browser Support
✅ Supported in all modern browsers (Chrome, Firefox, Edge, Safari).
❌ Not supported in Internet Explorer 11 without -ms-input-placeholder
.
7. Best Practices
✔ Use ::placeholder
to improve form aesthetics.
✔ Apply subtle styles (not too bold or distracting).
✔ Use :placeholder-shown
for validation or interactivity.