48

I'm writing a form in HTML5. One of the inputs is type=number. I want the input to only show 2 digits but it seems to default to showing at least 5 digits, which takes up extra space. I've tried adding size="2" attribute, with no effect.

This the tag i'm using:

<input type="number" name="numericInput" size="2" min="0" max="18" value="0" />

What am I missing?

1
  • 1
    If a max attribute is present and there is no width the input should adjust its size to the max value. At least that is the case in google chrome. Commented Jul 28, 2017 at 8:36

7 Answers 7

59

HTML5 number input doesn't have styles attributes like width or size, but you can style it easily with CSS.

input[type="number"] {
   width:50px;
}
Sign up to request clarification or add additional context in comments.

2 Comments

To clarify, size does still exist for HTML5, but it is only effective for these types: text, search, tel, url, email, and password w3schools.com/tags/att_input_size.asp
Which makes sense. size refers to the length of the text, so it only applies to strings.
22

I have been looking for the same solution and this worked for me...add an inline css tag to control the width of the input.

For example:

<input type="number" min="1" max="5" style="width: 2em;">

Combined with the min and max attributes you can control the width of the input.

2 Comments

The problem with this solution is that this might clip the visibility of the numbers that should be displayed since part of the input number box's width is the up and down arrows contained within it.
I like this solution (or something like style="max-width: 3em;") compared to the other alternatives - at least the width is relative to the font size being used
11

Unfortunately in HTML 5 the 'pattern' attribute is linked to only 4-5 attributes. However if you are willing to use a "text" field instead and convert to number later, this might help you;

This limits an input from 1 character (numberic) to 3.

<input name=quantity type=text pattern='[0-9]{1,3}'>

The CSS basically allows for confirmation with an "Thumbs up" or "Down".

Example 1

Example 2

5 Comments

This is so frustrating. I searched a long time before finding your answer.
The number state has a 'max' attribute, just set that to 999 instead
@Dane, Your script is not working. The green thumbs up is not showing.
Sorry @Dane, this doesn't work at all. Also, there are other differences between text and number inputs, other than parsing. Main reason I use the proper type of input is for virtual keyboards (phones) to show the relevant keys.
The pattern attribute only performs validation on submission but does not prevent me from inserting invalid characters
3

There are only 4 specific atrributes:

  1. value - Value is the default value of the input box when a page is first loaded. This is a common attribute for element regardless which type you are using.
  2. min - Obviously, the minimum value you of the number. I should have specified minimum value to 0 for my demo up there as a negative number doesn't make sense for number of movie watched in a week.
  3. max - Apprently, this represents the biggest number of the number input.
  4. step - Step scale factor, default value is 1 if this attribute is not specified.

So you cannot control length of what user type by keyword. But the implementation of browsers may change.

1 Comment

Ok, so, no way currently to style the number input box to be less wide.
1

Also you can replace size attribute by a style attribute:
<input type="number" name="numericInput" style="width: 50px;" min="0" max="18" value="0" />

Comments

-3

There is a way:

<input type="number" min="0" max="100" step="5"/>  

1 Comment

This doesn't quite answer the question, at least not for what I tried. I use chrome, windows 10
-5
<input type="number" name="numericInput" size="2" min="0" maxlength="2" value="0" />

1 Comment

Please edit your answer, and explain why this solves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.