5

I have and input with icon, looking like this: screenshot

<div class="input-with-icon">
  <input type="text" value="abcdefeghijklmnopqrtvuvwxyz" class="form-control">
  <span class="icon icon-calendar"></span>
</div>

live demo

Is it possible to adjust CSS so the input will always fit the text width? e.g. it will stretch as I type.

currently, .input-with-icon has fixed width, but if I set it to auto, input won't stretch.

Liero
  • 25,216
  • 29
  • 151
  • 297
  • 1
    Possible duplicate of [make html text input field grow as I type?](http://stackoverflow.com/questions/7168727/make-html-text-input-field-grow-as-i-type) – ca8msm Nov 10 '16 at 10:06
  • 1
    I don't think this is possible without using JavaScript. The reason is that when you are entering text into `` or ` – Narxx Nov 10 '16 at 13:32

3 Answers3

1
<input id="txt" type="text" value="Hello World!" />
input {
min-width:50px!important;
max-width:99.99%!important;
transition: width 0.25s;
text-align:center;
}
function resizable (el, factor) {
var int = Number(factor) || 7.7;
function resize() {el.style.width = ((el.value.length+1) * int) + 'px'}
var e = 'keyup,keypress,focus,blur,change'.split(',');
for (var i in e) el.addEventListener(e[i],resize,false);
resize();
}
resizable(document.getElementById('txt'),7);
-2

You have many option for that. The First option is to change your <input type="text"> into <textarea class="form-control"></textarea>. <textarea> can handle much better characters than <input type="text">.
The Second option is use this example. This example is from @megakorre in this link. Dont forget to Vote up or put a check if you got the answer. That's all dude! Happy Coding! :)

Community
  • 1
  • 1
  • I changed input to textarea, set rows to 1, set white-space to nowrap and still not working. I got scrollbars. Javascript solution is not acceptable – Liero Nov 10 '16 at 10:43
-3
<div class="input-with-icon">
<input type="text" value="tes" class="form-control">
</div>
input[type="data-autosize-input"] {
 width: 90px;
min-width: 90px;
 max-width: 300px;
 transition: width 0.25s;    
 }