Can I hide the HTML5 number input’s spin box?
Make number input spinners disappear swiftly with this CSS:
Using -webkit-appearance: none
against webkit's pseudo-elements, we can defenestrate those troublesome spinners in Chrome/Safari. For Firefox, the magic spell is -moz-appearance: textfield
, poof! No more spinboxes. This pure CSS incantation grants your number fields a monastic calm across major browsers.
CSS and cross-browser compatibility
Browser-specific quirks
Custom elements are a bit like kids. Different ones need different treatment and understanding. Sometimes they even respond to different names!
Webkit-based browsers (such as Chrome, Safari, and recently Edge) like -webkit-appearance: none;
Meanwhile, Firefox, staunchly independent as always, prefers -moz-appearance: textfield;
.
The treacherous lure of display: none
It might look like a quick solution but display: none
can be a double-edged sword. While it effectively removes the spinner, it can also hide the entire input field in Chrome. So avoid it, unless you enjoy vanishing input fields magic tricks.
Number entry on touch devices
On touch devices, using type="text"
and a numerical pattern
brings up a comfortable numeric keyboard, without the clutter of a spinner. It's like having your pie and eating it too.
Spinner on demand?
Like a jack-in-the-box, you may want your spinners to pop up on certain occasions. Relax, CSS pseudo-classes :hover
and :focus
are here to the rescue.
Now, the spinner only shows when the user hovers over or focuses on the input field, making it clear who's the boss.
A picture paints a thousand words
When the number was wearing running shoes (spin-box), it felt the pressure to run up and down. After some CSS magic, its running shoes were replaced with a pair of comfortable slippers:
Before: 🔢👟👟 (number laced up, ready to run) After: 🔢 (number, feet up, chilling)
Relax, number. There's no more pressure to go up or down. 🛌
Going the extra mile with form controls
Maximizing user interaction
Sometimes it's about balance. You might want the spinner on user interaction, without it being a constant presence. Enter, :hover
and :focus
. CSS to the rescue again!
Make it mobile-friendly
When using a mobile browser, you want to make it as smooth as possible. By switching the type
attribute from "number"
to "text"
, and setting a numerical pattern
, you ensure a numeric data entry on mobile platforms too.
Future-proof your code
Remember the mantra, progressive enhancement. CSS can be a warm friend or a tough workout partner, depending on how much you've kept up with its ever-evolving nature. Keep an eye on beta versions of various browsers to foresee any breaking changes.
Was this article helpful?