Example 1
suggest changeIt 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 */
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents