如何设置html输入标签宽度“ fit-content”在CSS中
我的HTML页面中有一个输入标签,如下所示。
我想制作一个输入标签宽度以适合内容和最小宽度。 所以我写了CSS如下。
.number-input {
width: fit-content;
min-width: 120px;
}
<div class="price-input">
<input type="text" value="200001230001003002" class="number-input">
<button class="save"></button>
</div>
是否可以调整CSS,以使输入适合文本宽度?
我已经搜索了,但是答案使用JavaScript函数。
请任何人帮助。
I have an input tag in my HTML page as following.
I want to make an input tag width to fit the content and minimum width.
So I wrote CSS as follows.
.number-input {
width: fit-content;
min-width: 120px;
}
<div class="price-input">
<input type="text" value="200001230001003002" class="number-input">
<button class="save"></button>
</div>
Is it possible to adjust CSS so the input will fit the text width?
I searched already, but the answers use the javascript function.
Please anyone help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是使用
CH
的很少使用的单位的好时机,该单元相对于当前字体和字体大小的“ 0”(零)的宽度相关!然后使用JS使宽度等于CH
中的输入的长度,我在2个位置,一次加载时完成了此操作,然后在onkeypress
上进行。因此编辑时它将扩展。在这里看看:This is a great time to use that rarely used unit of
ch
which is relative to the width of the "0" (zero) of the current font and font sized used! Then use JS to make the width equal to the length of the input inch
I've done this in 2 places, once when it loads, and then subsequently on theonkeypress
so that it will expand when edited. Take a look here:你可以这样做。
我发布了两个带有跨度和数据*的解决方案。
HTML
-CSS
You can do like this.
I posted two solution with span and data-*.
html
-css
尝试这个
try this one