<html>
<head>
</head>
<body>
<input type="text" value="1" style="min-width:1px;" />
</body>
</html>
This is my code and it is not working. Is there any other way in HTML, JavaScript, PHP or CSS to set minimum width?
I want a text input field with a dynamically changing width, so that the input field fluids around its contents. Every input has a built-in padding of 2em
, that is the problem and second problem is that min-width
ain't working on input at all.
If I set width more than it is needed than the whole program is messy, I need the width of 1px, more only if it's needed.
It sounds like your expectation is that the style be applied dynamically to the width of the textbox based on the contents of the textbox. If so you will need some js to run on textbox contents changing, something like this:
Note: this solution only works when every character is exactly
8px
wide.Just adding on top of other answers.
I noticed that nowadays in some browsers the input field has a scrollWidth. Which means:
should work nicely. tested in chrome, firefox and safari.
For deletion support, you can add '=0' first and then readjust.
FOR A NICER LOOK&FEEL
You should use jQuery keypress() event in combination with String.fromCharCode(e.which) to get the pressed character. Hence you can calculate what your width will be. Why? Because it will look a lot more sexy :)Here is a jsfiddle that results in a nice behaviour compared to solutions using the keyup event : http://jsfiddle.net/G4FKW/3/
Below is a vanilla JS which listens to the
input
event of an<input>
element and sets aspan
sibling to have the same text value in order to measure it.You can do it even simpler in angularjs using the built-in ng-style directive.
In your html:
In your controller:
In your css:
Adjust the charWidth to match the width of your font. It seems to be 7 at a font-size of 12px;
To calculate the width of the current input, you'll have to embed it in a temporary
span
element, attach that thing to the DOM, get the computed width (in pixels) using thescrollWidth
property and remove thespan
again. Of course you'll have to ensure that the same font family, font size, etc., is used in theinput
as well as in thespan
element. Therefore I assigned the same class to them.I attached the function to the
keyup
event, as onkeypress
the input character is not yet added to the inputvalue
, so that will result in the wrong width. Unfortunately, I don't know how to get rid of the scrolling of the input field (when adding characters to the end of the field); it scrolls, because the character is added and shown beforeadjustWidthOfInput()
is called. And, as said, I can't do this the other way round because then you'll have the value of the input field before the pressed character is inserted. I'll try to solve this issue later.BTW, I only tested this in Firefox (3.6.8), but you'll get the point, I hope.
Here is my 2 cents. Create an empty invisible div. Fill it with the input content and return the width to the input field. Match text styles between each box.