<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.
Here's a modification of Lyth's answer that takes into account:
It also allows for any number of input fields! To see it in action: http://jsfiddle.net/4Qsa8/
Script:
Style:
HTML:
In modern browser versions, CSS unit
ch
is also available. To my understanding, it is font-independent unit, where1ch
equals to width of character0
(zero) in any given font.Thus, something as simple as following could be used as resize function:
That example would resize "elem" to length of the value + 2 characters extra.
You could do something like this
I just spend some time figuring out how to do it.
Actually the simplest way I found is to move input value to span just before the input, keeping input 1 symbol width. Though I can't be sure that it fit for your initial need.
Maybe it some extra code, but in react+flux based application it is quite natural solution.
It's worth noting that a nice-looking resize can be done when the font is monospaced, so we can perfectly resize the
input
element using thech
unit.Also in this approach we can update the width of the field by just updating a CSS variable (custom property) on
input
event and we should also take care of already pre-filled input onDOMContentLoaded
eventMarkup
CSS
As a root variable we set
--size: 0
: this variable will contain the length of the input and it will be multiplied by1ch
inside thecalc()
expression. By default we could also set a min-width, e.g.10ch
The Javascript part reads the length of the value inserted and updates the variable
--size
:JS
of course this still works even if you don't use monospaced font, but in that case you will need to change the
calc()
formula by multiplying the--size
variable by another value (which it's strictly dependent on thefont-family
andfont-size
) different than1ch
.Here is a plain JS and a jQuery plugin I wrote that will handle resizing an input element using a canvas and the font size / family to determine the actual string length when rendered. (only works in > IE9, chrome, safari, firefox, opera and most other major browsers that have implemented the canvas element).
PlainJS:
jQuery Plugin:
Note: this will not handle text-transforms, line spacing and letter spacing, and probably some other text size changing properties. To handle text-transform property set and adjust the text value to match that property. The others are probably fairly straight forward. I will implement if this starts gaining some traction...