My images have transparent areas at the top and bottom, making the actual content size roughly 350 x 350 px in the center of the png. To display this content with rounded corners, I applied the following CSS code:
img {
width: 100%;
height: 60%;
object-fit: cover;
border-radius: 2rem;
}
This code creates a 1:1 aspect ratio image with rounded corners. However, the issue arises when arranging these images in a grid - the <div>
containing the image maintains its original height, resulting in empty spaces within the grid.
Using React and styled-components, here is the equivalent HTML and CSS structure:
The main container for the grid layout:
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr));
grid-gap: 2rem;
}
Each element within the grid is referred to as a card, defined like this:
<div class="card">
<a href="#">
<img src="image-path" alt="" />
<h4>some text</h4>
</a>
</div>
img {
width: 100%;
height: 60%;
object-fit: cover;
border-radius: 2rem;
}
a {
text-decoration: none;
}
h4 {
text-align: center;
padding: 1rem;
}
Attached are some images depicting the issue. How can I resolve this problem?