已经有很多问题和答案解释了
max-width
和 max-height
的工作原理(例如 this)。基本上,如果父母不受限制,孩子也不受限制。好吧,好吧,但是……
...但是有没有其他方法可以让我限制孩子的大小为父母在孩子根本不存在的情况下的大小?
...
<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 */
}
max-width
) 期间,我通过将子级的
max-height
和 width
更改为父级的 height
和 display: none;
(以像素为单位),将故障转移到 JS。我首先隐藏子项以查看父项的大小,然后限制子项的大小,然后再显示子项。请参阅 JSFiddle 演示中的 JS 函数
refreshMaxWidthAndHeight(wrapper)
我实际上在做什么。
这种方法的缺点:
setInterval
虽然这个演示是我的情况的简化版本,但我故意留下了一些额外的设计切片层来演示:
为什么不可能有演示的当前行为正是我想要实现的行为 编辑:
,它演示了不良行为,它与上面的 JSFiddle 基本相同,但具有改变 max-width
和
max-height
的 JS 解决方法被注释掉。注意显示大表格时整个布局如何被破坏。
问题
max-width
max-height
还有其他方法吗?是否可以避免在每次相关内容更改时使用 JS 来调整 CSS?