UPDATE
TranslucentCloud indicated that fit-content limited in support. This update has jQuery only for demo purposes and is not required for a solution. Almost a year passed, and I learned something. There are 3 options:
- NO: No extra styles on
#child1 - FIT-CONTENT: Adds CSS
fit-content property to #child1 - TABLE: Adds CSS
display:table property to #child1
Options 2 and 3 behave the same, so the simplest solution is to use display:table and FTW, which is incredibly compatible *, because it's simple.
* Do not add add-ons or fields to #child1 if you want to support Safari 5.1.17 , which is an obsolete browser for 5 years, 5 versions and 1 new engine, it is probably safe to assume that there is no longer a problem.
OLD
Try adding the following to child1 :
height:-moz-fit-content; height:-webkit-fit-content; height:fit-content;
SNIPPET
$('.rad').on('change', switchStyle); function switchStyle(e) { var pick = $(this).val(); switch (pick) { case 'nil': $('#child1').removeClass('fit tab'); break; case 'fit': $('#child1').removeClass('tab').addClass('fit'); break; case 'tab': $('#child1').addClass('tab').removeClass('fit'); break; default: break; } }
#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; } #set { position: relative; z-index: 1; bottom: 75px; left: 30%; width: 30ch; font: 400 16px/1.428 Verdana; } #child1.fit { height: -moz-fit-content; height: -webkit-fit-content; height: -fit-content; } #child1.tab { display: table; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div id="child1"> <div id="child1_child"><pre> CONTENT CONTENT CONTENT CONTENT</pre> </div> </div> <div id="child2"></div> <div id="child3"></div> </div> <fieldset id='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> <label>TABLE <input class='rad' name='rad' type='radio' value='tab'> </label> </fieldset>
source share