<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.
I really liked Lyth's answer, but also really wanted it to:
I adapted his JSFiddle and came up with this. One improvement not present in this fiddle would be to use something like the jQuery CSS Parser to actually read the initial width from the input.textbox-autosize rule, and use that as the minWidth. Right I'm simply using an attribute on the , which makes for a compact demo but is not ideal. as it requires an extra attribute on each input. You might also just want to put the minWidth as 100 right in the JavaScript.
HTML:
CSS:
JavaScript:
I think you're misinterpreting the min-width CSS property. min-width is generally used to define a minimum DOM width in a fluid layout, like:
That would set the input element to a minimum width of 200 pixels. In this context, "px" stands for "pixels".
Now, if you're trying to check to make sure that input field contains at least one character when a user submits it, you'll need to do some form validation with JavaScript and PHP. If that is indeed what you're attempting to do, I'll edit this answer and do my best to help you out.
If you use Bootstrap, it could be done very easily:
<div contenteditable="true" class="form-control" style="display: inline"></div>
You will just need to fetch div's content and put it in a hidden input before submitting the form.
This answer provides one of the most accurate methods of retrieving text width available in the browser and is more accurate than the accepted answer. It uses the canvas html5 element and unlike other answers does not add the element into the DOM and thus avoids any reflow issues caused by excessively adding elements to the DOM.
Read more about the Canvas element here in relation to text width.
Here is an alternative way to solve this using a DIV and the 'contenteditable' property:
HTML:
CSS: (to give the DIV some dimensions and make it easier to see)
Note: If you are planning on using this inside of a FORM element that you plan to submit, you will need to use Javascript / jQuery to catch the submit event so that you can parse the 'value' (
.innerHTML
or.html()
respectively) of the DIV.Here is a solution without monospaced font needed, with only very small piece code of javascript, do not need calculate computed styles, and, even support ime, support rtl texts.
Use the
span.text
to fit width of text, and let the input have same size with it byposition: absolute
to the container. Copy value of input to thespan
every time it changed (you may change this piece of code to vanilla js easily). So the input will just "fit" the size of its content.