Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - position: absolute; - auto height

Tags:

css

I am having a problem with some div's

The outer div has a min-height, but the inner divs are all varying heights. Because the inner divs are absolute positioned, they do not affect the outer divs height. Is there a way to make these inner divs affect the height of the outer div?

The reason I am styling these divs with position:absolute is so that they all start at the top of the container div.

like image 503
superUntitled Avatar asked May 12 '10 20:05

superUntitled


People also ask

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.)

How do you change position absolute?

Absolute Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.

How do you fix a div to the bottom of another div?

Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.


2 Answers

As far as I know, there's no way for absolutely positioned child elements to affect the height of their statically, or relatively positioned parent elements using only CSS. Either:

  • Reorganize so that the child elements remain in the document flow
  • Use JavaScript on load of the page to set the height of the parent to the height of the largest child

This issue is common in fade-in/fade-out JavaScript slideshows, and from what I've seen either 1) the height of the parent container needs to be defined or 2) the parent container's height is set dynamically for each slide.

like image 190
Andrew Avatar answered Sep 21 '22 11:09

Andrew


I recently had this problem with a fade in/out CSS transition slideshow, and ended up solving it by giving the first child element position: relative; and the others position: absolute; top:0; left: 0; which ensures that the containers height is the same as the height of first element. Since my CSS transition slideshow uses the opacity property the container dimensions never changes during the course of the slideshow.

Alas, since I also needed to supply a javascript fallback for older browsers I had to set the container height for these browsers anyway (because of jQuerys fadeIn/fadeOut actually setting display: none; I would guess).

like image 26
Simon Avatar answered Sep 25 '22 11:09

Simon