Solved

Relative container to match height of absolute child

Posted on 2009-05-08
3
880 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ 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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

861 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