How can I make the images inside my centered flexbox parent div, #con
, be a square with side length equal to the width of the parent div? The image-containing div (.block
) is positioned between two text divs (#info
and #links
). I want the images to be squared even if they contain a different number of images. However, usingbackground-image
is not an option in this case. The solution mentioned in this link only works if I specify a specific value for the width (e.g., 300px). This is not an ideal situation as I want it to be dynamic.
var con = $("#con");
$(".block").css("height", con.width(), "width", con.width());
body {
font-size:1rem;
text-align:center;
margin:0;
height:100vh;
display: flex;
align-items: center;
justify-content: center;
overflow:hidden;
}
#con {
width:50%;
max-width:300px;
display:flex;
flex-flow:row wrap;
justify-content:center;
margin:5rem auto;
border:1px solid black;
}
.block {width:100%; overflow:hidden; background:black;}
.block img {
position: relative;
top: 0;
bottom: 0;
left: 0;
right: 0;
object-fit: cover;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<div id="con">
<div id="info">...</div>
<div class="block"><img src="https://dbdzm869oupei.cloudfront.net/img/vinylrugs/preview/60150.png"></div>
<div id="links">...</div>
</div>