Container div height not resizing? - html

I know there are a hundred and one questions about autosizing a div's height but no suggestions seem to work for me which leads me to believe I'm missing something obvious. Basically I have a container Div, then another smaller div inside (absolutely positioned) and in side that one I have X about of div boxes being created (relatively positioned). The problem is that the height of the main container div won't stretch with it. Any help? Thanks!
.mainContainer{
position: relative;
width:800px;
height:auto;
padding-top:10px;
margin-left: auto;
margin-right: auto;
border-style:solid;
border-width:2px;
}
.smallerDivInsideMainDiv{
position:absolute;
left: 20px;
top: 50px;
width: 600px;
}
.divsThatAreOverflowing{
position: relative;
margin-top:20px;
border-style:solid;
border-width:1px;
}

From what I know, you can't have an absolutely positioned element inside a relatively positioned element and attempt to have the parent height adjust to the absolute child. If you're only using it for positioning with left and right, use margins instead.
<style>
.mainContainer{
width: 800px;
height: auto;
padding-top: 10px;
margin-left: auto;
margin-right: auto;
border-style: solid;
border-width: 2px;
background: #999;
}
.smallerDivInsideMainDiv{
margin-left: 20px;
margin-top: 50px;
background: #CCC;
width: 200px;
}
.divsThatAreOverflowing{
margin-top: 20px;
border-style: solid;
border-width: 1px;
background: red;
height: 300px;
width: 100px;
}​
</style>
<div class="mainContainer">
<div class="smallerDivInsideMainDiv">
<div class="divsThatAreOverflowing"></div>
</div></div>
Here the parent is dark grey, and smaller div inside is light grey, with the overflowing div as red. When it changes height, both of it's containers will adjust.

You should use relative position for the smallerDivInsideMainDiv too and the main div will auto-resize with the total sizes of the contained divs (with their margins too), but it will ignore your "top: 50px;". For that you could add an empty div
<div style='height: 50px;'></div>
after smallerDivInsideMainDiv while still in mainContainer, something like:
.smallerDivInsideMainDiv{
position:relative;
<div class="mainContainer">
<div class="smallerDivInsideMainDiv">
<div class="divsThatAreOverflowing">x1</div>
<div class="divsThatAreOverflowing">x2</div>
</div>
<div style='height: 50px;'></div>
</div>

Related

div includes img as content

I would love your help with an issue I have with my html code.
I have the following code:
<img id="logo" src="images/logo.png">
<div id="content">
<h2>header</h2>
<p>text</p>
</div>
and my css code is:
img#logo {
width: 300px;
position:absolute;
right: 10px;
z-index:-1;
}
div#content {
text-align: center;
border: 2px solid;
border-radius: 25px;
margin: 100px 25 0 25px;
}
My problem is that the div includes the image within its borders (so it pulls the image to the margin of 100px from top.
When I include an <br> after the <img> it won't happen but that isn't the best way to solve it.
Does anybody know how to solve this?
this is happening because you have positioned your image absolutely meaning it is taken out of the flow of the page. If you are just wanting to place the image on the right, try using
float:right;
margin-right:10px;
instead of absolute positioning. You can then ensure the content div appears below the image by adding clear:right to it's styles
top position is missing to the absolute logo image. And a typo in right margin (missing px) -
img#logo {
width: 300px;
position:absolute;
right: 10px;
top: 0;
z-index:-1;
}
div#content {
text-align: center;
border: 2px solid;
border-radius: 25px;
margin: 150px 25px 0 25px;
}
Fiddle

Z-Index Does Not Work on Child Div

