3

Is this a bug or what?

.page__header {
  background: lightgreen;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: space-between;
  align-content: stretch;
  align-items: stretch;
}
.page__header h1 {
  flex: 1 0;
}

.page__header button {
   order: 5;
}

.page__header div {
  align-self: left;
  order: 2;
  position: relative;
}
.page__header div input {
  min-width: 260px;
  padding: 0 1em;
  height: 100%;
  text-transform: uppercase;
}
<div class='page__header'>
  <h1>title</h1>
  <div>
    <input>
  </div>
  <button>some button</button>
</div>

Chrome, as always, refuses to cooperate.
Does anyone know how to force an input to take the full height of its flex item container?

On Firefox it obeys the 100% height.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
vsync
  • 118,978
  • 58
  • 307
  • 400
  • Seems like anything doesn't get height in percentages in Chrome when inside a Flex item.. again, Chrome suck. – vsync Oct 28 '15 at 10:45
  • 1
    How was this a duplicate of a same question asked *after* this one? just because it randomly got more popular? ridicules, it should have been marked as a duplicate itself back when it was asked :/ – vsync May 24 '20 at 20:16

2 Answers2

4

Does anyone know how to force an input to take the full height of its flex item container?

Here are three options:

Option #1

Make the input a flex item by removing its div parent.

<div class='page__header'>
    <h1>title</h1>
    <input>
    <button>some button</button>
</div>

jsfiddle demo

NOTES:

  • When wrapped by the div, the input is not a flex item. It's a child of a flex item, so it ignores flex properties.
  • In a flex container, the flex items are the in-flow child elements only. Descendants beyond the children are not flex items and ignore flex properties.
  • Once input is a child of the .page__header flex container, default flex properties (like align-items: stretch) apply.

Option #2

Make the div wrapping the input a flex container, converting input into a flex item.

HTML back to original:

<div class='page__header'>
    <h1>title</h1>
    <div>
        <input>
    </div>
    <button>some button</button>
</div>

CSS adjustments:

.page__header div {
    /* align-self: left; */
    order: 2;
    position: relative;
    display: flex; /* NEW */
}

.page__header div input {
    min-width: 260px;
    padding: 0 1em;
    /* height: 100%; REMOVE */
    text-transform: uppercase;
}

jsfiddle demo

NOTES:

  • With the div flex item doubling as a (nested) flex container, default flex properties (like align-items: stretch) will apply to input.
  • height: 100% is removed because it would otherwise override the flex align-* properties.

Option #3

Using percentage heights.

The reason that height: 100% wasn't working for you is because you weren't specifying heights for all parent elements, as required by the spec when using percentage heights. (The fact that it works in Firefox suggest that Chrome adheres more closely to the spec.)

If you're going to use percentage heights, and the element is not absolutely positioned, then you need to specify the height for each parent element up to and including body and the root element (html).

Add this to your CSS:

html, body { height: 100%; }
.page__header { height: 30%; }
.page__header div { height: 100%; }
.page__header div input {  height: 100%; }

jsfiddle demo

NOTES:

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 1
    Indeed, setting the flex item to also have `display:flex` and removing the `height` property from the input solved it. Thanks! – vsync Oct 15 '15 at 20:32
0

You can try taking it out-of-flow:

input {
  position: absolute;
  right: 0;
}

.page__header {
  background: lightgreen;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: space-between;
  align-content: stretch;
  align-items: stretch;
}
.page__header h1 {
  flex: 1 0;
}
.page__header div {
  align-self: left;
  order: 4;
  position: relative;
}
.page__header div input {
  min-width: 260px;
  padding: 0 1em;
  height: 100%;
  text-transform: uppercase;
  position: absolute;
  right: 0;
}
<div class='page__header'>
  <h1>title</h1>
  <div>
    <input>
  </div>
</div>

This may break your layout, though.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • I can't, it's a part of a dynamic structure, I have no idea in what order it will be positioned as a flex item. I wonder if there's an open bug on this.. – vsync Oct 14 '15 at 21:33
  • Bro, your editing my question wasn't cool, now it's not using SASS, and it's not as easy to debug using inspector. inline-code in stackoverflow isn't better than jsbin sometimes . – vsync Oct 14 '15 at 21:38
  • 1
    @vsync Note StackOverflow requires including the code in the question itself. I thought a runnable snippet would be nice, but feel free to remove the snippet, include the SASS code, and a link to jsbin if you want. – Oriol Oct 15 '15 at 08:44
  • Thanks for your time in trying to answering this question friend! I appreciate it. – vsync Oct 15 '15 at 20:10