CSS: fit relative positioned parent to height of absolute positioned child

自古美人都是妖i 提交于 2019-12-01 02:38:51

Absolutely-positioned items are logically-associated with their parent, but not "physically". They're not part of the layout, so the parent item can't really see how big they are. You need to code the size yourself, or sniff it with JavaScript and set it at run-time.

You can use this using jquery:

    function sliderheight(){
        divHeight = $('.slide').height();
        $('.slide-container').css({'height' : divHeight});
    }
    sliderheight();

This will resize the 'slide-container' div to have the same height as the 'slide' div.

You can make it responsive by calling the function on the firing of a 'resize' event:

   $(window).resize(sliderheight);

Hope this helps.

Perfect way of doing this is to set the relative parent's dimensions i.e. height & width to fit the absolute children.


http://learn.shayhowe.com/advanced-html-css/detailed-css-positioning/

This guy wrote enough to understand the basics of css positioning. Actually the relative parent is used to position all its absolute child logically. But it does not share the physical position as a result it does not stretch itself to cover the relative children.

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