Example 1

suggest change

It is easy to assume that a percentage margin-left or margin-right is relative to the parent’s width.

.parent {
    width : 500px;
    height: 300px;
}

.child {
    width : 100px;
    height: 100px;
    margin-left: 10%;  /* (parentWidth * 10/100) => 50px */
}

That is not the case for margin-top and margin-bottom. Percentage margins on all four sides are resolved against the width of the containing block (here, the parent’s content width), not its height.

So,

.parent {
    width : 500px;
    height: 300px;
}

.child {
    width : 100px;
    height: 100px;
    margin-left: 10%;  /* (parentWidth * 10/100) => 50px  */
    margin-top: 20%;   /* (parentWidth * 20/100) => 100px */
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents