2

I have a label and button, and I have given a fixed width to the button, if label text is less, it is taking given width but when label text increased button is taking less width.

Why is it happening ?

I didn't gave any width to label.

HTML

<div>
  <label>Some text now this text is incrasinf </label>
  <button>Button</button>
</div>

CSS

div {
  border: solid green;
  width: 200px;
  display: flex;
}

label {
    border: solid blue;
    display: flex;
    flex-grow:1;
}

button {
  width: 100px;
}

When label text is small button is taking 100px as expected.

enter image description here

When the label text increased button is taking less than 100px (unexpected for me)

enter image description here

Codepen Link https://codepen.io/anon/pen/OGmMpd

kukkuz
  • 41,512
  • 6
  • 59
  • 95
Rahul Kumar
  • 2,184
  • 3
  • 24
  • 46

1 Answers1

3

Add flex-shrink: 0 to the button - the default is flex-shrink: 1 and so according to the change in content flex items can shrink.

See demo below:

div {
  border: solid green;
  width: 200px;
  display: flex;
}

label {
  border: solid blue;
  display: flex;
  flex-grow: 1;
}

button {
  width: 100px;
  flex-shrink: 0; /* added */
}
<div>
  <label>Some text now this text is incrasinf </label>
  <button>Button</button>
</div>
kukkuz
  • 41,512
  • 6
  • 59
  • 95