Solved

Relative container to match height of absolute child

Posted on 2009-05-08
3
885 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

688 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