Solved

Relative container to match height of absolute child

Posted on 2009-05-08
3
874 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:the_hero
  • 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:the_hero
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:the_hero
ID: 31579533
Thanks! For anyone who is interested see my follow up comment for full implementation.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now