vendredi 8 mai 2015

CSS margin-top makes div go out of parent's height

I have problem setting layout because one of my child divs makes goes out of it's parent div.

I have: header with 10% height, container with height 90%, and inside one 'div1' with height set to 90% and margin-top set to 10%. If I remove margin-top everything is ok, if not it goes out of parent size creating scrolls etc. (I want div1 height set, I dont need height set to auto etc.)

JsFiddle: http://ift.tt/1EnThk2

HTML:

<body>
    <div id="header">Header</div>
    <div id="container">
        <div id="div1">1</div>
        <div id="div2">2</div>
    </div>
</body>

CSS:

body, html
{
    color: white;
    margin: 0px;
    padding: 0px;
    height: 100%;
}
#header
{
    background-color: blue;
    height: 10%;
}
#container
{
    width: 100%;
    height: 90%;
    background-color: gray;
}
#div1 {
    background-color: red;
    float: left;
    width: 15.67%;
    margin-top: 10%;
    margin-left: 1.5%;
    height: 90%;
}
#div2 {
    background-color: green;
    float: right;
    width: 100px;
    width: 43.17%;
    margin-right: 3.6%;
}

Aucun commentaire:

Enregistrer un commentaire