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.
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.
The max attribute defines the maximum value that is acceptable and valid for the input containing the attribute.
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.
Simply set a width
for input.
input {
width: 100px;
}
for example.
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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With