Skip to content Skip to sidebar Skip to footer

In Html, How Is A Child Element's Percentage-based Size Calculated When Its Parent Has Padding?

In the following example, the outer yellow box is 240px wide (width of 200 plus padding of 20 on left and right). Its red child has width set to 50%. If you measure the red box as

Solution 1:

makes perfect sense (jk). It's because the child div is inheriting the parent's padding.

Solution 2:

I'm not sure exactly what you are trying to do, but why not create a separate class for each div, rather than starting out by styling just plain div? That way the inheriting won't interfere with what you want.

Here's a fiddle: http://jsfiddle.net/B3Mjt/

div.parent {
    background-color:yellow;
    padding-left:20px;
    padding-right:20px;
    width:200px;
    height:200px;
 }
div.child {
    background-color:red;
    width:100%;
    height:50%;
 }

Post a Comment for "In Html, How Is A Child Element's Percentage-based Size Calculated When Its Parent Has Padding?"