Example with percentage:
.outer {
display: inline-block;
outline: 1px solid pink;
}
.inner {
display: inline-block;
height: 250px;
width: 250px;
background: rgba(255, 255, 0, .5);
padding: 6.974%;
}
hello
<div class="outer">
<div class=inner></div>
</div>
world
https://jsfiddle.net/bo3x1cz9/2/ Example with px:
.outer {
display: inline-block;
outline: 1px solid pink;
}
.inner {
display: inline-block;
height: 250px;
width: 250px;
background: rgba(255, 255, 0, .5);
padding: 17.4222px;
}
hello
<div class="outer">
<div class=inner></div>
</div>
world
https://jsfiddle.net/v659df4x/
As you can see in the first example, the .outer
div does not include the padding in its calculated width when setting the padding of .inner
by percentage. The padding is still clearly applied, as can be seen by the yellow square. In both examples, the .inner
div has the same size, so why does the .outer
div not respect its size when using percentages for padding? It works as expected when using px, as can be seen in the second example.