1
               .block    
               {
                width:540px;
                margin:20px;
                padding:10px;
                border:1px solid Gray;
               }

              <div id="header" class="block">
                <div id="pe" class="text">
                  <b>Name :</b> <span>King</span><br />
                  <b>Surname :</b> <span>Kong</span>
                </div>
                <div id="area" class="text">
                  <span id="city">Abcs</span><b>/</b>
                  <span id="state">Bcsdf</span>
                </div>
              </div>​

If u run the above code in Jsfiddle, then it shows a border around the text and the important thing is that the height of the block class is auto, so it automatically adjust its height.

But the problem comes when i added the following css :

 #pe
 {
  float:left;
 }
 #area
 {
   float:right;
 }​

Now the height of div.block is not set automatically. Can anybody tell me the problem?

Gaurav
  • 8,367
  • 14
  • 55
  • 90

8 Answers8

2

add float:left; in your block class.

CSS Guy
  • 1,978
  • 3
  • 18
  • 27
1

because float takes them out of the current flow. They are not inside the block div in the same way they where, use positioning and display:inline to get them to line up the way you want

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
1

You can use absolute positioning where by the outer element is set to height:auto and the inner #pe and #area are set to height:100%.

Have a look at this answer: How to make a floated div 100% height of its parent?

Community
  • 1
  • 1
monokh
  • 1,970
  • 3
  • 22
  • 31
1

That's because they're not part of the common flow of the document anymore.

The solution could be to set display: block in the block class and then use position: absolute to position the element within that block by using left: 0 and right: 0

Jon G Stødle
  • 3,844
  • 1
  • 16
  • 22
1

Just add overflow:hidden to class "block".

 .block{
   width:540px;
   margin:20px;
   padding:10px;
   border:1px solid Gray;
   overflow:hidden;
 }

Here is the fiddle: http://jsfiddle.net/rWuBF/

SVS
  • 4,245
  • 1
  • 23
  • 28
1

I would add overflow:hidden to the containing element (#header). That should fix it.

Anriëtte Myburgh
  • 13,347
  • 11
  • 51
  • 72
1

Now You need a Clearfix for it

.clearfix:after{
clear:both;
line-height:0;
height:0;
display:block;
background-color:transparent;
border:none;
content: ".";
visibility:hidden;
 }

html[xmlns] .clearfix {
display: block;
}

* html .clearfix {
height: 1%;
 }

you add it like this

"<div id = "header" class="block clearfix"></div>"
Arnab Shaw
  • 539
  • 3
  • 17
0

Although a bit dirtier, you can also add something that clears both after the floated elements.

<div id="header" class="block">
  <div id="pe" class="text"> ... </div>

  <div id="area" class="text"> ... </div>

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

There are also cleaner "clearfix" variations of this as well, that will let you clear:both without adding non-semantic markup. http://www.positioniseverything.net/easyclearing.html

ltiong_sh
  • 3,186
  • 25
  • 28