Step 1
To ensure a square aspect ratio for the div regardless of its width, set the padding-bottom to 100%. Utilize position: relative/absolute to position a child element that fills the entire container. This CSS technique is commonly used for embedding videos at specific aspect ratios but can be applied to various content types.
I have provided a working example in a fiddle. Note that I used SuitCSS' flex embed component, but you have the flexibility to use any other method.
View Example
HTML:
<div class="FlexEmbed FlexEmbed-ratio">
<div class="UserImage FlexEmbed-content"></div>
</div>
CSS:
/*
* FlexEmbed component from suit-css.
* @see: https://github.com/suitcss/components-flex-embed/blob/master/lib/flex-embed.css
* @see: http://suitcss.github.io/components-flex-embed/test/
* @see: http://alistapart.com/article/creating-intrinsic-ratios-for-video
*/
.FlexEmbed {
display: block;
overflow: hidden;
position: relative;
}
/**
* The aspect-ratio hack is applied to an empty element because it allows
* the component to respect `max-height`. Default aspect ratio is 1:1.
*/
.FlexEmbed-ratio {
display: block;
padding-bottom: 100%;
width: 100%;
}
/**
* Fit the content to the aspect ratio
*/
.FlexEmbed-content {
bottom: 0;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
Step 2
Choose a method to resize images to fit or fill their containers. Consider options like backend image manipulation libraries, CSS background-size: cover property, or JavaScript image cropping through DOM manipulation.
Hoping this information proves useful!