Auto height on parent container with Absolute/Fixe

2020-01-24 12:59发布

Im trying to set an automatic height a div that contains 2 child elements, positioned fixed and absolutely respecitvely.

I want my parent container to have an auto height but I know this is hard as the child elements are taken out of the page structure with their positions.

I have tried setting a height to my parent div that works, but with my layout being responsive, when its scaled down to mobile, the height remains the same where as the content within becomes stacked and so the height needs to increase with its children.

Not sure if this makes sense, I dont have my actual code on me atm but ive made a fiddle trying to explain...

http://jsfiddle.net/dPCky/

8条回答
劳资没心,怎么记你
2楼-- · 2020-01-24 13:08

The parent div can not use height:auto when its children are positioned absolute / fixed.

You would need to use JavaScript to achieve this.

An example in jQuery:

var biggestHeight = 0;
// Loop through elements children to find & set the biggest height
$(".container *").each(function(){
 // If this elements height is bigger than the biggestHeight
 if ($(this).height() > biggestHeight ) {
   // Set the biggestHeight to this Height
   biggestHeight = $(this).height();
 }
});

// Set the container height
$(".container").height(biggestHeight);

Working example http://jsfiddle.net/blowsie/dPCky/1/

查看更多
Animai°情兽
3楼-- · 2020-01-24 13:08

If you don't want to use JavaScript, you could refer this answer https://stackoverflow.com/a/33788333/1272106.

Short description: duplicate one element and set visibility to hide to expand the parent.

查看更多
▲ chillily
4楼-- · 2020-01-24 13:14

http://jsfiddle.net/dPCky/32/ - A similar effect using float:left;

http://jsfiddle.net/dPCky/40/ - An even closer result to your desired effect.

If you're willing to change your html, then you could do what I have done above.

I learned positioning from the following tutorials which I would highly recommend to anyone who wants to become a positioning pro in html/css:

http://www.barelyfitz.com/screencast/html-training/css/positioning/

I would generally avoid using javascript where possible when doing something that could potentially have a css or html level fix, if you're willing to adjust your html.

查看更多
劳资没心,怎么记你
5楼-- · 2020-01-24 13:17

An easier way is:

$(".container").height($(document).height());
查看更多
唯我独甜
6楼-- · 2020-01-24 13:21

Related to @Blowsie's answer:

/**
 * Sets the height of a container to its maximum height of its children. This
 * method is required in case
 * 
 * @param selector jQuery selector
 */
function setHeightByChildrenHeight(selector)
{
    $(selector).each(function()
    {
        var height = 0;

        $(this).children("*").each(function()
        {
            height = Math.max(height, $(this).height());
        });

        $(this).height(height);
    });
};
查看更多
Explosion°爆炸
7楼-- · 2020-01-24 13:23

Enjoy this auto height for container that adapts to any device and viewport resize or rotation.

It has been tested with float, inline-block, absolute, margins and padding set to the childs.

<div class="autoheight" style="background: blue">
    <div style="position: absolute; width: 33.3%; background: red">
    Only
        <div style="background: green">
        First level elements are measured as expected
        </div>
    </div>
    <div style="float:left; width: 33.3%; background: red">
    One Two Three Four Five Six Seven Eight Night Ten
    </div>
    <div style="float:left; width: 33.3%; background: red">
    One Two Three Four Five Six
    </div>
</div>

<script>
function processAutoheight()
{
    var maxHeight = 0;

    // This will check first level children ONLY as intended.
    $(".autoheight > *").each(function(){

        height = $(this).outerHeight(true); // outerHeight will add padding and margin to height total
        if (height > maxHeight ) {
            maxHeight = height;
        }
    });

    $(".autoheight").height(maxHeight);
}

// Recalculate under any condition that the viewport dimension has changed
$(document).ready(function() {

    $(window).resize(function() { processAutoheight(); });

    // BOTH were required to work on any device "document" and "window".
    // I don't know if newer jQuery versions fixed this issue for any device.
    $(document).resize(function() { processAutoheight(); });

    // First processing when document is ready
    processAutoheight();
});
</script>
查看更多
登录 后发表回答