[英]CSS: Parent div with overlapping child div - can't get parent height right
I'm trying to eliminate the extra space in the #middle-panel
underneath the .box-label
text, but I need to maintain the functionality of the #middle-panel
expanding when I get to smaller screen sizes and the text becomes stacked. 我正试图消除.box-label
文本下面的#middle-panel
的额外空间,但是当我到达较小的屏幕尺寸并且文本堆叠时,我需要保持#middle-panel
功能的扩展。 But if I set a specific height on the #middle-panel
to eliminate the extra space, it no longer expands to accommodate the stacked type. 但是如果我在#middle-panel
上设置一个特定的高度以消除额外的空间,它就不再扩展以适应堆叠类型。
This fiddle shows my current implementation: Current Fiddle 这个小提琴显示了我目前的实现: 当前小提琴
If, in the fiddle, you add height: 65px;
如果,在小提琴中,你增加height: 65px;
to the #middle-panel-inner-div
, you will see the desired amount of space below the text. 在#middle-panel-inner-div
,您将看到文本下方所需的空间量。 Unfortunately, if you then change the width:
of the .red-box-and-label
class from 25% to 20% (to represent what will happen at smaller screen sizes), you will see the text becomes stacked, but the #middle-panel
doesn't expand to accommodate it. 不幸的是,如果然后将.red-box-and-label
类的width:
从25%更改为20%(以表示在较小的屏幕尺寸下会发生什么),您将看到文本堆叠,但#middle-panel
不会扩展以适应它。 Delete the just-added height: 65px;
删除刚添加的height: 65px;
and you will see that the #middle-panel
has now expanded to accommodate the text, but once again with too much space on the bottom. 你会看到#middle-panel
现在已经扩展到容纳文本,但是底部的空间太多了。
I've tried some solutions, such as: Div overlapping & wrong height but none of them seem to work. 我已经尝试了一些解决方案,例如: Div重叠和错误的高度,但它们似乎都不起作用。
Any suggestions would be greatly appreciated! 任何建议将不胜感激!
This thing is happens because height is auto
and parent div expand height depend on child div. 发生这种情况是因为高度是auto
而父div扩展高度取决于子div。 So it takes it's child div height. 所以它需要它的孩子div高度。 Even though you give negative top
value. 即使你给出负面的top
价值。
The solution of this issue is, remove negative top
value and give margin-top
to .red-box-and-label
这个问题的解决方案是,删除负top
值并给.red-box-and-label
赋予margin-top
Remove top: -30px;
删除top: -30px;
from #middle-panel-inner-div
. 来自#middle-panel-inner-div
。 And: 和:
.red-box-and-label {
margin: -30px 0 0;
padding: 0;
width: 25%;
}
Normally you'd need an inner wrapper for the #middle-panel-inner-div
with a negative bottom-margin
to compensate for the 30px
you moved the boxes up with. 通常你需要#middle-panel-inner-div
的内包装, bottom-margin
为负,以补偿你移动盒子的30px
。 But if you place it, you'd have to move all the flex
properties from #middle-panel-inner-div
to the inner wrapper. 但是如果放置它,则必须将所有flex
属性从#middle-panel-inner-div
到内部包装器。 So it's easier to just make an outer wrapper for it, move the top:-30px; position:relative
因此,为它做一个外包装更容易,移动top:-30px; position:relative
top:-30px; position:relative
to it and add the margin-bottom: -30px
to #middle-panel-inner-div
. top:-30px; position:relative
对于它并将margin-bottom: -30px
到#middle-panel-inner-div
。
Here's your updated fiddle . 这是你的更新小提琴 。
Notice the change in markup and the relevant CSS code: 注意标记的变化和相关的CSS代码:
.aWrapper {
top: -30px;
position: relative;
}
#middle-panel-inner-div {
/* top: -30px */
/* position: relative */
margin: 0 auto -30px;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.