How can I get a textfield that expands it's width to accommodate the width of input while typing?
Asked
Active
Viewed 952 times
2
-
possible duplicate of [Liquid textfield width](http://stackoverflow.com/questions/5201621/liquid-textfield-width) – Christian Nov 27 '11 at 11:00
-
I'll just leave this here: http://stackoverflow.com/questions/118241/calculate-text-width-with-javascript – Matthew Nov 27 '11 at 16:46
2 Answers
0
Depends what browsers are you trying to target. New browsers probably support some way of doing this only via CSS, but I doubt IE6 has such a functionality.
So if you need legacy browser support, then I suggest sticking with JavaScript.

Jakub Arnold
- 85,596
- 89
- 230
- 327
-
"Probably"? You mean to say "CSS is probably the solution to any scripting needs"? As I see it, CSS is for presentation not behaviour. The OP's need is strictly in script land. – Christian Nov 27 '11 at 10:59
-
@ChristianSciberras the original question before editing was if it is possible to do this without JavaScript ... – Jakub Arnold Nov 27 '11 at 11:52
-
Ah, I see. Excuse my prejudice. Nowadays it's all too common to find people expecting magic out of raw CSS. – Christian Nov 27 '11 at 12:50
-1
With CSS just set the width
to auto
.

SomeKittens
- 38,868
- 19
- 114
- 143

Charming Prince
- 491
- 3
- 11
-
2Of course it won't work. An input box is different from a span or div, hence why auto width won't work. Perhaps messing with appearance CSS attrib might help, but it's a huge hack. – Christian Nov 27 '11 at 10:58
-
@amiawizard you should also consider the width of any container element where this textfield is placed. – Charming Prince Nov 27 '11 at 11:06