I don’t expect you to understand the problem, really, I find it very unlikely that you will understand what happens, but basically it is because of the padding
that you used in the container father, that padding
ends up influencing the child who has values in %
.
That values of padding
of the father end up "adding" to the values in %
of the children, since the padding
always adds to the box-model
, as well as the bordas
tb if they add the total width/height of the element, unless you say otherwise, as I explain below.
To solve just what you put box-sizing: border-box;
for the padding value does not change the size of the box-model
, or else you don’t use padding
on the father and use margins
in the son, as I did in the examples below.
div {
display: flex;
flex-direction: column;
align-items: center;
width: 100px;
height: 200px;
padding: 5px;
margin: 42px;
border: 1px solid #CCC;
}
div img {
width: 50%;
}
body {
display: flex;
}
<div style="box-sizing: border-box;">
<img src="https://www.placecage.com/100/100">
<br>
com box-sizing
</div>
<div>
<img src="https://www.placecage.com/100/100">
<br>
sem box-sizing
</div>
<div style="padding: 0;">
<img style="margin: 5px" src="https://www.placecage.com/100/100">
<br>
sem padding no pai<br>
com margin no filho
</div>
It is not clear what you want. Could you put an example of how you are now and how you want it to look?
– Ricardo Ferreira
What size do you want the image to be inside the div? In this case, how high and how wide is the div...
– Robson Alves Moreira Pires
Yes, Square image, div (containner), rectangular. At the time I do at img width: 50% it kind of takes the height of the image together and the magim ends up being distorted!
– Carlos Rocha