Here's the fiddle: http://jsfiddle.net/gBQqQ/
Here's the html:
<div id='testtexture'>
<div id='testinside'>
<div style='vertical-align: top;' class='test'></div>
</div>
</div>
And the css:
.test {
width: 50px;
position: relative;
margin-left: auto;
margin-right: auto;
min-height: 130px;
height:auto;
padding-bottom:50px;
background:blue;
}
#testtexture {
width: 100%;
position: relative;
top: 10px;
}
#testinside {
z-index: 3;
background:red;
position:relative;
}
I do not see why there is an issue. I expect either there is something obvious that I am missing, or there is an underlying issue which means I cannot make the red div go above the blue div- maybe because it is a child of the blue div?
Generally not the best idea to have a child div you want to appear behind it's parent. Usually you would take the child div outside the parent to do this. Nonetheless it is possible. Add z-index:-1 to the child div and remove position:relative from the parent.
HTML
<div id='testtexture'>
<div id='testinside'>
<div class="test"></div>
</div>
</div>
CSS
.test {
position: relative;
z-index: -1;
width: 50px;
margin: 0 auto;
height: auto;
min-height: 130px;
padding-bottom: 50px;
background: blue; }
#testinside { background: red; }
See fiddle: http://jsfiddle.net/gBQqQ/1/
If you use firebug, you can see div.test is still there in the correct position behind it's parent. As a side note, the styling vertical-align you had on a div won't do anything.

Empty div is hidden without any content, how to display it but keep it empty?

So I understand when you set a pixel width and height you can fix this problem of an empty div being hidden, but what about when using width and heights as a percentage?
Here is my code see if you can figure out how to make this div display the background color, but keep it empty... Thanks!
HTML:
<div id="container"></div>
CSS:
#container{
position: relative;
width: 80%;
height: 80%;
margin-left: auto;
margin-right: auto;
margin-top: 10%;
margin-bottom: 10%;
background-color: #E9E9E9;
opacity:0.6; filter:alpha(opacity=60);
}
#container:before {
content:"\00a0\00a0";
}
Demo
I see two options:
insert a inside the div: <div id="container"> </div>
insert min-width:1px; in the CSS
The height is not possible to make bigger without specifing a height number (if the DIV id empty). You can only use height percentage (%) if the parent has a height defined.
mention class attribute in div to refer container style class
<div id="container"></div>
<style>
<!--
#container{
position: relative;
width: 80px;
height: 80px;
margin-left: auto;
margin-right: auto;
margin-top: 10%;
margin-bottom: 10%;
background-color: #E9E9E9;
opacity:0.6; filter:alpha(opacity=60);
}
-->
</style>
check the demo file here: jsfiddle

Have parent DIV height auto-size based on child DIV height

