0

I've had a bit of an issue with some flex containers. They're a label and an input in a flex container. Unfortunately, if I change the height of the container from auto, the contents cease aligning - the label's text remains at the start of the flexbox, but the input's text follows the middle. I've written a MWE to demonstrate.

label, input {
  border-style: solid;
  border-width: 2px;
  border-color: red;
  background-color: black;
}

.stretch {
  display:flex;
  height: 4em;
}
.baseline {
  display: flex;
  height: 4em;
  align-content: baseline;
}
.flex-innards {
  display: flex;
  height: 4em;
  align-content: stretch;
}
.flex-innards * {
  display: flex;
  align-items: center;
}
.flex-innards-start {
  display: flex;
  height: 4em;
  align-content: stretch;
}
.flex-innards-start * {
  display: flex;
  align-items: flex-start;
}

.short {
  height: auto;
}
<div class="stretch">
  <label>Stretch alignment</label>
  <input value=":("></input>
</div>

<div class="baseline">
  <label>How about baseline?</label>
  <input value=">:("></input>
</div>

<div class="flex-innards">
  <label>Flexing the children kinda works</label>
  <input value=":o"></input>
</div>

<div class="flex-innards-start">
  <label>But only if they're centered</label>
  <input value=":("></input>
</div>

<div class="field short">
  <label>Deceptive stretch</label>
  <input value=":S"></input>
</div>

https://jsbin.com/nulobolulo/edit?html,css,output.

I've managed to make them vertically centered together in the third box there (using How to vertically align and stretch content using CSS flexbox), but I'd like to have them both have their text aligned to flex-start, as well as filling the complete height of their container.

If possible, I'd like to avoid adding extra elements to the HTML. Thank you.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
OctarineSorcerer
  • 445
  • 3
  • 10

1 Answers1

0

I think the only way to address this problem with CSS uses the padding property.

input {
  padding-bottom: 40px;
}

label,
input {
  border-style: solid;
  border-width: 2px;
  border-color: red;
}

.stretch {
  display: flex;
  height: 4em;
}
<div class="stretch">
  <label>Stretch alignment</label>
  <input value=":(">
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701