Floating elements within a div, floats outside of div. Why?

The easiest is to put overflow:hidden on the parent div and don’t specify a height:

#parent { overflow: hidden }

Another way is to also float the parent div:

#parent { float: left; width: 100% }

Another way uses a clear element:

<div class="parent">
   <img class="floated_child" src="https://stackoverflow.com/questions/2062258/..." />
   <span class="clear"></span>
</div>

CSS

span.clear { clear: left; display: block; }

Leave a Comment