I have a parent container that encapsulates a number of child containers. The child containers have heights depending on the amount of text they hold. I would like the child containers to determine the height of the parent container. So if a child container has a height of 40px, then the parent container height should be 80px. If the child container has a height of 100px, then the parent container height should be 140px (40px fixed + 100px variable).
I've tried leaving the height of the parent container undefined or set as auto or set at 100%, but they all leave my parent container with a height that is too small to hold the child container. If I give my parent container height a set pixel then the problem goes away.
My parent DIV with fixed height(divs behave as expected)
.resultsbox{
width: 800px;
position: relative;
left: 0px;
right: 0px;
margin-left: auto;
margin-right: auto;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #DDD;
border-radius: 5px;
height: 400px;
}
jsFiddle example with set height:
http://jsfiddle.net/Uj5FP/6/
My parent DIV with no height set(parent height isn't enough to hold child)
.resultsbox{
width: 800px;
position: relative;
left: 0px;
right: 0px;
margin-left: auto;
margin-right: auto;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #DDD;
border-radius: 5px;
}
jsFiddle with no height set:
http://jsfiddle.net/Uj5FP/7/
How do I make the parent height auto-adjust correctly?
Here is another attempt. Wrap your absolutely place elements in <div class='absolute-panel'>:
<div class='resultsbox'>
<div class='absolute-panel'>
<div class='reviewtitle'>
<strong>Fun</strong>
</div>
.
.
.
<div class='reviewstatsright'>
<span class='text-info'>
<dt>Rate</dt><dd>$50</dd>
</span>
<span class='text-info'>
<dt>Tip</dt><dd>$10</dd>
</span>
</div>
</div><!-- .absolute-panel -->
<div class='reviewbody'>
<p>What a great time!</p>
<cite>-Rodger</cite>
</div>
</div>
For the CSS:
.absolute-panel {
border: 1px solid blue;
width: 800px;
height: 150px;
position: relative;
left: 0px;
right: 0px;
}
For the .resultsbox, take out the absolute positioning...
.resultsbox {
width: 800px;
margin-left: auto;
margin-right: auto;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #DDD;
border-radius: 5px;
border-left: 1px solid red;
}
For the .reviewbody take out the absolute positioning... and add 40px bottom padding (if needed):
.reviewbody {
margin-top: 20px;
margin-bottom: 20px;
padding-bottom: 40px;
border: 1px dotted red;
}
Comment
I added some colored borders to keep track of things and these can be removed.
The trick is to define a fixed height panel to add all your precisely positioned elements.
After this, add your review body which has a variable height.
The fiddle: http://jsfiddle.net/audetwebdesign/8mRVp/
There are a lot of things on this page you could do different and much simpler, but that is a different conversation.
I think what you are really asking about is clear fix options. The most basic is just adding overflow:hidden to the parent element.
And another option that I use for clear fix is: http://nicolasgallagher.com/micro-clearfix-hack/
The difference is, if you have any content spilling out of the box, overflow:hidden will hide it and micro clearfix will show it.

CSS centered divs side by side

I have this following chunk of my page.
Style:
.featuredcontainer {
width: 450px;
height: 700px;
margin-left: auto;
margin-right: auto;
position: relative;
right: 160px;
top: 30px;
border: 1px groove grey;
}
.navcontainer
{
margin-left: auto;
margin-right: -8px;
position: relative;
top: 75px;
height: 600px;
width: 300px;
}
And example HTML:
<div class="featuredcontainer">
content
</div>
<div class="lessonnavcontainer">
menu
</div>
When the page is displayed. the navcontainer is to the right of (as it should) but under the featuredcontainer. When I move the navcontainer up using relative positioning, it looks right, but there is a bunch of empty space at the bottom of the page. What do I do?
Surround your two divs with a "wrapper" div like so:
<div id="wrapper">
<div class="featuredcontainer">content</div>
<div class="lessonnavcontainer">menu</div>
</div>
Then to center them, add margins to the wrapper:
#wrapper { margin: 0px auto; }
Then to have the two divs be side by side, add a float:
.featuredcontainer { float: left; }
.lessonavcontainer { float: left; }
In order for the centering to work, you need to declare a width on the wrapper:
#wrapper { width: 800px; }
Put both the nav and the featured containers into another wrapper div.
HTML
<div class='wrapper'>
<div class="navcontainer">
menu
</div>
<div class="featuredcontainer">
content
</div>
</div>
And get rid of all the relative positioning. Relative positioning is not recommended for basic layout issues like this. Use floats instead. The wrapper should have a fixed width, which allows it to be centered properly with margin: 0 auto.
CSS
.wrapper{
width:752px;
margin: 0 auto;
overflow:auto;
}
.featuredcontainer {
width: 450px;
height: 700px;
float:left;
border: 1px groove grey;
}
.navcontainer{
float:left;
height: 600px;
width: 300px;
background:#ff0;
}
JSFiddle http://jsfiddle.net/5w5SC/
Use the float property. Using float, css can position divs next to each other horizontally.
.featuredcontainer {
width: 450px;
height: 700px;
margin-left: auto;
margin-right: auto;
position: relative;
right: 160px;
top: 30px;
border: 1px groove grey;
float: left;
}
.navcontainer
{
margin-left: auto;
margin-right: -8px;
position: relative;
top: 75px;
height: 600px;
width: 300px;
float: left;
}
Thats a starting point, try to use float left or float right and see what happens. Fiddle with it until it looks exactly how you want it.
To get them side-by-side you need to add the float attribute in the CSS. To get them to resize with page width you need to add relative widths to them. To center them on the page (horizontally) you need to put the divs inside a relative positioned div in the html. Here is a fiddle. http://jsfiddle.net/Ne5zs/
Be sure to introduce a clearfix (there are many versions of this technique) on any floated object; then center their containing block element using margin: 0 auto.