Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust relative div height with respect to inner absolute height?

For example: http://jsfiddle.net/MYvYy/182/

I have a lot of 'inner_box' elements inside of 'outer_box'. Inner_box elements a absolute.

I would like to adjust the outer_box height so that all inner_box elements fit in the outer_box.

I know it can be done with js. But I don't really like adjusting style with scripts.

So I was wondering if it is possible to be done using CSS?

like image 967
Arturs Vancans Avatar asked Jul 15 '12 12:07

Arturs Vancans


People also ask

How do I set relative height in CSS?

If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly. If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow.

How do you set the height of an absolute position in CSS?

Centering div (vertical/horizontally)Make inner div position absolute and give top and bottom 0. This fills the div to available height. (height equal to body.)

Can a div be position absolute and relative?

If you position it as absolute , then you're positioning it relative to its closest ancestor which is fixed or relative ... Clearly, nothing can be absolute and relative at the same time. Either you want the element to position itself based on another ancestor's position or based on the page flow.


2 Answers

I have some workaround for this problem, it may not fit your situation but consider looking at it.
First of all we need to duplicate all absolute positioned div which you want to make the parent extend to its height. So your HTML will look like this.

<div class="outer_box">
    <div class="inner_box">1</div>
    <div class="inner_box ghost">1</div>
</div>

Then we need to add the "ghost div" CSS like so:

.inner_box.ghost{
    visibility: hidden;
    width: 100%;
    top: 0;
    left: 0;
    position: relative;
}
like image 138
Abood Sy Avatar answered Sep 18 '22 05:09

Abood Sy


It's not possible with CSS alone.

Layout flow:

An element with position:absolute is outside of the layout flow of the rest of the page. As far as the relative parent is concerned, the absolute child occupies no space in the layout.

This is very useful if you need to have a pop-up or a nav menu nested inside a container, because it won't affect the layout of the container. That's the sort of use case that position:absolute is well-suited for.

Fixed height:

If you need absolute content to behave as if it's a part of the layout flow, use fixed height. Give the relative parent and the absolute child a fixed height, and avoid placing any variable-height child elements before the absolute child. If variable-height content does precede it, use a relative placeholder div with a fixed height at the location where the absolute child needs to appear.

If position:absolute has to be used and fixed height is not an option, use JavaScript.

like image 39
Matt Coughlin Avatar answered Sep 19 '22 05:09

Matt Coughlin