Can I hide the HTML5 number input’s spin box?
Asked Answered
K

20

1311

Is there a consistent way across browsers to hide the new spin boxes that some browsers (such as Chrome) render for HTML input of type number? I am looking for a CSS or JavaScript method to prevent the up/down arrows from appearing.

<input id="test" type="number">
Kapoor answered 24/9, 2010 at 20:58 Comment(5)
Can you post an example of the code you're using? A screenshot would be great as well so we know what you're looking at. I'm looking at <input type="number" min="4" max="8" /> in Chrome and seeing a typical input field with up and down arrows on the side.Extracanonical
You are seeing exactly what I am seeing. I am trying to keep the type=number markup to ensure that mobile browsers bring up an appropriate keyboard, and prevent the up and down arrows from appearing in computer browsers.Kapoor
If you're using number inputs, be sure to use something that plays nice with modern iOS, Android, and Desktop browsers: <input type="number" pattern="[0-9]*" inputmode="numeric">. More info: https://mcmap.net/q/46460/-phone-numeric-keyboard-for-text-inputStoic
What I suggest doing is writing markup along the lines of <input type="text" placeholder="number (digits only)" pattern="[0-9]+" class="validate number-validation" ... > and using jQuery Validati or similar to handle validation. I haven't tested this approach, which is why this is a comment, rather than an answer.Coadjutor
You might be misusing that input type, the HTML spec explains that it is “not appropriate for input that happens to only consist of numbers but isn't strictly speaking a number.” html.spec.whatwg.org/multipage/…. There are other attributes to control touch screen keyboards like inputmode.Crypto
C
1478

This CSS effectively hides the spin-button for webkit browsers (have tested it in Chrome 7.0.517.44 and Safari Version 5.0.2 (6533.18.5)):

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    /* display: none; <- Crashes Chrome on hover */
    -webkit-appearance: none;
    margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}

input[type=number] {
    -moz-appearance:textfield; /* Firefox */
}
<input type="number" step="0.01" />

You can always use the inspector (webkit, possibly Firebug for Firefox) to look for matched CSS properties for the elements you are interested in, look for Pseudo elements. This image shows results for an input element type="number":

Inspector for input type=number (Chrome)

