CSS Adjust Parent DIV size if Absolute Child DIV falls outside boundaries

大憨熊 提交于 2019-12-25 08:38:35

问题


I have a parent div that is a specified width and height. If I have a child div inside that, that is outside the boundaries, is there a way to make the parent div adjust size to fit the child div?

EX:

<div style="width:500px; height:500px; position:relative;">
  <div style="width:300px; height:300px; position:absolute; top:250px;">
    Some Content
  </div>
</div>

As you can see in this example the child div would overflow the parent div on the bottom by 50px. I do not want to use overflow:auto because that just creates scrollbars. I really want the parent div to adjust and add 50px in height to compensate for the child div.


回答1:


Check this out.

http://jsfiddle.net/jURc9/8/

You want to push the child down 250px from the top of the parent so do no use top:250px. Use margin-top:250px; and position:relative;



来源:https://stackoverflow.com/questions/5188625/css-adjust-parent-div-size-if-absolute-child-div-falls-outside-boundaries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!