Link to home
Create AccountLog in
Avatar of Shaye Larsen
Shaye Larsen

asked on

Relative container to match height of absolute child

I have an absolute div inside of a relative container. The height of the absolute child div is dynamic and will change depending on the content. I need the relative container's height to stretch with the height of the absolute child.

I have not been able to get this to work. I am hoping there is a simple css trick but javascript is also welcome.

Please help me to find a solution so that the absolute div doesn't stretch outside of the relative container.

Also, the absolute child has 3 divs inside it. Those divs contain the content that stretches.

Thank you in advance!!!
<style type="text/css">
.wrapper {
position:relative;
width:900px;
height:300px;
}
.child {
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
}
.inner1 {
width:300px;
height:100%;
}
.inner2 {
width:300px;
height:100%;
}
.inner3 {
width:300px;
height:100%;
}
</style>
<div class="wrapper">
	<div class="child1">
		<div class="inner1">Content 1</div>
		<div class="inner2">Content 2</div>
		<div class="inner3">Content 3</div>
	</div>
</div>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Brian Withun
Brian Withun
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Shaye Larsen
Shaye Larsen

ASKER

Thanks!! Perfect! I removed the height of the main wrapper, set it to be 100%, then I made the wrapper float left. I also removed position absolute and just nested everything.

So my markup is as follows:
<style type="text/css">
.wrapper {
background:#DBDBDB;
position:relative;
width:900px;
height:100%;
float:left;
}
.child {
width:100%;
height:100%;
}
.inner1 {
width:300px;
height:100%;
float:left;
}
.inner2 {
width:300px;
height:100%;
float:left;
}
.inner3 {
width:300px;
height:100%;
float:left;
}
</style>
<div class="wrapper">
	<div class="child1">
		<div class="inner1">
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlk jdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlk jdhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlk jdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjd hljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjh lkjdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkj dhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlkj dhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkj dhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjd hljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkj dhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhl kjdhl jskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkj dhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdh ljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlk jdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkj dhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjd hljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjd hljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlk jdhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjd hljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlk j dhljskjsf jf
		Content 1 hsdl fjhsdl jfhsldk fjalfj hlkjdhljskjsf jf Content 1 hsdl fjhsdl jfhsldk fjalfjhlkjdhljskjsf jf
		</div>
		<div class="inner2">Content 2</div>
		<div class="inner3">Content 3</div>
	</div>
</div>

Open in new window

Thanks! For anyone who is interested see my follow up comment for full implementation.