20

#container {
  display:flex;
  height:300px;
  background:#333;
}
#child1 {
  width:30%;
  background:#3cf;
}
#child2 {
  width:30%;
  background:#3fc;
}
#child3 {
  width:40%;
  background:#cf3;
}
#child1_child {
  width:100%;
  background:#fc3;
}
pre {
  margin:0px;
}
    <div id="container">
    <div id="child1"><div id="child1_child"><pre>CONTENT<BR>CONTENT<BR>CONTENT<BR>CONTENT</pre></div></div>
    <div id="child2"></div>
    <div id="child3"></div>
    </div>

The height of #child1 is automatically set to the same height of #container, How can I make it to fit to #child1_child rather than 100% height of #container?

The height of #child1_child is not static, it can be changed by content inside of it, height of #child1 with static value is useless.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Fei Sun
  • 361
  • 1
  • 3
  • 10

3 Answers3

13

Try setting min-height or min-width... Magic.

#container {
  display: flex;
  height: 100%;
  min-height: 0;
}

By default, flex items won’t shrink below their minimum content size (the length of the longest word or fixed-size element). To change this, set the min-width or min-height property. (See §4.5 Automatic Minimum Size of Flex Items.) https://www.w3.org/TR/css-flexbox-1/#propdef-flex

Austen Stone
  • 948
  • 1
  • 10
  • 21
11

fit-content is limited in support; IE won't ever support it, and Firefox still requires a prefix. This solution has jQuery for demo purposes only and is not required for the solution. There are four choices:

  1. NONE: No extra styles on .A
  2. FIT-CONTENT: Adds the CSS property fit-content on .A
  3. MAX-CONTENT: Adds the CSS property max-content on .A
  4. TABLE: Adds the CSS property display:table on .A

Option 2 and 3 behave identically , so in conclusion the simplest solution is to apply display:table and it's incredibly compatible as it is simple. height: fit-content and max-content is almost as compatible with one minor caveat being that IE does not support it (IE is going the way of the dinosaur so it's pretty much a non issue).


Demo

$('.rad').on('change', switchStyle);

function switchStyle(e) {
  var pick = $(this).val();
  switch (pick) {
    case 'nil':
      $('.A').removeClass('fit max tab');
      break;
    case 'fit':
      $('.A').addClass('fit').removeClass('max tab');
      break;
    case 'max':
      $('.A').addClass('max').removeClass('fit tab');
      break;      
    case 'tab':
      $('.A').addClass('tab').removeClass('fit max');
      break;
    default:
      break;
  }
}
.box {
  display: flex;
  height: 300px;
  background: #333;
}
.A {
  width: 30%;
  background: #3cf;
}
.B {
  width: 30%;
  background: #3fc;
}
.C {
  width: 40%;
  background: #cf3;
}
.A1 {
  width: 100%;
  background: #fc3;
}
pre {
  margin: 0px;
}
.set {
  position: relative;
  z-index: 1;
  bottom: 300px;
  left: 30%;
  width: 30ch;
  font: 400 16px/1.428 Verdana;
}
.A.fit {
  height: -moz-fit-content;
  height: -webkit-fit-content;
  height: -fit-content;
}
.A.max {
  height: max-content;
}
.A.tab {
  display: table;
}
<div class="box">
  <div class="A">
    <div class="A1"><pre>
    CONTENT
    CONTENT
    CONTENT
    CONTENT</pre>
    </div>
  </div>
  <div class="B"></div>
  <div class="C"></div>
</div>

<fieldset class='set'>
  <label>NONE
    <input class='rad' name='rad' type='radio' value='nil' checked>
  </label>
  <label>FIT-CONTENT
    <input class='rad' name='rad' type='radio' value='fit'>
  </label><br>
  <label>MAX-CONTENT
    <input class='rad' name='rad' type='radio' value='max'>
  </label>  
  <label>TABLE
    <input class='rad' name='rad' type='radio' value='tab'>
  </label>
</fieldset>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Community
  • 1
  • 1
zer00ne
  • 41,936
  • 6
  • 41
  • 68
9

Using of experimental values of the height CSS property is generally discouraged.

If it is individual flexbox child which you want to fill the height according to its content, use align-self: baseline; for it (see the example). If it is all the children, put align-items: baseline; into the parent container.

#container {
  display: flex;
  height: 300px;
  background: #333;
}

#child1 {
  width: 30%;
  background: #3cf;
  align-self: baseline;
}

#child2 {
  width: 30%;
  background: #3fc;
}

#child3 {
  width: 40%;
  background: #cf3;
}

#child1_child {
  width: 100%;
  background: #fc3;
}

pre {
  margin: 0;
}
    <div id="container">
    <div id="child1"><div id="child1_child"><pre>CONTENT<BR>CONTENT<BR>CONTENT<BR>CONTENT</pre></div></div>
    <div id="child2"></div>
    <div id="child3"></div>
    </div>
Neurotransmitter
  • 6,289
  • 2
  • 51
  • 38
  • You don't have to -1 my answer just because I did it to yours. Mine answers the OP clearly. – Neurotransmitter Jan 21 '17 at 08:59
  • Did I seem resentful to you when you pointed out that my original answer was not fully browser compatible? I updated my answer and mentioned that you are the one that pointed that out. Sorry you feel that way. – zer00ne Jan 21 '17 at 09:16
  • Well, I -1'd your answer's original version to point out that it doesn't actually answer the OP and has some serious downfalls. That's what -1 button is for. You -1'd my perfectly working solution just to get my attention. Yeah, this looks kind of "resentful". – Neurotransmitter Jan 21 '17 at 09:43
  • As you should -1, and I updated it. Answering a question and providing a solution aren't always one in the same thing. No, I don't need to get your attention, I needed you to see the update. You are the gentleman that brought it to my attention, correct? I'm not the only one that's been through this page, the OP already got 2 more votes. – zer00ne Jan 21 '17 at 09:50
  • I'm not a "gentleman that brings attention of others" giving out -1s. I just push -1 when I find serious discrepancies in the answers provided. Just like in this case. – Neurotransmitter Jan 21 '17 at 09:58
  • No you didn't bring attention to "others" you brought it to me. I respected your downvote by accrediting you for pointing it out and then updating my answer with not only a working solution but an interactive demo as well. Did you even bother to review the update? Look whatever dude, I didn't downvote you but you made up your mind anyways. Don't bother me, anymore, please. – zer00ne Jan 21 '17 at 10:07
  • Lol. If you get bothered by mere -1s, I politely suggest you to quit SO. – Neurotransmitter Jan 21 '17 at 10:11
  • You are the one ranting I didn't even care about one point I got 11k dude, read this thread. I even said that you should -1 me. Loss of a point isn't bothering me, it's you keep ranting, calm down. I'm leaving and I'll let you rant by yourself, good day. – zer00ne Jan 21 '17 at 10:13
  • Which I did exactly. – Neurotransmitter Jan 21 '17 at 10:14
  • 2
    Worth noting that it's not specifically `baseline` that accomplishes it. For example, if you want it centered while still forming to content height then you can use `center` as the value. – BryanGrezeszak Jul 11 '17 at 17:53
  • Great answer, except you probably want to use `flex-start` and not `baseline`. See here: https://stackoverflow.com/a/34611670/4642943 – Oliver Sep 18 '20 at 10:51