Gaurav
Gaurav

Reputation: 8487

The height of div element is not setting automatically

               .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?

Upvotes: 1

Views: 149

Answers (8)

Arnab Shaw
Arnab Shaw

Reputation: 539

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>"

Upvotes: 1

ltiong_dbl
ltiong_dbl

Reputation: 3216

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

Upvotes: 0

CSS Guy
CSS Guy

Reputation: 1984

add float:left; in your block class.

Upvotes: 2

Anri&#235;tte Myburgh
Anri&#235;tte Myburgh

Reputation: 13517

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

Upvotes: 1

SVS
SVS

Reputation: 4275

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/

Upvotes: 1

Jon G St&#248;dle
Jon G St&#248;dle

Reputation: 3904

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

Upvotes: 1

monokh
monokh

Reputation: 1990

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?

Upvotes: 1

Simon McLoughlin
Simon McLoughlin

Reputation: 8465

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

Upvotes: 1

Related Questions