0

I have the following div, but it wont increase in size according to the content.

<div style="width:250px;border-radius:4px; background:#374953;padding:10px; font-weight:200; clear:both"> <img src="http://li.zoocdn.com/6ac9972d726687d35e8e1c25d2d0a23beaa2ea43_150_113.jpg" width="80" style="float:left; margin-right:10px;" />
  <h2 style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#fff; margin-top:0px; font-weight:200">2 Bedroom Property For Sale</h2>
  <h3 style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#fff; margin-top:0px; margin-bottom:0px; font-weight:200">&pound;1,234,567</h3>
</div>

I've added clear:both to the container div but no change.

Here is a fiddle: http://jsfiddle.net/CFwmh/

putvande
  • 15,068
  • 3
  • 34
  • 50
user1038814
  • 9,337
  • 18
  • 65
  • 86

4 Answers4

3

Your problem is that you have a floating img inside a div, and floating elements don't affect the dimensions of their containers (as you would've maybe expected).

Problem is solved with setting the overflow of the containing the div:

overflow: hidden;

jsFiddle Demo


Also, please consider to stop using inline styles and use CSS files.

Itay
  • 16,601
  • 2
  • 51
  • 72
0

use

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

before end of last div

<div style="width:250px;border-radius:4px; background:#374953;padding:10px; font-weight:200; clear:both"> <img src="http://li.zoocdn.com/6ac9972d726687d35e8e1c25d2d0a23beaa2ea43_150_113.jpg" width="80" style="float:left; margin-right:10px;" />
      <h2 style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#fff; margin-top:0px; font-weight:200">2 Bedroom Property For Sale</h2>
      <h3 style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#fff; margin-top:0px; margin-bottom:0px; font-weight:200">&pound;1,234,567</h3>
    <div style="clear:both"></div>
    </div>
Ankit Agrawal
  • 6,034
  • 6
  • 25
  • 49
0

What you are experiencing is known as the 'clearfix problem'. Most browsers do not take floating children into account when calculating an elemnts height.

There is plenty of information out there about this. This might be a good starting point: What is a clearfix?

In your case, you can solve this in 2 ways.

Method 1:
add an none floating elemnt as a last child into your div, and give it a style of clear: both; Something like this:

<div> 
  <img/>
  <h2>2 Bedroom Property For Sale</h2>
  <h3>&pound;1,234,567</h3>
  <div style='clear:both;'></div>
</div>

The disadvantage of this method is that you are actually adding markup, just for the purpose of styling, which is considered bad practice. That is why i would go for method 2.

Method 2:
Make the parent div self clearing. This can be done by adding overflow:hidden;to it as suggested, but that my produce unwanted results. There are cases where you may want overflow, but still want to clear the div. That is why I have a clearfixclass in my standard stylesheet. It looks like this:

.clearfix:before,
.clearfix:after {
  content: ".";
  display: block;
  height: 0;
  overflow: hidden;
}
.clearfix:after {
  clear: both;
}
.clearfix {
  zoom: 1;
}

And you just need to apply it to the div like this:

    <div class='clearfix'> 
      <img/>
      <h2>2 Bedroom Property For Sale</h2>
      <h3>&pound;1,234,567</h3>
    </div>

This is just an example of a clearfix class, there are many different ones out there. Yhis one seems to work fine for me however.

And an example demonstrating both techniques: http://jsfiddle.net/LGvgj/

Community
  • 1
  • 1
Pevara
  • 14,242
  • 1
  • 34
  • 47
0

It's because you have floated items. clear the last item.

DEMO http://jsfiddle.net/kevinPHPkevin/CFwmh/2/

In this case it's h3

h3 {
    clear: both;
}
Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37