I do not care for a specific technology, it could be JS, CSS or even some unstandard and evil html attributes. I just want the input
to get bigger if user types over the right border.
Use the span. text to fit width of text, and let the input have same size with it by position: absolute to the container.
The simple way to make an input resize freely is to set it to it to have width: 100% , and then place it inside an inline-block container with a min-width ( min-width sets the default width for the input ). Resize the container to resize the input .
1. Set width in HTML. In HTML, you can use the width attribute to set the width of an element. Alternatively, you can also use the size attribute to define the width of the <input> .
Let the browser do the calculating of the width by using a div
with contenteditable
set to true
.
<div class="pseudo-input" contenteditable="true">Resizes to my content!</div>
There should be no issues with browser support http://caniuse.com/#feat=contenteditable
You can also create div element, copy your input style to it and use it's dimensions to resize input. (Chosen multiple uses this solution).
var input = $('input'),
dummy = $('<div style="position:absolute; left: -1000%;">').appendTo('body');
['font-size', 'font-style', 'font-weight', 'font-family',
'line-height', 'text-transform', 'letter-spacing'].forEach(function(index) {
dummy.css(index, input.css(index));
});
input.on('input', function() {
dummy.html(this.value);
input.width(dummy.width());
});
UPD: better to use pre instead div to follow spaces size.
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