Encountering an issue while attempting to maintain image aspect ratio
In a container with height: auto; max-width: 640px
, I aim to showcase four 1:1 aspect ratio images per row
The dilemma arises when the window's/container's width falls below 640 (small screen device), resulting in elongated images
Is this quandary addressable solely through CSS?
The code snippet is provided below
Thank you in advance
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
#container {
border: 1px solid red;
display: flex;
flex-direction: row;
flex-wrap: wrap;
height: auto;
margin: 14px auto;
max-width: 640px;
width: 100%;
}
#container .child {
height: 160px;
overflow: hidden;
width: 25%; /* 100%/4, only 4 image in each row */
}
.child img {
height: 100%;
object-fit: cover;
width: 100%;
}
</style>
</head>
<body>
<div id="container">
<div class="child"> <!-- Image #1 -->
<img src="https://s3.amazonaws.com/cdn0.dutchbulbs.com/images/500/60190.jpg">
</div>
<div class="child"> <!-- Image #2 -->
<img src="https://previews.123rf.com/images/dreamcreation01/dreamcreation011805/dreamcreation01180503640/102424777-it-s-spring-time-banner-with-round-frame-and-flowers-on-blue-sky-background.jpg">
</div>
<div class="child"> <!-- Image #3 -->
<img src="https://i.pinimg.com/originals/11/2b/74/112b746a2182417b2a947d949798c968.jpg">
</div>
<div class="child"> <!-- Image #4 -->
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcT_t3dcCgs9yN7QliI29w0acibdSgVzV9obJw&usqp=CAU">
</div>
<div class="child"> <!-- Image #5 -->
<img src="https://images-na.ssl-images-amazon.com/images/I/41MrYM42sIL.jpg">
</div>
<div class="child"> <!-- Image #6 -->
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSyhn20sg2ZKvx47Gl_1L7sww2W3df_RvjCBA&usqp=CAU">
</div>
<div class="child"> <!-- Image #7 -->
<img src="https://images.unsplash.com/photo-1464982326199-86f32f81b211?ixlib=rb-1.2.1&w=1000&q=80">
</div>
<div class="child"> <!-- Image #8 -->
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcStidi_9IZSp3q2dSqbIOodAd8ib1K3xWMv9g&usqp=CAU">
</div>
</div>
</body>
</html>