Chloras answered 28/11, 2010 at 18:17 Comment(8)
It seems that Chrome no longer has a problem, so you can just use display: none; as the only thing inside the selectorKano
This is unfortunately not cross-browser so I'd advise against using type=number if you have to hide these arrows. E.g. currently they'd be still displayed in Opera and they'll start being displayed in Firefox, IE etc. when they implement this input type.Sudor
Just use `<input inputmode="numeric" ... /> It has wide support nowCocky
I would not call that "wide support"...Blindly
for some reason I can type in the letter 'e' in Chrome 94Salazar
@Salazar because letter 'e' can be a part of a valid number, i.e. 1e3 === 1000.Viperous
In 2022 it's ok to omit -webkit and -moz prefixes because all wide-spread browsers suport the property. See details on caniuse.com/?search=AppearanceKlaxon
Can't reproduce on stackoverflow but for me this on Chrome breaks text-align making it to a wider width then it actually is. Using only display: none; in the first rule works flawlessly.Chitterlings
K
493

Firefox 29 currently adds support for number elements, so here's a snippet for hiding the spinners in webkit and moz based browsers:

input[type='number'] {
    -moz-appearance:textfield;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}
<input id="test" type="number">
Keyser answered 21/3, 2014 at 12:45 Comment(0)
I
433

Short answer:

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
input[type="number"] {
    -moz-appearance: textfield;
}
<input type="number" />

Longer answer:

To add to existing answer...

Firefox:

In current versions of Firefox, the (user agent) default value of the -moz-appearance property on these elements is number-input. Changing that to the value textfield effectively removes the spinner.

input[type="number"] {
    -moz-appearance: textfield;
}

In some cases, you may want the spinner to be hidden initially, and then appear on hover/focus. (This is currently the default behavior in Chrome). If so, you can use the following:

input[type="number"] {
    -moz-appearance: textfield;
}
input[type="number"]:hover,
input[type="number"]:focus {
    -moz-appearance: number-input;
}
<input type="number"/>

Chrome:

In current versions of Chrome, the (user agent) default value of the -webkit-appearance property on these elements is already textfield. In order to remove the spinner, the -webkit-appearance property's value needs to be changed to none on the ::-webkit-outer-spin-button/::-webkit-inner-spin-button pseudo classes (it is -webkit-appearance: inner-spin-button by default).

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
<input type="number" />

It's worth pointing out that margin: 0 is used to remove the margin in older versions of Chrome.

Currently, as of writing this, here is the default user agent styling on the 'inner-spin-button' pseudo class:

input::-webkit-inner-spin-button {
    -webkit-appearance: inner-spin-button;
    display: inline-block;
    cursor: default;
    flex: 0 0 auto;
    align-self: stretch;
    -webkit-user-select: none;
    opacity: 0;
    pointer-events: none;
    -webkit-user-modify: read-only;
}
Inexperience answered 14/1, 2015 at 3:34 Comment(2)
i like this answer better because it includes relevant firefox info, which, when developing something, needs to be considered. webkit-only answers that ignore other browser engines leave much to be desiredLanguor
It's difficult to follow "current" and "as of writing" with all the edits. Maybe revise to reference browser versions or add actual dates.Pfaff
R
141

According to Apple’s user experience coding guide for mobile Safari, you can use the following to display a numeric keyboard in the iPhone browser:

<input type="text" pattern="[0-9]*" />

A pattern of \d* will also work.

Ricci answered 3/2, 2011 at 3:43 Comment(4)
This currently does nothing on Android, in all browsers. Tested on this test page in Browser 4.2.2, Chrome 28, and Firefox 23 on Android 4.2. Those browsers just show the standard text keyboard with this markup.Blameworthy
The above comment is obsolete, according to caniuse the most popular Android browsers support it now. Plus, even if it doesn’t do anything YET, it’s standard compliant and should be added, because browsers keep evolvingCrypto
You will loose numeric validation provided by min and max as well.Maidel
How about decimals? Scientific format, negative values, international formatting? Now the dev has to write regexes for all those possibilities?Shiv
T
105

I found a super simple solution using

<input type="text" inputmode="numeric" />

This is supported in most browsers:

Titty answered 3/6, 2020 at 18:13 Comment(4)
technology.blog.gov.uk/2020/02/24/… explains why UK gov switched from type="number" to this solution.Middlings
Can still type any character if you have a physical keyboardBalladry
@Balladry The OP and youjin never mentioned that it won't be possible. There lot of another posts handling this - for example #995683Hett
Heads up for anyone not reading the other answers on this page. There is no validation performed with this method. The developer has to provide a regex pattern to validate against (and/or a JS function). That would need to include all possibilities for the given input, eg. digits-only, min/max values, decimals, negatives, scientific notation, and international formats, just to name the obvious ones which type=number currently handles "automatically."Shiv
R
46

Try using input type="tel" instead. It pops up a keyboard with numbers, and it doesn’t show spin boxes. It requires no JavaScript or CSS or plugins or anything else.

Roseleeroselia answered 7/3, 2013 at 22:52 Comment(4)
This solution does not currently do any validation in any browsers like type=number does.Framboise
The telephone keyboard is not as good as the number keyboard, though it’s far better than the text keyboard. The telephone keyboard has irrelevant letters and symbols that are omitted from the number keyboard. (Tested with this page on Android 4.2.)Blameworthy
there is no "." in the tel keyboard :-<Coakley
This is not a solution, and by the HTML5 standards its stupid. input[tel] is for phone numbers, input[number] is for generic numbers, including floating point ones. So this is not a solution at all.Scintillate
T
21

If you are trying to hide controls the browser adds, it should be a hint that you are misusing that input type.

Standard Conformance Issues

From the HTML spec on type=number:

The type=number state is not appropriate for input that happens to only consist of numbers but isn't strictly speaking a number. For example, it would be inappropriate for credit card numbers or US postal codes.

(Emphasis is mine)

Usability Issues

The <input type=number> was found to be problematic during a usability study performed by the GOV.UK team. This team maintains one of the world's best known design systems and concludes that this input type:

Alternative Solution

Instead, they recommend to use <input type="text" inputmode="numeric" pattern="[0-9]*"> which comes down to the combined answers of @team_steve and @youjin. This not only hides the spin boxes, it also improves the overall usability of the element.

It comes without saying that additional validation needs to be done, along with a helpful error text.

<label>Postal Code
<input type="text" inputmode="numeric" pattern="[0-9]*" size="5">
</label>
Transudation answered 23/3, 2021 at 7:48 Comment(7)
Can still type in non-numeric characters - which is undesired. The usability thing is not always important, as the limitation effect of allowing to input only numbers, which is a huge plusBalladry
You can put in non-numeric characters but not submit the form. I'd argue that's a plus because this way you can detect errors and give a proper validation message. If someone mistypes but only can put in numbers, the values might be right according to the rules but not from a user point of view. For example if someone types in 1K, they mean 1000. But if the K can't be typed in, you cannot detect this mistake. The input will be 1, which is not what was meant.Transudation
I don’t understand your argument, @vsync. In such web-apps w/o <form> elements, validation needs to be implemented anyway. And an appropriate error message will be way more user friendly than messing with the user’s input. You got the evidence right in the links.Crypto
@Crypto - I've deleted the comment. I think I commented on the wrong answer, because it doesn't fit, so I just removed it all together.Balladry
The input correctly forbids non-numeric characters because of the supplied pattern. What does @Balladry mean that it does allow, maybe browser support is better for the pattern attribute now?Lowtension
Unfortunately, you can't use number specific attributes like min and max.Lowtension
How about decimals? Scientific format, negative values, international formatting? Now the dev has to write regexes for all those possibilities?Shiv
P
20

Only add this css to remove spinner on input of number

/* For Firefox */

input[type='number'] {
    -moz-appearance:textfield;
}



/* Webkit browsers like Safari and Chrome */

input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
Pleasance answered 8/8, 2018 at 9:51 Comment(0)
G
18
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}

Source

Gehenna answered 7/8, 2021 at 7:15 Comment(0)
T
12

I've encountered this problem with a input[type="datetime-local"], which is similar to this problem.

And I've found a way to overcome this kind of problems.

First, you must turn on chrome's shadow-root feature by "DevTools -> Settings -> General -> Elements -> Show user agent shadow DOM"

Then you can see all shadowed DOM elements, for example, for <input type="number">, the full element with shadowed DOM is:

<input type="number">
  <div id="text-field-container" pseudo="-webkit-textfield-decoration-container">
    <div id="editing-view-port">
      <div id="inner-editor"></div>
    </div>
    <div pseudo="-webkit-inner-spin-button" id="spin"></div>
  </div>
</input>

shadow DOM of input[type="number"

And according to these info, you can draft some CSS to hide unwanted elements, just as @Josh said.

Terenceterencio answered 30/12, 2015 at 11:30 Comment(0)
G
5

To make this work in Safari I found adding !important to the webkit adjustment forces the spin button to be hidden.

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    /* display: none; <- Crashes Chrome on hover */
    -webkit-appearance: none !important;
    margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}

I am still having trouble working out a solution for Opera as well.

Gerge answered 3/7, 2014 at 0:45 Comment(0)
W
5

This is more better answer i would like to suggest on mouse over and without mouse over

input[type='number'] {
  appearance: textfield;
}
input[type='number']::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button,
input[type='number']:hover::-webkit-inner-spin-button, 
input[type='number']:hover::-webkit-outer-spin-button {
-webkit-appearance: none; 
 margin: 0; }
Wilfredowilfrid answered 22/9, 2017 at 9:20 Comment(0)
B
4

Not what you asked for, but I do this because of a focus bug in WebKit with spinboxes:

// temporary fix for focus bug with webkit input type=number ui
if (navigator.userAgent.indexOf("AppleWebKit") > -1 && navigator.userAgent.indexOf("Mobile") == -1)
{
    var els = document.querySelectorAll("input[type=number]");
    for (var el in els)
        el.type = "text";
}

It might give you an idea to help with what you need.

Bingo answered 8/10, 2010 at 11:10 Comment(0)
D
4

Maybe change the number input with javascript to text input when you don't want a spinner;

document.getElementById('myinput').type = 'text';

and stop the user entering text;

  document.getElementById('myinput').onkeydown = function(e) {
  if(!((e.keyCode > 95 && e.keyCode < 106)
    || (e.keyCode > 47 && e.keyCode < 58) 
    || e.keyCode == 8
    || e.keyCode == 9)) {
          return false;
      }
  }

then have the javascript change it back in case you do want a spinner;

document.getElementById('myinput').type = 'number';

it worked well for my purposes

Dwell answered 26/9, 2016 at 12:44 Comment(0)
T
3

On Firefox for Ubuntu, just using

    input[type='number'] {
    -moz-appearance:textfield;
}

did the trick for me.

Adding

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

Would lead me to

Unknown pseudo-class or pseudo-element ‘-webkit-outer-spin-button’. Ruleset ignored due to bad selector.

everytime I tried. Same for the inner spin button.

Textile answered 18/11, 2016 at 21:16 Comment(0)
B
3

I needed this to work

/* Chrome, Safari, Edge, Opera */
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button
  -webkit-appearance: none
  appearance: none
  margin: 0

/* Firefox */
input[type=number]
  -moz-appearance: textfield

The extra line of appearance: none was key to me.

Bayard answered 31/12, 2019 at 7:32 Comment(0)
J
3

In WebKit and Blink-based browsers & All Kind Of Browser use the following CSS :

/* Disable Number Arrow */
input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button { 
  -webkit-appearance: none; 
  margin: 0; 
}
Jahdol answered 30/3, 2020 at 3:7 Comment(1)
others wrote the same thing years before, so why did you choose to add this still?Balladry
U
2

For me on chrome nothing has worked but display: none.

/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
  display: none;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}

Credits to @philfreo for pointing it out in the comments.

Upas answered 12/4, 2021 at 15:3 Comment(0)
F
1

input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button {
     -webkit-appearance: none;
<input id="test" type="number">
Forestry answered 17/2, 2017 at 8:41 Comment(0)
V
-2

This like your css code:

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
Vociferant answered 3/11, 2020 at 5:49 Comment(1)
Please don't repeat existing answers.Pfaff

© 2022 - 2024 — McMap. All rights reserved.