Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - relative positioned parent div not stretching to absolute child div height

Tags:

html

css

I've been googling this all morning and can't seem to get it to work:

I have a parent DIV with Relative positioning and a two column child DIV setup inside of it, both positioned Absolute. I need the parent DIV's height to stretch with the content of the inner DIV's.

I have tried putting a .clearfix type bit before the closing tags for #content but I'm not floating anything. I've also tried adding a float attribute to the #content div to no avail. Can anyone point me to the right direction here. Clearly I'm missing something with how the nested displays affect each other.

CSS:

#content {     width: 780px;     padding: 10px;     position: relative;     background: #8b847d; }  #leftcol {     width: 500px;     position: absolute; }  #rightcol {     width: 270px;     position: absolute;     left: 500px;     margin-left: 10px;     text-align: center; } 

HTML:

<div id="content">     <div id="leftcol">         <p>Lorem Ipsum</p>     </div><!-- /leftcol -->     <div id="rightcol">         <img src="images/thumb1.jpg">         <img src="images/thumb2.jpg">     </div><!-- /rightcol -->     <br style="clear:both;"> </div><!-- /content --> 
like image 817
user1054093 Avatar asked Nov 18 '11 15:11

user1054093


People also ask

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

Centering div (vertical/horizontally) This height is equal to the viewport height. 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.

How do you make a child div element wider than the parent div?

In this case, we set the child element's width to be 100% of the viewport width by using a percentage viewport unit (vw), then, we move it to the left side (by the distance of the viewport's half, minus 50% of the width of the parent element) with the left property.


1 Answers

Dark side of the force is a pathway to many abilities some consider to be unnatural.

$(document).ready(function()  {      var objHeight = 0;      $.each($('#content').children(), function(){             objHeight += $(this).height();      });      $('#content').height(objHeight); });​ 
like image 178
tuze Avatar answered Sep 22 '22 17:09

tuze