Is there a way to make absolutely positioned divs expand the container?

后端 未结 2 1323
野性不改
野性不改 2021-01-18 14:25

I have html template - div#container inside body, and some absolutely-positioned divs in container w/o fixed height. Is there a way to expand container to 100% height of pag

相关标签:
2条回答
  • 2021-01-18 15:01

    You'll have problems with explorer (can't remember which versions) but you can set

    left:0;
    right:0;
    top:0;
    bottom:0;
    

    to expand an absolutely positioned element within a container whose dimensions have been set (in it or in a parent)

    0 讨论(0)
  • 2021-01-18 15:07

    The only way I can see is through Javascript

    // returns the coordinates of top/left corner of an element
    function getPosition(obj)
    {
        var left = 0;
        var top = 0;
        if (obj.offsetParent)
        {
            do
            {
            left += obj.offsetLeft;
        top += obj.offsetTop;
        }
        while (obj = obj.offsetParent);
    }
    return {x:left, y:top};
    }
    
    document.onload = function()
    {
        var div = document.getElementById('yourLowestPositionnedDiv');
        var divBottom = getPosition(div).y + div.offsetHeight; // y coordinate of the bottom/left corner
        document.body.style.height = divBottom - getPosition(document.body).y;
    }
    

    This code will expand the size of your body at runtime so that it ends just below your lowest absolute-postioned div.

    0 讨论(0)
提交回复
热议问题