Problem with square image height within rectangular div

Asked

Viewed 69 times

-1

I have the following structure:

<style>
display: flex;
flex-direction: column;
align-items: center;
width: 100px;
height: 200px;
padding: 5px;
margin: 42px;
border: 1px solid #CCC;
}
div img {
  width: 50%;
}
</tyle>

<div>
      <img src=''>
</div>

The problem: That one image is square and the div rectangular.

How do I make image NAY stay with the wrong time within div ? I mean, stop staying square because of the width: 50%?

I’ve tried to height: auto in image but it didn’t work out!

I’ve tried to max-width: 50%;in image but it didn’t work either!

  • It is not clear what you want. Could you put an example of how you are now and how you want it to look?

  • What size do you want the image to be inside the div? In this case, how high and how wide is the div...

  • 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!

1 answer

1


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>

  • I understood your answer very well. my mistake was that instead of putting border-box, I had put content-box. I went to border box and it worked, Again, my thank you!

  • @Carlosrocha that the border-box solves is clear, hard to realize that the padding of the father affects the son, this is one of the bizarres of CSS,

  • Yes, it will affect because if the div is content-box (default), then the padding would increase the height of the div in disproportionality with the height of the image since the image is square and the div rectangular. If both were square, then would not give this problem! Understood correct? On the other hand, with border-box, the border becomes the limit!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.