I'd like to change the font size into an <input type="text" size="10>
dynamically to fill the box. Here are some examples:
With less chars:
With more chars:
Is it possible?
To dynamically change the size of font size based on text length using JavaScript, we can resize the text if the height of the containing div is bigger than the div containing the text. to add the input to enter the text and the outer and inner divs. The inner div will contain the text. to select the elements with document.querySelector.
Answer: By using style attribute in <input> tag you can give width and Height to HTML input text box. See the below simple example of it. Output: See the below changed width and Height to HTML input text box.
Size Input Text Box Using the Size attribute in the input html tag will indicating how many characters wide the input field should be. The value is numeric. Where the value must be a number greater than 0, and the default value is 20.
Observation: With this code, the textbox width is not returning back to normal once the text is cleared. This dynamic calculation will highly depend on font size and this is not gonna work always . Unfortunately this will only increase the size of the input. If you delete characters the size will not decrease.
I know JQuery and JavaScript weren't mentioned or tagged, but what you want will need JavaScript, and the JQuery.InputFit plugin will do exactly what you want:
<input type="text" name="younameit" id="input">
<script type="text/javascript">
$('input').inputfit();
</script>
I'm a little late to this question, but I want offer a solution in case you don't want to implement jquery just for this:
<p>A function is triggered when the user releases a key in the input field. The function transforms the characters size.</p>
Enter your name: <input type="text" id="fname" onkeyup="myFunction()">
function myFunction(){
var x=document.getElementById("fname");
var initialSize=25-x.value.length;
initialSize=initialSize<=10?10:initialSize;
x.style.fontSize = initialSize + "px";
}
check out this jsfiddle
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