Solved

Relative container to match height of absolute child

Posted on 2009-05-08
3
878 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Shaye Larsen
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
Brian Withun earned 500 total points
ID: 24338159
Here's a mildly applicable discussion on the subject:

http://www.webmasterworld.com/forum83/7551.htm

[exerpt]
...
What you're looking for is called "shrinkwrapping," a method of letting the width of an element be determined by the width of it's content
...
Option Two: Float the div. That's it. By floating an element but not giving it an explicit width, you trigger a "shrinkwrapping" behavior which will cause the floats' width to be content defined. However, note two things. One, the specs specifically indicate that floats should have explicit widths, so there may be hidden dangers. And two, you really have to be using content with a fixed width, such as a fixed-width table or an image, otherwise the browser may not be sure what to base the width on and you can get inconsistent results
...
[/exerpt]
0
 
LVL 1

Author Comment

by:Shaye Larsen
ID: 24338264
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

0
 
LVL 1

Author Closing Comment

by:Shaye Larsen
ID: 31579533
Thanks! For anyone who is interested see my follow up comment for full implementation.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question