简体   繁体   中英

position relative content calculate height

I have a position relative container and it's a lot of absolute position div. I want to calculate absolute div's height for the my relative container. How can i do this with css or jquery? I can't see container's red background...

<!DOCTYPE html>
<html>
<body>
<div id="container" style="position:relative;overflow:hidden;width:300px;background-color:red;">
    <div style="position:absolute;top:0px;left:0px;width:100px;">
        Lorem ipsum
    </div>  
    <div style="position:absolute;top:0px;width:100px;left:100px;">
        Lorem Ipsum
    </div>

</div>
</body>
</html>

if the divs are all positioned horizontally (next to each other) and width no left or right margin (which by looking at your code I suppose the conditions are matched) then you could do this: http://jsfiddle.net/U6RXB/1/

var width=0;
$('#container').children('div').each(function(){
    width=width+$(this).width();
});
$('#container').width(width);

if any of the conditions I mentioned are not matched with your code then notice me and I'll fix it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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