make html text input field grow as I type?

前端 未结 12 1161
陌清茗
陌清茗 2020-11-27 12:02

I can set initial text input size in css, like so:

width: 50px;

But I would like it to grow when I type until it reaches for example 200px.

12条回答
  •  再見小時候
    2020-11-27 12:39

    If you're allowed to use the ch measurement (monospaced) it completely solved what I was trying to do.

    onChange(e => {
        e.target.style.width = `${e.target.length}ch`;
    })
    

    This was exactly what I needed but I'm not sure if it works for dynamic width font-families.

提交回复
热议问题