-2

min-content and max-content don't seem to care about the input element's value:

input {
  border: 1px solid black;
  width: min-content;
}
<input type="text">

CSS totally ignores the input's value when figuring out min-content, just using some default width that never changes no matter the contents:

enter image description here

How can I make the CSS take the "content" (value) width into account:

enter image description here

Without using javascript to set width, min-width or max-width

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • @tacoshy no because the answers very clearly set `width`, `min-width`, `max-width` – theonlygusti Sep 12 '21 at 21:21
  • there are no other answer you can get. CSS has no way in setting the width to fit the content. The only way to do it is through scripting but you obviosly can't set a width of an element if you not allowed to use width. – tacoshy Sep 12 '21 at 21:26
  • 1
    Consider using a [`contenteditable`](//developer.mozilla.org/docs/Web/HTML/Global_attributes/contenteditable) element instead. – Sebastian Simon Sep 12 '21 at 21:28
  • @tacoshy the top answer on the duplicate has a solution to this question: set `size` using JS and `width: auto;` – theonlygusti Sep 12 '21 at 21:34

1 Answers1

5

It is not possible without JavaScript.

You have to programmatically set the input's width to the length of its value. This can be simplified with the ch unit.

document.querySelector('input').addEventListener('input', function() {
  this.style.width = this.value.length + 'ch'
})
<input type="text">
Spectric
  • 30,714
  • 6
  • 20
  • 43