Why Doesn't the Height of a Container Element Increase If It Contains Floated Elements

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:

enter image description here

enter image description here

enter image description here

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 does float do? Initially, the float property was introduced to flow text around images, which are floated left or right. 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 the float property in order to set boxes side by side.

  • Floating an inline or block level element will make the element behave like an inline-block element.

    Demo

  • If you float an element left or right, the width of the element will be limited to the content it holds, unless width is defined explicitly ...

  • You cannot float an element center. This is the biggest issue I've always seen with beginners, using float: center;, which is not a valid value for the float property. float is generally used to float/move content to the very left or to the very right. There are only four valid values for float property i.e left, right, none (default) and inherit.

  • 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; or float: 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 to z-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...

enter image description here

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 to clear 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 inside p 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.

Why is height:auto not working on 2 floating elements?

Float the outer div. that will cover your all height, whatever the inner divs holding heights. But if you will provide your inner div float property. then i will suggest you to use the hack clearfix..

    /* Assuming this HTML structure:

<div class="clear">
<div class="floated"></div>
<div class="floated"></div>
<div class="floated"></div>
</div>
*/

.clear:before, .clear:after {
content: "\0020";
display: block;
height: 0;
overflow: hidden;
}

.clear:after {
clear: both;
}

try this it will sure work

Parent div doesn't recognise child's height if child element contains float:left

You need to clear the float element so that You can use :after & :before with clear:both

#header, #footer, #content-wapper, section {  max-width: 100%;  margin: 0 auto;  text-align: center;}#leftContent{  display: inline-block;  width: 49%;  height: auto;  float: left;}.center-content:before, .center-content:after {  content: "";  clear: both;  display: table;}
input{ width: 98%; height: 40px; border: solid 1px gray; background-color: white;}
.center-content { width: 960px; max-width: 100%; margin: 0 auto; padding: 2vw 0 2vw 0; background-color: #E8E8E8}
<section class="center-content">    <div id="leftContent">        <a><input name="income" type="text" id="income0" placeholder="Main Applicant Annual Income"></a><br>        <a><input name="income" type="text" id="income1" placeholder="Main Applicant Any-other Income"></a><br>        <a><input name="income" type="text" id="income2" placeholder="Second Applicant Annual Income"></a><br>        <a><input name="income" type="text" id="income3" placeholder="Second Applicant Any-other Income"></a><br><br>        <a><button class="btnCal" onclick="calculateMort()">Calculator</button></a>    </div></section>

Parent Height doesn't follow their float children

Add overflow:hidden; to the container:

#mainContainer{
width: 1000px;
/*height: 1000px;*/
height:auto;
margin-left:auto;
margin-right:auto;
background-color: #ff6600;
padding-bottom: 20px;

overflow: hidden; /* <--- here */
}

Because its content is floated, the container div collapses. Using a 'clearfix' class or, as I mentioned, adding overflow:hidden will cause the container to contain the floated elements.

UPDATE Explanation of why this works can be found here: https://stackoverflow.com/a/9193270/1588648

... and here:

In order for them (browsers) to calculate what overflowed the bounds of the block (and thus should be hidden), they needed to know the size of the block. Because these blocks do no have an explicit height set, the browsers used the calculated height of the content instead.

http://www.stubbornella.org/content/2009/07/23/overflow-a-secret-benefit/

CSS make non-floating container height of floated elements inside

If i understand correctly all you need to do to fix this is clear the floats before the container ends.

1. You can simply apply a clearing div before end of container.

<div style="clear:both;"></div>

2. My peronal preference however is to create a clearing class:
(At the end of you stylesheet to maximize specificity).

.CF:before,.CF:after{content:"";display:table;}
.CF:after{clear:both;}

Then simply apply this class to the containing div of any floated elements.
The result is the containg div will clear floats before it closes.


So for you code above.

HTML

<body>
<div class="headerBar">

</div>
<div class="tabArea">

</div>
<div class="container CF">Container
<div id="featureBar">
<div class="feature">Feature 1</div>
<div class="feature">Feature 2</div>
<div class="feature last">Feature 3</div>
</div>
<div class="contentBox" id="medHeight">
ef
</div>
<div class="contentBox" id="largeHeight">
ef
</div>
</div>
<footer>
footer
</footer>
</body>

CSS

.headerBar {
margin: 30px -10px 0 -10px;
height: 100px;
background: black;
}
.tabArea {
margin: 0 -10px 0 -10px;
height: 100px;
background: red;
}
#featureBar {
margin-top: 50px;
}
.feature {
width: 310px;
height: 265px;
margin-right: 10px;
background: green;
float: left;
}
.contentBox {
margin-top: 60px;
width: 100%;
background: purple;
float: left;
}

#medHeight {height: 270px;}
#largeHeight {height: 540px;}

.last{margin-right: 0;}

footer {
margin: 60px -10px 0 -10px;
height: 120px;
background: red;
}
.CF:before,.CF:after{content:"";display:table;}
.CF:after{clear:both;}

DEMO HERE

http://jsfiddle.net/s87uP/4/

CSS: Floating divs have 0 height

It's not because the floating divs doesn't have a height, it's because the floating divs don't affect the size of the parent element.

You can use the overflow style to make the parent element take the floating elements in consideration:

#outer { overflow: auto; }

How to make a div grow in height while having floats inside

overflow:auto; on the containing div makes everything inside of it (even floated items) visible and the outer div fully wraps around them. See this example:

.wrap {  padding: 1em;  overflow: auto;  background: silver; } .float {  float: left;  width: 40%;  background: white;  margin: 0 1%;}
<div class="wrap">  <div class="float">Cras mattis iudicium purus sit amet fermentum. At nos hinc posthac, sitientis piros Afros. Qui ipsorum lingua Celtae, nostra Galli appellantur. Petierunt uti sibi concilium totius Galliae in diem certam indicere. Ambitioni dedisse scripsisse iudicaretur.</div>  <div class="float">Mercedem aut nummos unde unde extricat, amaras. A communi observantia non est recedendum. Quisque ut dolor gravida, placerat libero vel, euismod. Paullum deliquit, ponderibus modulisque suis ratio utitur.</div>  </div>

Why does an inline element accept width and height when floated?

In accordance with CSS rules, when you apply float to an element, in most cases it becomes a block element. Elements that are inline and inline-block will compute to block.

From MDN: enter image description here

Inner left-floating div's do not expand the container div vertically

you need to set overflow for the main div. overflow: auto; this will force the div container to expand and adapt to the content.

#cat_container{
margin:0;
padding:5px;
border:1px solid red;
min-height:200px;
overflow: auto;
height: auto !important;
}


Related Topics



Leave a reply



Submit