In this case, with height set to auto, the image should retain it’s native aspect ratio. Also a picture should not pixelate unless its native resolution is less than the size it is displayed. so at 200px width, a picture of 100px would pixelate, but anything 200px or more would not.
If you want to force a uniform aspect ratio on a foreground image (which may or may not differ from its native aspect) without stretching or distorting you can use the object-fit css property. But note it does not have universal browser support yet.
In the instagram page the thumbnail images are in a square aspect, even if the original photo is not. I imagine that the images are cropped to square on the server-side as the thumbnails are created rather than by css on the client side.
The fake trick only really works with images that are within a reasonable size but where you have very large images like yours the trick fails as the image is too big (the answer is in my codepen as you have missed out some vital ingredients).
If you know that the width will always be longer than the height then you can do it differently like this:
.squareframe {
width: 150px;
height: 150px;
position: relative;
overflow:hidden;
background:red
}
.squareframe img {
position:absolute;
top:50%;
left:50%;
height:100%;/* if width is greater than height then reverse height and width values*/
width:auto;
transform:translate(-50%, -50%);
}
what I was trying to say is that I am not copying pasting @PaulOB’s code, but grasp the logic and do it on my own so that my brain can adapt the concept. Did you get it now? It’s not that I didn’t look at all in his code.
Thanks for all your help. This is a very difficult concept. It is for the first time that after receiving so much of help I couldn’t successfully implement this.
In Instagram are they managing that through Javascript?