Navjot Singh
Navjot Singh

Reputation: 696

Flex-Grow leading to different widths depending on the markup

I am designing a form using flexbox. The input fields of the form are grouped in a ul. Each li groups a label and the corresponding input markup. A working snippet is as follows:

* {
  box-sizing: border-box;
}

div {
  width: 400px;
}

.form-input {
  padding: 4px;
  border: 1px solid #565656;
}

.form-input-list {
  list-style-type: none;
  width: 100%;
  padding-left: 0;
}

.form-input-item {
  display: flex;
  align-items: center;
}

.form-label {
  flex-basis: 0;
  flex-grow: 1;
  background-color: yellow;
}

.form-input,
.form-input-group {
  flex-basis: 0;
  flex-grow: 1.5;
}

.form-input-group {
  display: flex;
}

.form-input-group-item {
  flex-basis: 0;
  flex-grow: 1;
}
<div>
  <form>
    <ul class="form-input-list">
      <li class="form-input-item">
        <label class="form-label">First Name</label>
        <input type="text" class="form-input" />
      </li>
      <li class="form-input-item">
        <label class="form-label">Last Name</label>
        <input type="text" class="form-input" />
      </li>
      <li class="form-input-item">
        <label class="form-label">Phone</label>
        <div class="form-input-group">
          <select class="form-input-group-item form-input">
            <option>+49</option>
          </select>
          <input class="form-input-group-item form-input" type="text" />
        </div>
      </li>
    </ul>
  </form>
</div>

So, here each form-input-item is a flex container. The first two form-input-item have the input element directly as flex child, however the last form-input-item groups the two input elements in a div with class form-input-group. The form-label has a flex-grow of 1, form-input has a flex-grow of 1.5 and form-input-group also has a flex-grow of 1.5.

Since, the form-input and form-input-group are flex child to the form-input-item, I expect them to have same width with a flex-grow of 1.5 for both of them. However, if we run the above snippet we can see that form-input-group has a smaller width as compared to form-input. The only difference is that we have an extra div that wraps the input elements. Also form-input-group is itself a flex container as well.

The flex basis is 0. Why does the flex-grow behave like this? How to fix this?

EDIT As Temain Afif suggested in the comments, that this width issue is because of the padding and border that I have defined. I need the padding for the input elements for a better look. Is there a solution that allows me to keep the padding without causing the width and alignment issues?

Upvotes: 0

Views: 349

Answers (1)

Temani Afif
Temani Afif

Reputation: 273626

Simply keep the same structure and wrap all the input in div element to get the same result:

* {
  box-sizing: border-box;
}

div {
  width: 400px;
}

.form-input {
  padding: 4px;
  border: 1px solid #565656;
}

.form-input-list {
  list-style-type: none;
  width: 100%;
  padding-left: 0;
}

.form-input-item {
  display: flex;
  align-items: center;
}

.form-label {
  flex-basis: 0;
  flex-grow: 1;
  background-color: yellow;
}

.form-input,
.form-input-group {
  flex-basis: 0;
  flex-grow: 1.5;
}

.form-input-group {
  display: flex;
}

.form-input-group-item {
  flex-basis: 0;
  flex-grow: 1;
}
<div>
  <form>
    <ul class="form-input-list">
      <li class="form-input-item">
        <label class="form-label">First Name</label>
        <div class="form-input-group">
          <input type="text" class="form-input" />
        </div>
      </li>
      <li class="form-input-item">
        <label class="form-label">Last Name</label>
        <div class="form-input-group">
          <input type="text" class="form-input" />
        </div>
      </li>
      <li class="form-input-item">
        <label class="form-label">Phone</label>
        <div class="form-input-group">
          <select class="form-input-group-item form-input">
            <option>+49</option>
          </select>
          <input class="form-input-group-item form-input" type="text" />
        </div>
      </li>
    </ul>
  </form>
</div>

Related questions to get more details about the calculations:

Why is padding expanding a flex item?

How does flex-shrink factor in padding and border-box?

Upvotes: 1

Related Questions