简体   繁体   English

如何使子元素粘在父底部而忽略顶部溢出?

[英]How to make child element sticky to parent bottom ignoring top overflow?

How do you make a child element stick to the bottom of its parent, even when overflowing? 即使溢出,如何使子元素坚持其父元素的底部? Please see this fiddle for a demo. 请观看此小提琴以获取演示。 In the demo the child container is denoted by a red border, as you can see now the child element is sticky to the top of the parent and overflowing from the bottom, which is natural. 在演示中,子容器用红色边框表示,您现在可以看到,子元素粘在父元素的顶部,并从底部溢出,这很自然。 But I want the element to be sticky to the bottom and overflowing from the top. 但是我希望元素在底部保持粘性并从顶部溢出。 I can think of several relevant approaches right off my head: 我可以想到几种相关的方法:

  1. Use position: relative on parent and use position: absolute; bottom: 0px 使用position: relative对于父项,使用position: absolute; bottom: 0px position: absolute; bottom: 0px on child. position: absolute; bottom: 0px的孩子。 This doesn't work in my case since I don't want the child to be taken out of the flow. 在我的情况下,这是行不通的,因为我不希望孩子被带离流程。

  2. As mentioned in this post I can utilize a css table to keep the element in the flow. 正如提到的这个职位 ,我可以利用CSS表,以保持要素的流动。 But this doesn't really produce the desired effect, since I want the element to stick to the bottom of the parent even when overflowing. 但这并不能真正产生预期的效果,因为我希望元素即使在溢出时也要粘在父元素的底部。

The reason I want this effect is so when I animate the height of the parent, I have a nice "dropdown" effect on the child. 我想要这种效果的原因是,当我为父母的身高设置动画时,我对孩子有很好的“下拉”效果。 Is there a way to accomplish the desired layout with css? 有没有办法用CSS完成所需的布局?

OK, I think I get what you are asking. 好的,我想我明白您的要求。 The desired affect can be achieved by changing up your margins, padding, and height on the parent. 可以通过更改父项的边距,填充和高度来实现所需的效果。

.parent {
    padding-top: 200px;
    height: 0;
    background: #e0e0e0;
    overflow: hidden;
}

.child {
    height: 300px;
    font-size: 80px;
    border: 1px solid red;
}

The padding acts as the height in this case. 在这种情况下,填充充当高度。 Animating the height from 0 to height of child (300px) should give the desired affect. 对从0到孩子的高度(300px)的高度进行动画处理应具有所需的效果。

Here's an update to the jsfiddle http://jsfiddle.net/8FeC9/2/ 这是jsfiddle的更新http://jsfiddle.net/8FeC9/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM