Search code examples
cssjquery-uipositionheightabsolute

jQueryUI slider: absolutely positioned element & parent container height


I have an example on http://jsfiddle.net/SsYwH/

In case it don't work

HTML:

<div class="container">
   <div class="absolute">
       Testing absolute<br />
       Even more testing absolute<br />
   </div>
   A little test<br />
</div>

CSS:

.container {
    background: green;
}
.absolute {
    position: absolute;
    background: red;
}

Problem

I use jQuery to create a slider-effect. To do that I need to set position absolute.

  • The red block in my code is the position absolute slider.
  • The green block is the container.

I still want the container to be set by it's childs height. Now it don't know it because of the position absolute. Solution?


Solution

  • Then you'll also need to use jQuery to fix the height of the container div. Like so:

    http://jsfiddle.net/khalifah/SsYwH/24/

    $( document ).ready(function() {
        $( ".container" ).each(function() {
            var newHeight = 0, $this = $( this );
            $.each( $this.children(), function() {
                newHeight += $( this ).height();
            });
            $this.height( newHeight );
        });
    });
    

    This is wrong however, since an absolute positioned element can sit outside of it's container. What you really what is something that will find the bottom of the element that sits lowest in the containing div, with respect to the view.