Search code examples
htmlcsscss-floatinline-styles

Image tag in the div overflows


I was creating a simple html with a header and logo in it. Im doing this for email templates, so all are inline styles. I noticed there is a float break happening and the image is overflowing its parent.

<div style="width:640px;">
        <!--  header -->
        <div id="header" style="background-color:#005387; height:160px;">
            <div id="logo-container" style="margin-top:20px;margin-left:20px;">
                <img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTPCYwy-3sPJo4XjlB28KVXivC2FlDjYquB1i5Kb7assH9trLoanw">
            </div>
        </div>
    </div>

http://jsfiddle.net/HMswX/8/

Any idea why this is happening? When I add overflow:hidden to #header elem, it works fine. But Im not floating any element within that, then why is there a float break?

EDIT:

Okey, I wasnt clear enough. Updated the code. I want to add margin-top to #logo-container. But when I do that, the whole div comes down, as if the #header is not within the normal flow(which I meant by float-break which usually happens when we float elements inside a parent).


Solution

  • Why not just specify a height on the img?

    <img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTPCYwy-3sPJo4XjlB28KVXivC2FlDjYquB1i5Kb7assH9trLoanw" height="60px">
    

    http://jsfiddle.net/HMswX/2/

    Otherwise just don't spcify a height on the header..

    http://jsfiddle.net/HMswX/3/


    Based on your update..

    The margin isn't working because the div is collapsing.. look at this:

    Float the div.. http://jsfiddle.net/HMswX/10/

    Apply overflow:auto.. http://jsfiddle.net/HMswX/12/


    If you want to read more on collapsing divs see this post same issue..

    Why does this CSS margin-top style not work?