Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input number max attribute resizes field

When adding a max value to an input number field in Chrome, it will re-size the field according to width of highest value.

It seems that I cannot control the re-sizing behavior.

<input type="number" name="" value="3500" placeholder="3500" min="500">
<br>
<input type="number" name="" value="3500" placeholder="3500" min="500" max="100000">
<br>
<input type="number" name="" value="3500" placeholder="3500" min="500" max="100000000">

Question: How can I make it behave like without max attribute or a normal text field.

PS: I cannot see any changes in the styles when switch the type. PPS: I must say that I already tried using -webkit-appearance: textfield; but Chrome was already using textfield by default.

like image 383
yesman82 Avatar asked Oct 22 '15 14:10

yesman82


People also ask

How do you limit a number in input field?

To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

Which attribute is used to specifies the maximum value for an input field?

The max attribute defines the maximum value that is acceptable and valid for the input containing the attribute.

How do you set the maximum length of an input type number?

The <input> maxlength attribute is used to specify the maximum number of characters enters into the <input> element. Attribute Value: number: It contains single value number which allows the maximum number of character in <input> element. Its default value is 524288.


2 Answers

Simply set a width for input.

input {
  width: 100px;
}

for example.

like image 81
mstruebing Avatar answered Nov 09 '22 00:11

mstruebing


Solution with percentage and div

Although it's been almost 6 years since this question was posted, just a few minutes ago I found myself in the same situation as the OP. The solution is to add a div that acts as a container for the input field. Then to the width of the input we add the rule for fill all the available space and at this point we can just change the size of the div wrapper as desired.

The first two inputs show what happens without div. The last two inputs have the same size despite the different 'max' value.

p{margin-bottom: 0;}

.wrapped{
    width: -moz-available;
    width: -webkit-fill-available;
    width: fill-available;
}
.inputWrapper{width: 30%;}
<p>Min 0, max 10</p>
<input type="number" min="0" max="10">

<p>Min 0, max 1000000</p>
<input type="number" min="0" max="1000000">

<div class="inputWrapper">
    <p>Wrapped: min 0, max 10</p>
    <input class="wrapped" type="number" min="0" max="10">

    <p>Wrapped: min 0, max 1000000</p>
    <input class="wrapped" type="number" min="0" max="1000000">
</div>

About the auto-resize of the input field

I honestly didn't read what the documentation says about this automatic resize and I was surprised when I noticed this unexpected and undesired behaviour. From the following simple tests we can see that the resize occurs only if both values 'min' and 'max' are set: the resize seems to be proportional only to the distance between max-0 instead of the distance max-min. Note that this is the default behavior without the proposed solution.

p{margin-bottom:0;}
<p>Min 0</p>
<input type="number" min="0">

<p>Min 100</p>
<input type="number" min="100">

<p>Max 0</p>
<input type="number" max="0">

<p>Max 100</p>
<input type="number" max="100">
<p> The previous input fields have the same width<p>

<p>Min 0, max 10</p>
<input type="number" min="0" max="10">

<p>Min 0, max 100</p>
<input type="number" min="0" max="100">

<p>Min 99, max 100</p>
<input type="number" min="99" max="100">
like image 2
Wippo Avatar answered Nov 08 '22 23:11

Wippo