Expanding the parent container with 100% height to account for floated content
You used the wrong overflow-y
property for clearing, and you should set a min-height
instead of a regular height. Try this:
#static-content {
background-color: #FFFFFF;
margin: 0 auto;
min-height: 100%; /* Set to minimum height so overflow doesn't get hidden */
overflow-y: hidden; /* HIDE overflow; I know, it doesn't make much sense */
position: relative;
width: 960px;
}
Floating Content by Itself
Given this green box which has a padding of 20px (for visibility), notice how a single red box floated to the left will expand past the boundary of its parent box. This is because floating content doesn't actually take up any "space" in the visual area. All other elements will expand underneath it, and only text will wrap around it.
Clearing Floated Content in the Parent
In order to counter this and make the green box completely encompass the area of its child red box, we can add overflow: hidden
to its styles. This will expand the box down far enough.
Expanding the Parent to 100% Height
You might think that just adding height: 100%
is the simplest way to make it expand to where it needs to be.However, the height
property specifies an absolute height. Since the content which is floated does not actually take up any vertical space, our overflow: hidden
property will cut off all the content that goes past the parent's height.
Using a Minimum Height Instead
Since we want it to expand to at least a 100% height, we can use the min-height
property to force it there and still maintain the "automatic" height needed to make the parent green box fully encompass the child red box, letting it push past the 100% only when it needs too.
How You Were Set Up
All elements, by default, are set to overflow: visible
so that property didn't really change anything. The only difference you had between this and the first example I provided was that you had a height: 100%
set on the element. So the parent was expanding to 100% height but still not encompassing the full height of its child red box.
Expanding a parent div to the height of its children
Try this for the parent, it worked for me.
overflow:auto;
UPDATE:
One more solution that worked:
Parent:
display: table;
Child:
display: table-row;
How to make a floated div 100% height of its parent?
For #outer
height to be based on its content, and have #inner
base its height on that, make both elements absolutely positioned.
More details can be found in the spec for the css height property, but essentially, #inner
must ignore #outer
height if #outer
's height is auto
, unless #outer
is positioned absolutely. Then #inner
height will be 0, unless #inner
itself is positioned absolutely.
<style>
#outer {
position:absolute;
height:auto; width:200px;
border: 1px solid red;
}
#inner {
position:absolute;
height:100%;
width:20px;
border: 1px solid black;
}
</style>
<div id='outer'>
<div id='inner'>
</div>
text
</div>
However... By positioning #inner
absolutely, a float
setting will be ignored, so you will need to choose a width for #inner
explicitly, and add padding in #outer
to fake the text wrapping I suspect you want. For example, below, the padding of #outer
is the width of #inner
+3. Conveniently (as the whole point was to get #inner
height to 100%) there's no need to wrap text beneath #inner
, so this will look just like #inner
is floated.
<style>
#outer2{
padding-left: 23px;
position:absolute;
height:auto;
width:200px;
border: 1px solid red;
}
#inner2{
left:0;
position:absolute;
height:100%;
width:20px;
border: 1px solid black;
}
</style>
<div id='outer2'>
<div id='inner2'>
</div>
text
</div>
I deleted my previous answer, as it was based on too many wrong assumptions about your goal.
Set height 100% but css div does not expand
Just add display property value
of inline-flex
to:
#content
#leftcontent
#rightcontent
Also add size property value
of height:auto
to:
#leftcontent
#rightcontent
#content { background-color: grey; height: 500px; width: auto; display: inline-flex;}#leftcontent { height: auto; background-color: red; display: inline-flex;}#rightcontent { background-color: green; height: auto; display: inline-flex;}
<body> <div id="content"> <div id="leftcontent"> <ul> <li>The height of this div is set to 100</li> <li>List 2</li> <li>List 3</li> </ul> </div> <div id="rightcontent"> <h2>This is my first header.</h2> <p>Content.</p> <h2>This is my second header.</h2> <p>Content 2.</p> </div> </div></body>
How to force child div to be 100% of parent div's height without specifying parent's height?
NOTE: This answer is applicable to legacy browsers without support for the Flexbox standard. For a modern approach, see: https://stackoverflow.com/a/23300532/1155721
I suggest you take a look at Equal Height Columns with Cross-Browser CSS and No Hacks.
Basically, doing this with CSS in a browser compatible way is not trivial (but trivial with tables) so find yourself an appropriate pre-packaged solution.
Also, the answer varies on whether you want 100% height or equal height. Usually it's equal height. If it's 100% height the answer is slightly different.
make div's height expand with its content
You need to force a clear:both
before the #main_content
div is closed. I would probably move the <br class="clear" />;
into the #main_content
div and set the CSS to be:
.clear { clear: both; }
Update: This question still gets a fair amount of traffic, so I wanted to update the answer with a modern alternative using a new layout mode in CSS3 called Flexible boxes or Flexbox:
body { margin: 0;}
.flex-container { display: flex; flex-direction: column; min-height: 100vh;}
header { background-color: #3F51B5; color: #fff;}
section.content { flex: 1;}
footer { background-color: #FFC107; color: #333;}
<div class="flex-container"> <header> <h1> Header </h1> </header>
<section class="content"> Content </section>
<footer> <h4> Footer </h4> </footer></div>
Why doesn't the height of a container element increase if it contains floated elements?
The floated elements do not add to the height of the container element, and hence if you don't clear them, container height won't increase...
I'll show you visually:
More Explanation:
<div>
<div style="float: left;"></div>
<div style="width: 15px;"></div> <!-- This will shift
besides the top div. Why? Because of the top div
is floated left, making the
rest of the space blank -->
<div style="clear: both;"></div>
<!-- Now in order to prevent the next div from floating beside the top ones,
we use `clear: both;`. This is like a wall, so now none of the div's
will be floated after this point. The container height will now also include the
height of these floated divs -->
<div></div>
</div>
You can also add overflow: hidden;
on container elements, but I would suggest you use clear: both;
instead.
Also if you might like to self-clear an element you can use
.self_clear:after {
content: "";
clear: both;
display: table;
}
How Does CSS Float Work?
What is float exactly and what does it do?
The
float
property is misunderstood by most beginners. Well, what exactly doesfloat
do? Initially, thefloat
property was introduced to flow text around images, which are floatedleft
orright
. Here's another explanation by @Madara Uchicha.So, is it wrong to use the
float
property for placing boxes side by side? The answer is no; there is no problem if you use thefloat
property in order to set boxes side by side.Floating an
Demoinline
orblock
level element will make the element behave like aninline-block
element.If you float an element
left
orright
, thewidth
of the element will be limited to the content it holds, unlesswidth
is defined explicitly ...You cannot
float
an elementcenter
. This is the biggest issue I've always seen with beginners, using, which is not a valid value for thefloat: center;
float
property.float
is generally used tofloat
/move content to the very left or to the very right. There are only four valid values forfloat
property i.eleft
,right
,none
(default) andinherit
.Parent element collapses, when it contains floated child elements, in order to prevent this, we use
clear: both;
property, to clear the floated elements on both the sides, which will prevent the collapsing of the parent element. For more information, you can refer my another answer here.(Important) Think of it where we have a stack of various elements. When we use
float: left;
orfloat: right;
the element moves above the stack by one. Hence the elements in the normal document flow will hide behind the floated elements because it is on stack level above the normal floated elements. (Please don't relate this toz-index
as that is completely different.)
Taking a case as an example to explain how CSS floats work, assuming we need a simple 2 column layout with a header, footer, and 2 columns, so here is what the blueprint looks like...
In the above example, we will be floating only the red boxes, either you can float
both to the left
, or you can float
on to left
, and another to right
as well, depends on the layout, if it's 3 columns, you may float
2 columns to left
where another one to the right
so depends, though in this example, we have a simplified 2 column layout so will float
one to left
and the other to the right
.
Markup and styles for creating the layout explained further down...
<div class="main_wrap">
<header>Header</header>
<div class="wrapper clear">
<div class="floated_left">
This<br />
is<br />
just<br />
a<br />
left<br />
floated<br />
column<br />
</div>
<div class="floated_right">
This<br />
is<br />
just<br />
a<br />
right<br />
floated<br />
column<br />
</div>
</div>
<footer>Footer</footer>
</div>
* {
-moz-box-sizing: border-box; /* Just for demo purpose */
-webkkit-box-sizing: border-box; /* Just for demo purpose */
box-sizing: border-box; /* Just for demo purpose */
margin: 0;
padding: 0;
}
.main_wrap {
margin: 20px;
border: 3px solid black;
width: 520px;
}
header, footer {
height: 50px;
border: 3px solid silver;
text-align: center;
line-height: 50px;
}
.wrapper {
border: 3px solid green;
}
.floated_left {
float: left;
width: 200px;
border: 3px solid red;
}
.floated_right {
float: right;
width: 300px;
border: 3px solid red;
}
.clear:after {
clear: both;
content: "";
display: table;
}
Let's go step by step with the layout and see how float works..
First of all, we use the main wrapper element, you can just assume that it's your viewport, then we use header
and assign a height
of 50px
so nothing fancy there. It's just a normal non floated block level element which will take up 100%
horizontal space unless it's floated or we assign inline-block
to it.
The first valid value for float
is left
so in our example, we use float: left;
for .floated_left
, so we intend to float a block to the left
of our container element.
Column floated to the left
And yes, if you see, the parent element, which is .wrapper
is collapsed, the one you see with a green border didn't expand, but it should right? Will come back to that in a while, for now, we have got a column floated to left
.
Coming to the second column, lets it float
this one to the right
Another column floated to the right
Here, we have a 300px
wide column which we float
to the right
, which will sit beside the first column as it's floated to the left
, and since it's floated to the left
, it created empty gutter to the right
, and since there was ample of space on the right
, our right
floated element sat perfectly beside the left
one.
Still, the parent element is collapsed, well, let's fix that now. There are many ways to prevent the parent element from getting collapsed.
- Add an empty block level element and use
clear: both;
before the parent element ends, which holds floated elements, now this one is a cheap solution toclear
your floating elements which will do the job for you but, I would recommend not to use this.
Add, <div style="clear: both;"></div>
before the .wrapper
div
ends, like
<div class="wrapper clear">
<!-- Floated columns -->
<div style="clear: both;"></div>
</div>
Demo
Well, that fixes very well, no collapsed parent anymore, but it adds unnecessary markup to the DOM, so some suggest, to use overflow: hidden;
on the parent element holding floated child elements which work as intended.
Use overflow: hidden;
on .wrapper
.wrapper {
border: 3px solid green;
overflow: hidden;
}
Demo
That saves us an element every time we need to clear
float
but as I tested various cases with this, it failed in one particular one, which uses box-shadow
on the child elements.
Demo (Can't see the shadow on all 4 sides, overflow: hidden;
causes this issue)
So what now? Save an element, no overflow: hidden;
so go for a clear fix hack, use the below snippet in your CSS, and just as you use overflow: hidden;
for the parent element, call the class
below on the parent element to self-clear.
.clear:after {
clear: both;
content: "";
display: table;
}
<div class="wrapper clear">
<!-- Floated Elements -->
</div>
Demo
Here, shadow works as intended, also, it self-clears the parent element which prevents to collapse.
And lastly, we use footer after we clear
the floated elements.
Demo
When is float: none;
used anyways, as it is the default, so any use to declare float: none;
?
Well, it depends, if you are going for a responsive design, you will use this value a lot of times, when you want your floated elements to render one below another at a certain resolution. For that float: none;
property plays an important role there.
Few real-world examples of how float
is useful.
- The first example we already saw is to create one or more than one column layouts.
- Using
img
floated insidep
which will enable our content to flow around.
Demo (Without floating img
)
Demo 2 (img
floated to the left
)
- Using
float
for creating horizontal menu - Demo
Float second element as well, or use `margin`
Last but not the least, I want to explain this particular case where you float
only single element to the left
but you do not float
the other, so what happens?
Suppose if we remove float: right;
from our .floated_right
class
, the div
will be rendered from extreme left
as it isn't floated.
Demo
So in this case, either you can float
the to the left
as well
OR
You can use margin-left
which will be equal to the size of the left floated column i.e 200px
wide.
Set floating div to height: 100%?
You could do it by setting up a table-like scructure of <div>
s in your HTML and then using display: table
, display: table-cell
, etc. This will work because table cells in the same row automatically resize to the same height as the tallest cell. However, IE7 and below do not support display: table
.
jsFiddle
<div id="wrapper">
<div id="inner-wrapper">
<div id="left">
left
</div>
<div id="right">
right
</div>
</div>
</div>
#wrapper { display: table; }
#inner-wrapper { display: table-row; }
#left { display: table-cell; }
#right { display: table-cell; }
I had to remove float: left
and float: right
from #left
and #right
to get the table-cell display to work, since table cells can’t float. However, this has stuck the two divs together. And table cells do not accept margins, only padding. You may need an extra display: table-cell
div between left
and right
to separate them, if you don’t want padding separating them.
Read more about display: table
and family on QuirksMode and on MDN.
Related Topics
Is the :Before Pseudo-Element Allowed on an Input[Type=Checkbox]
How to Use Safe Center with Flexbox
Styling Mat-Select in Angular Material
Scrollbar Color Change in Firefox
CSS Sticky Footers with Unknown Height
What Does Font-Size Really Correspond To
Differencebetween Block and Inline-Block with Width: 100%
Calc() Not Working Within Media Queries
Why Is Backface-Visibility Hidden Not Working in Ie10 When Perspective Is Applied to Parent Elements
How to Colour Backgrounds of Selected Items in HTML Select Options with CSS Only
What's the Difference Between CSS3's :Root Pseudo Class and HTML
Reuse CSS Animation in Reversed Direction (By Resetting the State)
How to Handle 'Double Opacity' of Two Overlapping Divs
CSS Select the First Child from Elements with Particular Attribute
How to Reset a CSS-Counter to the Start-Attribute of the Given List