There are already plenty of questions and answers explaining how max-width
and max-height
works (this one for example). Basically, if the parent is unrestricted so is the child. OK, fine, but...
...But is there any other way where I could restrict size of the child to whatever size of parent would have if child was not present at all?
...
<div class="parent"> <!-- size determined by it's parent flexbox -->
<div class="child">
<!-- could have large content -->
</div>
</div>
...
.child {
max-height: 100%; /* not working as needed if parent is unrestricted */
max-width: 100%; /* not working as needed parent is unrestricted */
overflow: auto; /* handle oversized content */
}
Since I was unable to get desired behaviour using CSS, I have failover to JS by changing child's max-width
and max-height
to parent's width
and height
(in pixels) during child is hidden (display: none;
). I first hide child to see what would be the size of parent, then restrict size of child, and then show child back.
See JS function refreshMaxWidthAndHeight(wrapper)
in JSFiddle demo what I'm actually doing.
setInterval
and do checks constantlyJSFiddle demo Although this demo is very much simplified version of my situation, I've deliberately left in some extra layers of design slicing to demonstrate:
NOTE current behaviour of demo is exactly behaviour that I'm trying to achieve
Here is Broken behaviour JSFiddle which demonstrates undesired behaviour, it's basically the same as JSFiddle above but with JS workaround that alters max-width
and max-height
being commented-out.
Note how whole layout breaks when large tables are being displayed.
max-width
/max-height
?Doesn't it boil down to keeping the container size no matter its content? After all your overall layout is determined top-down as you said. So you should not have to recalculate anything if something in the inner layout changes and hence neither should you have to limit the inner content at all in width or height. Just let it overflow:
outer layout <-> layout boundary <-> inner layout
Does https://jsfiddle.net/conv1eau/ recreate your problem or is this too simple?