How do I auto-hide placeholder text upon focus using css or jquery?
Instantly hide placeholder text on input focus via this CSS snippet:
The ::placeholder
pseudo-element paired with :focus
makes the placeholder disappear once the user starts typing. No JavaScript required, just copy, paste, and presto!
Cross-browser support (or "Why can't we all just get along?")
Browser compatibility is often a "fun" game of whack-a-mole. To make sure your placeholder hiding works across browsers, look at these vendor prefixes:
This ensures that users on various browsers all get the same great user experience.
Gracefully fade-out placeholders with transition
Don't scare away your users by making your placeholders disappear suddenly. We're not in a magic show, after all:
With CSS transition
, your placeholder will gracefully fade out like my diet resolutions after New Year's.
Overlapping labels for Bollywood-style drama
Sometimes accessibility calls for a Bollywood-style love triangle – the input field, the placeholder, and the label:
Labels behaving like placeholders offer a visual charm, while still being a guiding angel for assistive technologies.
jQuery: Because who doesn't love more code?
If you'd rather go jQuery, here's how to make your placeholders pull a Houdini:
This neat trick stores the placeholder text before it vanishes, and brings it back after you've finished your input.
Outsmart browser defaults with -webkit-appearance: none
Because sometimes browser styles cramp your style:
Setting -webkit-appearance: none;
lets you maintain styling consistency by overriding default browser styles.
Interactive playground
Remember, experiments are best done outside the lab. Use JSFiddle or Stack Snippets to test, tweak, and tinker with your code, always keeping them updated with browser compatibility notes.
Was this article helpful?