Number input halves in size when using min and max - html

When making an input, like so:
<input type="number" min="0" max="100"><br>
<input type="number" min="0" max="99"><br>
<input type="number" min="1" max="100"><br>
<input type="number" min="1" max="99"><br>
It shortens the width of the input field, specifically when max is at 100. Since i've never seen this, i can only guess it does this thinking i will use percentages or currency, which i actually do, but i do not want this. Is there another way to prevent this from happening besides using different values or changing the size of the input yourself?

This is the default behaviour of how some* browsers render those inputs. So the answer is No. If you want to have certain width, take a look at the other answers.
* Different browsers render elements differently:
And this is the result of your snippet in Mozilla Firefox:

The input field width adapts to the width of the highest value: 100. 99 has one less character, so the input field is shorter. I don't think you can prevent that difference without CSS if your max values have a different character length.
Now, CSS should be a piece of cake....
<style>.medium-input {width:100px;}</style>
<input type="number" min="0" max="100" class="medium-input"><br>
<input type="number" min="0" max="99" class="medium-input"><br>
<input type="number" min="1" max="100" class="medium-input"><br>
<input type="number" min="1" max="99" class="medium-input"><br>
I'm adding the same class to all of the inputs, so that you can also define some other lengths if you like.
You could also do it with input[type=number] as a selector.

you can add css to control the width of inputs.
this will do the job :
input[type=number]{
width: 150px;
}

Related

Setting min and max on input removes spacing?

For some reason, when I set min and max values on an input like so:
<p>The placeholder of the second input is cut off (i.e. input is collapsed) when the input is assigned a min and max value?</p>
<input type="number" placeholder="This is my placeholder.">
<input type="number" placeholder="This is my placeholder." min="1" max="15">
The input collapses into a small space:
Example
I'd like to prevent this from happening without having to set a "width" value on the input. Is this possible?
Thanks!
The browser is rendering the <input type="number" /> to the size that will fit its maximum content:
<label for="99">Max is 99</label>
<input name="99" id="99" type="number" min="1" max="99">
<br/>
<label for="99">Max is 999</label>
<input name="99" id="99" type="number" min="1" max="999">
<br/>
<label for="99">Max is 9999</label>
<input name="99" id="99" type="number" min="1" max="9999">
<br/>
<label for="99">Max is 99999</label>
<input name="99" id="99" type="number" min="1" max="99999">
<br/>
<label for="99">Max is 999999</label>
<input name="99" id="99" type="number" min="1" max="999999">
If you want to enter some placeholder value that exceeds that content, you'll have to resize it with CSS; the size and width attributes are not valid for the "number" type.
Also, as A Haworth points out in the comments, the usefulness of the placeholder here is rather limited-- in fact, I would challenge that perhaps placeholders are of rather limited benefit generally. Consider that if this text is important enough to be on the page, it is important enough to be displayed as part of the label, where it is always visible and of sufficient contrast that it can be easily seen and read.

How to get HTML 5 input type range with string values

I need HTML 5 slider with three text value instead of integer. for example one I need following three value in slider.
likely
unlikely
very likely
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
This would give you the HTML input you want:
<input type="range" min="1" max="3" value"1" class="slider" id="myRange">
From there you could put text above/below it for display purposes. You'd simply save the integer as a value, or change it to text server-side.

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.
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">
Simply set a width for input.
input {
width: 100px;
}
for example.

Increase the size of the down and up arrow on the number input, CSS, HTML

Is there a way to increase the size of the up and down arrow on the right of the number input box by using CSS? Just the up and down arrows, not the whole input box, or at least proportionally. See this example:
.size-36 {
font-size: 36px;
}
.size-12 {
font-size: 12px;
}
<input type="number" class="size-36" value="2" min="0" max="10" step="1">
<input type="number" class="size-12" value="2" min="0" max="10" step="1">
<input type="number" value="2" min="0" max="10">
Current result:
There is no official way to style number input elements; you will have to come with a hack. These answers shows a few ways to do it:
Styling a input type=number
Making up down arrow of HTML's input number very bigger and cleaner
The main problem is where to put the bigger arrows. You don't want to change the size of the input which means the arrows can only grow wider (or they wouldn't fit into the input anymore). You will have to think of a way to solve this.
Possible solutions: Show the arrows after the input element, hide them unless you hover over the element, use cursor keys to increase/decrease the number, so you don't need a mouse at all.
You can just change the font size of the input 'number' element. This will increase the size of the arrow buttons too. Maybe not exactly what you're looking for, but anyway =
<input type="number" value="1" min="1" max="8" style="font-size: 55px;">

Styling HTML5 input type number

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?
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;
}
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.
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
There are only 4 specific atrributes:
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.
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.
max - Apprently, this represents the biggest number of the number input.
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.
Also you can replace size attribute by a style attribute:
<input type="number" name="numericInput" style="width: 50px;" min="0" max="18" value="0" />
There is a way:
<input type="number" min="0" max="100" step="5"/>
<input type="number" name="numericInput" size="2" min="0" maxlength="2" value="0" />