0

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?

Navjot Singh
  • 678
  • 7
  • 18
  • it's the padding and the border that make a difference, the default one applied to input – Temani Afif Aug 27 '20 at 14:02
  • No I have defined padding for input in the css, so it is not default anymore. Also I have checked these things using chrome dev tools. I can assure you that there are no hidden borders or paddings. – Navjot Singh Aug 27 '20 at 14:05
  • in all the cases it's the padding and border, even if it's not the default one (the border is still the default one in your case) – Temani Afif Aug 27 '20 at 14:16
  • I have defined border as well now, and nothing has changed. It is not about paddings and borders as I have looked for these things using the chrome dev tools. – Navjot Singh Aug 27 '20 at 14:19
  • nothing will change because the issue is with defining the border and padding. The existance of padding/border is the issue (either by default or defined). Remove them and your element will have equal width (There is a duplicate explaining this, no able to find it for now but will do) – Temani Afif Aug 27 '20 at 14:26
  • it does: https://jsfiddle.net/2e3svcza/ – Temani Afif Aug 27 '20 at 14:29
  • any alternative solution that allows me to keep the padding? – Navjot Singh Aug 27 '20 at 14:39

1 Answers1

1

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?

Temani Afif
  • 245,468
  • 26
  • 309
  • 415