Jump to content

CSS Box margin


rassul

Recommended Posts

<html lang="en">    <head>        <meta charset="utf-8">        <title> Title </title>        <style>            * {                margin: 0;                padding: 0;            }            #outerbox {                background-color: gray;                width: 300px;                height: 300px;            }                #innerbox {                width: 100px;                height: 100px;                background-color: green;                border: 1px solid black;                /*margin: 10px;*/            }        </style>    </head>    <body>        <div id="outerbox">            <div id="innerbox"></div>        </div>    </body></html>

The above code's result is:

post-132566-0-03914800-1393999289_thumb.jpg

but if I uncomment margin for innerbox the result is:

post-132566-0-03914800-1393999289_thumb.jpg

Shouldn't the margin applied to innerbox and not the outerbox?

Thanks in advance.

post-132566-0-50274600-1393999340_thumb.jpg

Link to comment
Share on other sites

Its called collapsing margins, the inner containers margin will seep beyond the edge of outer container, and if the outer container margin is less than inner, the inner container margin is used.

FIX: apply overflow: hidden; to outer container, other options include adding 1px padding.

Link to comment
Share on other sites

Its called collapsing margins, the inner containers margin will seep beyond the edge of outer container, and if the outer container margin is less than inner, the inner container margin is used.

FIX: apply overflow: hidden; to outer container, other options include adding 1px padding.

Thank you. Applied the overflow and it worked.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...