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:23

The right way... simple. No Javascript.

<div class="container">
    <div class="faq-cards">    
      <div class="faq-cards__card">Im A Card</div>
      <div class="faq-cards__card">Im A Card</div> 
      <div class="faq-cards__card">Im A Card</div> 
      <div class="faq-cards__card">Im A Card</div> 
      <div class="faq-cards__card">Im A Card</div> 
      <div class="faq-cards__card">Im A Card</div> 
      <div class="faq-cards__card">Im A Card</div>
    </div>
</div>
.container { 
    height: auto;
    width: 1280px;

    margin: auto;
    background: #CCC;
}

.faq-cards { 
    display: flex;
    flex-wrap: wrap;
    justify-content: center;

    position: relative;
    bottom: 40px;

    height: auto;
}

.faq-cards__card {
    position: relative;

    margin: 10px;
    padding: 10px;

    width: 225px;
    height: 100px;

    background: beige;
    text-align: center;
}

https://codepen.io/GerdSuhr/pen/jgqOJp

查看更多
相关推荐>>
3楼-- · 2020-01-24 13:32

A little late to the party, but this may help someone as this is how I resolved the issue recently without JS - IF the children maintain their aspect ratio as they shrink for mobile devices. My example relates to making a jQuery.cycle slideshow responsive, for context. Unsure if this is what you're trying to achieve above, but it involved absolutely positioned children within a container which has 100% page width on mobile and explicit dimensions on larger screens.

You can set the parent's height to use viewport width units (vw), so the height adapts relative to the device's width. Support is broad enough these days that most mobile devices will use these units correctly, bugs and partial support don't relate to vw (but rather, to vmin and vmax in IE). Only Opera Mini is in the dark.

Without knowing what the children are doing between responsive points in this example (the jsfiddle has explicit heights set), let's assume the height of the children scales down predictably relative to the device width, which allows you to fairly accurately assume the height based on aspect ratio.

.container{ height: 75vw; }

http://caniuse.com/#feat=viewport-units

Do note known issue #7 on caniuse if you're going for 100vw as a width measure, but here we're playing with height!

查看更多
登录 后发表回答