如何设置html输入标签宽度“ fit-content”在CSS中

发布于 2025-02-06 04:39:42 字数 555 浏览 2 评论 0原文

我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫漫岁月 2025-02-13 04:39:42

这是使用CH的很少使用的单位的好时机,该单元相对于当前字体和字体大小的“ 0”(零)的宽度相关!然后使用JS使宽度等于CH中的输入的长度,我在2个位置,一次加载时完成了此操作,然后在onkeypress上进行。因此编辑时它将扩展。在这里看看:

.number-input {
    width: fit-content;
    min-width: 120px;
}
<div class="price-input">
    <input type="text" id="mytxt" onkeypress="this.style.width = (this.value.length) + 'ch';" value="200001230001003002" class="number-input" onload="myFunction()">
    <button onload="myFunction()" class="save"></button>
</div>



<script>
function myFunction() {
  document.getElementById("mytxt").style.width = (mytxt.value.length) + 'ch';
}

window.onload = myFunction();
</script>

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 in ch I've done this in 2 places, once when it loads, and then subsequently on the onkeypress so that it will expand when edited. Take a look here:

.number-input {
    width: fit-content;
    min-width: 120px;
}
<div class="price-input">
    <input type="text" id="mytxt" onkeypress="this.style.width = (this.value.length) + 'ch';" value="200001230001003002" class="number-input" onload="myFunction()">
    <button onload="myFunction()" class="save"></button>
</div>



<script>
function myFunction() {
  document.getElementById("mytxt").style.width = (mytxt.value.length) + 'ch';
}

window.onload = myFunction();
</script>

羅雙樹 2025-02-13 04:39:42

你可以这样做。
我发布了两个带有跨度和数据*的解决方案。

HTML

    <p>solution with span <span class="number-input" role="textbox" contenteditable>200001230001003002</span></p>
    
    <p>Solution with data-*<label class="input-sizer" data-value="200001230001003002">
        <input class="number-input1" type="text" oninput="this.parentNode.dataset.value = this.value" size="1" value="200001230001003002">
    </label></p>
    <button class="save"></button>

-CSS

.number-input {
  border: 1px solid #ccc;
  font-family: inherit;
  font-size: inherit;
  padding: 1px 6px;
}

.number-input1{
  font-family: inherit;
  font-size: inherit;
}

.input-sizer {
  display: inline-grid;
  vertical-align: top;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  position: relative;
  font-family: inherit;
  font-size: inherit;
}  

.input-sizer::after 
{
    content: attr(data-value) "  ";
    visibility: hidden;
    font-family: inherit;
    font-size: inherit;
    white-space: pre-wrap; 
}

You can do like this.
I posted two solution with span and data-*.

html

    <p>solution with span <span class="number-input" role="textbox" contenteditable>200001230001003002</span></p>
    
    <p>Solution with data-*<label class="input-sizer" data-value="200001230001003002">
        <input class="number-input1" type="text" oninput="this.parentNode.dataset.value = this.value" size="1" value="200001230001003002">
    </label></p>
    <button class="save"></button>

-css

.number-input {
  border: 1px solid #ccc;
  font-family: inherit;
  font-size: inherit;
  padding: 1px 6px;
}

.number-input1{
  font-family: inherit;
  font-size: inherit;
}

.input-sizer {
  display: inline-grid;
  vertical-align: top;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  position: relative;
  font-family: inherit;
  font-size: inherit;
}  

.input-sizer::after 
{
    content: attr(data-value) "  ";
    visibility: hidden;
    font-family: inherit;
    font-size: inherit;
    white-space: pre-wrap; 
}
窗影残 2025-02-13 04:39:42

尝试这个

.number-input {
  width: -moz-fit-content;
  width: fit-content;
  background-color: #8ca0ff;
  padding: 5px;
  margin-bottom: 1em;
}

.container {
  border: 2px solid #ccc;
  padding: 10px;
  width: 20em;
}
<div class="container">
 <div class="price-input">
  <input type="text" value="200001230001003002" class="number-input">
  <button>Click it</button>
  </div>
</div>

try this one

.number-input {
  width: -moz-fit-content;
  width: fit-content;
  background-color: #8ca0ff;
  padding: 5px;
  margin-bottom: 1em;
}

.container {
  border: 2px solid #ccc;
  padding: 10px;
  width: 20em;
}
<div class="container">
 <div class="price-input">
  <input type="text" value="200001230001003002" class="number-input">
  <button>Click it</button>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文