Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make input inside flexbox size to available space? [duplicate]

Tags:

html

css

flexbox

I'm trying to get the input to take all available space inside this flexbox but not extend beyond the width of the flexbox.

<div>
   <input type="number"/>
   <button>OK</button>
</div>

div
{
  display:flex;
  width:100px;
  position:relative;
  border:solid 1px #000;
}

input
{
  flex:1;
  display:block;
  border:none;
}

The problem is that the input is overflowing the containing flexbox div. If I set the input's width to 100% it seems to work in Chrome but fail in FF. How should this be done?

https://jsfiddle.net/y7d7Ldur/

like image 433
Emanon Avatar asked Dec 24 '22 09:12

Emanon


2 Answers

This is because the input has a default width bigger than 100px. Add min-width:0 to the input to fix this:

div
{
  display:flex;
  width:100px;
  position:relative;
  border:solid 1px #000;
}

input
{
  flex:1;
  display:block;
  border:none;
  min-width:0;
} 
<div>
   <input type="number">
   <button>OK</button>
</div>
like image 132
Temani Afif Avatar answered Dec 26 '22 17:12

Temani Afif


You can give the div a max-width and activate flex with width: 0; on input

div {
  display: flex;
  max-width: 100px;
  position: relative;
  border: solid 1px #000;
}

input {
  display: flex;
  flex: 1 1 auto;
  border: none;
  width: 0;
}

button {
  display: flex;
  flex: 0 0 auto;
}
<div>
  <input type="number"/>
  <button>OK</button>
</div>
like image 28
Konstantin Kreft Avatar answered Dec 26 '22 19:12

Konstantin Kreft