Placeholder and file controls
Definition and purpose
Form pseudo-elements select supported pieces of native controls. ::placeholder styles hint text, and ::file-selector-button styles the button in a file input. They do not replace the input label or alter file selection permissions.
Readable hint
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a::placeholder { color: navy; opacity: 1; } </style> </head> <body> <label for="a">Search</label> <input id="a" placeholder="Topic"> <label for="b">Upload</label> <input id="b" type="file"> </body></html>The hint is navy at full opacity.
Syntax and mental model
A placeholder exists visually when the input is empty. Set color and opacity explicitly to avoid platform opacity differences. The file button accepts button-like styling; the adjacent filename text is not part of this pseudo-element.
Style the file picker button
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #b::file-selector-button { background: navy; color: white; padding: 6px; } </style> </head> <body> <label for="a">Search</label> <input id="a" placeholder="Topic"> <label for="b">Upload</label> <input id="b" type="file"> </body></html>The browser still manages the file picker.
Common mistakes
Do not make hint text too faint or use it for essential instructions. Never hide the file input without an accessible replacement.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Form state pseudo-classes. Next lesson: Selects, checkboxes, range and meters.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Placeholder and file controls — knowledge check
Five questions on placeholder and file controls.
Which pseudo-element styles the file button?
Which selector styles hint text?
When does placeholder text disappear?
Why keep a visible label?
Can file-selector-button style the filename text beside it?