How to make flexbox child's height to fit to content instead of 100% height of its container

Fei Sun

#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.

zer00ne

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the content of the bottom element of a flexbox to be 100% height its container

From Dev

Flexbox - make flex item child 100% height

From Dev

Flexbox: make child fill 100% height of it's parent in this layout without fixed height or position absolute

From Dev

Make container 100% height despite no content

From Dev

How to fit DataGridView width and height to its content?

From Java

Height 100% on flexbox column child

From Dev

How to make a 100% height container in Bootstrap 3

From Dev

How to make navigation button's height larger than its container's height?

From Dev

How to make this content div 100% of available height?

From Dev

Make a CodeMirror editor and its gutter 100% height of container

From Dev

How do I get this div to height 100%? (flexbox on container)

From Dev

How to make the checkbox to cover 100% width and 100% height of a sqaure container?

From Dev

CSS flexbox layout: child with height 100%?

From Java

How can I make Flexbox children 100% height of their parent?

From Dev

How to make floated div container fit to the div height?

From Dev

CSS: How to make image not exceed its parents height in flexbox?

From Dev

How to make UITextView in section header adjust its height to its content

From Dev

Fit UIView height to its content with AutoLayout

From Dev

HTML <object> fit height of its content

From Dev

Flexbox item height independent of its content

From Dev

CSS - how to make content fit to the width/height of paragraph?

From Dev

How to make div height fit the content using css

From Dev

Make vertical menu height fit 100%

From Dev

Make vertical menu height fit 100%

From Dev

iframe auto adjusting its height to fit to the content height

From Dev

Make parent container the height of a hovered child container

From Dev

Make parent container the height of a hovered child container

From Dev

Is it possible to make an UILabel, UITextView, or similar that always resizes its height to fit its content in Interface Builder without truncating?

From Dev

how to change parent view's height to fit child view

Related Related

  1. 1

    How to get the content of the bottom element of a flexbox to be 100% height its container

  2. 2

    Flexbox - make flex item child 100% height

  3. 3

    Flexbox: make child fill 100% height of it's parent in this layout without fixed height or position absolute

  4. 4

    Make container 100% height despite no content

  5. 5

    How to fit DataGridView width and height to its content?

  6. 6

    Height 100% on flexbox column child

  7. 7

    How to make a 100% height container in Bootstrap 3

  8. 8

    How to make navigation button's height larger than its container's height?

  9. 9

    How to make this content div 100% of available height?

  10. 10

    Make a CodeMirror editor and its gutter 100% height of container

  11. 11

    How do I get this div to height 100%? (flexbox on container)

  12. 12

    How to make the checkbox to cover 100% width and 100% height of a sqaure container?

  13. 13

    CSS flexbox layout: child with height 100%?

  14. 14

    How can I make Flexbox children 100% height of their parent?

  15. 15

    How to make floated div container fit to the div height?

  16. 16

    CSS: How to make image not exceed its parents height in flexbox?

  17. 17

    How to make UITextView in section header adjust its height to its content

  18. 18

    Fit UIView height to its content with AutoLayout

  19. 19

    HTML <object> fit height of its content

  20. 20

    Flexbox item height independent of its content

  21. 21

    CSS - how to make content fit to the width/height of paragraph?

  22. 22

    How to make div height fit the content using css

  23. 23

    Make vertical menu height fit 100%

  24. 24

    Make vertical menu height fit 100%

  25. 25

    iframe auto adjusting its height to fit to the content height

  26. 26

    Make parent container the height of a hovered child container

  27. 27

    Make parent container the height of a hovered child container

  28. 28

    Is it possible to make an UILabel, UITextView, or similar that always resizes its height to fit its content in Interface Builder without truncating?

  29. 29

    how to change parent view's height to fit child view

HotTag

Archive