Specify width of 3 chars for an html input text

爱⌒轻易说出口 提交于 2021-02-18 09:00:24

问题


How would I create an HTML text box that exactly 3 characters can fit into it?
I found this online: <input type="text" style="width: 10px; padding: 2px; border: 1px solid black"/>
It creates a text box with width of 10px. I could use this but I was wondering if we could explicitely set for 3 chars width instead of "playing" with pixels,


回答1:


Not reliably, since l and W have very different widths.

However, if you set the font to monospace, that helps. Then, you should be able to set size="3" on the input element, and in theory it should be exactly three characters wide.




回答2:


Use the ch metric.

width: 3ch;

Make sure your box-sizing property is set to content-box if you're changing left and/or right padding.



来源:https://stackoverflow.com/questions/16886674/specify-width-of-3-chars-for-an-html-input-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!