a problem with the floats

Discussion in 'CSS' started by pradeep.balua, Jul 17, 2008.

  1. #1
    hi guys , I needed a help from you people for a problem ..

    here it is

    
    <div style="background-color:gray;>
    
    <p> what is this thing in the box</p>
    </div>
    
    
    
    Code (markup):
    when I view this in a browser window the thing displays a paragraph with a gray background.But when I float my <p> element the background color just vanishes .what is the problem .....? can somebody explain me.. I am stuck on this ...
     
    pradeep.balua, Jul 17, 2008 IP
  2. MeetHere

    MeetHere Prominent Member

    Messages:
    15,399
    Likes Received:
    994
    Best Answers:
    0
    Trophy Points:
    330
    #2
    I think float is not a valid parameter for p.. use float in div tag.
     
    MeetHere, Jul 17, 2008 IP
  3. pradeep.balua

    pradeep.balua Active Member

    Messages:
    415
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    float can be used to any element except for those whose position attribute has been set to absolute or fixed.I am not saying this it's the w3c specifications
     
    pradeep.balua, Jul 17, 2008 IP
  4. MeetHere

    MeetHere Prominent Member

    Messages:
    15,399
    Likes Received:
    994
    Best Answers:
    0
    Trophy Points:
    330
    #4
    No idea then.. use the background in p tag.

    OR try correcting the below problem..

     
    MeetHere, Jul 17, 2008 IP
  5. garyc40

    garyc40 Peon

    Messages:
    115
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This is a very simple problem. Change the code to this:

    <div style="background-color:gray; overflow: hidden;">

    <p> what is this thing in the box</p>
    </div>

    Without overflow:hidden, when you float p, the div will not cover the whole p, instead its height is 0. When you set overflow to hidden, this fixes the problem.

    So if you want to preserve the background of the parent of a float, always add overflow:hidden. There are many other ways to achieve the same effect, but this one is the best imho.
     
    garyc40, Jul 17, 2008 IP
  6. pradeep.balua

    pradeep.balua Active Member

    Messages:
    415
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    The thing is whenever we float an element it is taken out of the normal document flow and shifted to the left or right as far as possible .The floated boxes will move to the right or left until their edge meets the containing blocks edges or the outer edge of another float. In this case div acts as a containing block for p ... So when I floated the p it should still be within the div and the div should have accomodated it .How can it's height become zero...?
     
    pradeep.balua, Jul 18, 2008 IP
  7. marinaroz

    marinaroz Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #7
    You'd think that, wouldn't you?

    Put a border on your div and you'll see the problem in all its glory.

    Anyway, a few yet unmentioned solutions (both come with strings attached): 1) Put a float on the div as well. 2) give the div a height.
     
    marinaroz, Jul 18, 2008 IP
  8. garyc40

    garyc40 Peon

    Messages:
    115
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    garyc40, Jul 18, 2008 IP
  9. pradeep.balua

    pradeep.balua Active Member

    Messages:
    415
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #9
    thanks gary for the wonderful link....
     
    pradeep.balua, Jul 18, 2008 IP
  10. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Containers do not naturally wrap around their floats, and instead the bottom of the floats "hang out"... it's too bad someone didn't just say that in the first place : )

    Be aware that if you had had something on that div like a width or height, IE6 would have indeed wrapped that floated p just like you'd expect. Which would have confused you more : )

    So, IE6 wraps floats incorrectly if Haslayout is triggered on the container... and it ignores overflow: hidden. Usually, this is okay-- overflow set to anything other than "visible" wraps floats for your normal browsers and IE6's bug takes care of that.

    But there are a few times where it doesn't-- when Haslayout wasn't triggered. So, if you ever start wrapping your floats with overflow: hidden; and find IE6 not wrapping, make sure you've set off Haslayout (there's a list out there at haslayout.net and some other sites).
     
    Stomme poes, Jul 19, 2008 IP