Strange spacing issues in CSS that occur when cropping images

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?

View Image 1

View Image 2

Answer №1

According to the response from Максим Мовчан

give this a try

.card { 
  margin-bottom: -40%;
}

Answer №2

Ensure that the a tag has a height set to max-content.

CSS:

a {
  height: max-content;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Resize background image to perfectly fit within div dimensions

One of my divs has a background image that is getting clipped: <div style='text-align:center;background-image: url(/media/img_1_bg.jpg);background-repeat:no-repeat;width:450px;height:900px;' id="mainpage" align="center"> Is there a soluti ...

Guide to integrating the Google identity services library in a React application

I've encountered an issue with my current code, which has suddenly stopped working: import React, { Component } from "react"; import Auth from "../../helper/auth"; import PropTypes from "prop-types"; import logo from &quo ...

Ways to compel divs underneath aligned divs

There are six divs numbered 1 to 6. I want divs 2, 4, and 6 to be positioned below divs 1, 3, and 5 respectively. Is it possible to achieve this layout? Sorry if my explanation is not very clear. Thank you. http://jsfiddle.net/LkGV8/ <body> <d ...

Issue with npm script execution and babel transpilation in a React project started with Create

I am currently in the process of setting up a new React app, which I have not done for quite some time. However, as I run npm run build, an error keeps popping up: SyntaxError: /Users/.../Projects/MyProject/frontend/src/App.js: Support for the experimental ...

How can one change the color of movable tiles to red while keeping the rest of the tiles in their original state in a puzzle game?

In this section of our code, the function moveTile is responsible for moving a tile. How can I modify it so that the hover color changes to red? function moveTile(identity) { var emptyId = findEmptySpace(); if (isEmptyNeighbor(identity)) { document.ge ...

React component props are failing to update

import React,{useEffect} from 'react' import { Pin, Group } from './Pin'; import Cluster from './Cluster'; function DisplayClusterGroup({textAsIcon,iconStyle,mapRef,onViewportChange,dataSet,icon,onClick,clickedMarkers }) { ...

Could you please show me a demonstration of how to add text animation dynamically to a Div element?

I am interested in creating a text animation using JQuery, but I am unsure of how to start and actually code it. Where should I begin or what can/should I search for on Google? If you take a look at , you will notice the moving text and the seamless way t ...

I'm sorry, but your request to access the API from the React localhost has been hinder

Currently, I am delving into the world of React, a JavaScript framework. My task involves fetching a token from an API provided by this service: . To achieve this, I must include my X_CONSUMER_KEY in the request. Here is the approach I am taking using the ...

Is it possible to incorporate a foreach loop while also implementing unique adjustments at specific locations within the code?

I have been searching for a solution to this problem on various forums, but I couldn't find one that fits my needs. My challenge is to create a foreach loop for 12 elements, with 3 of them having a different class applied to them. This is the code I ...

Move my site content to the appropriate location

I am working on a single-paged website built with Jquery and HTML. The main content is housed within a div called #site-content, which has a width of 4400px. Each "page" is positioned within this div, with the first page starting at left: 0px, the second a ...

Integrating Azure Authentication with ASP.NET Core 6 and ReactJS: A Seamless Connection

As I work on integrating a ReactJS website with our estate's web APIs through Azure integrated SSO, a challenge has arisen. The goal is for users to sign on using the React front-end and then use that authentication to access data from our APIs. Howev ...

Removing the transparent section of an image: A step-by-step guide

I am struggling to figure out how to remove the background boxes in this image that I want to add to my website. click here for image Although it appears transparent, there are still gray and white boxes visible in the background. How can I get rid of th ...

Utilizing React MUI to create a custom component with a text input field and a dropdown selection field

Looking to develop a custom component in React MUI that includes a text field and an option selection field. I've explored the library but haven't come across what I need. My attempts at creating it have not been successful, here is my current co ...

The display: flex property is not being properly implemented in Tailwind CSS for the sm:flex

I have a div with the following styles <div class="hidden sm:visible sm:flex"> .... </div> The sm:visible style is applied like this @media (min-width: 640px) .sm\:visible { visibility: visible; } However, the property disp ...

Moving to a different page using properties in React JS

I need to perform a redirect from one page to another while passing props without displaying them in the URL. Here is the method I am currently using: saveForLater(){ if (typeof window !== "undefined") { window.location.href = " ...

"What is the best way to prevent a button from being enabled in Angular if a list or array is not

I am incorporating data from my service in the component: ngOnInit() { this._sharedService. getReceiptItem().takeUntil(this.ngUnsubscribe). subscribe(products => this.receiptItems = products); } Is there a way to deactivate a button if there ...

I encountered a SyntaxError while parsing JSON due to an absence of a number after a minus sign at position 1

I am trying to use the replicate model visoar/product-photo:edf42659dae0da88a26dba4912e7e4bb6c2fba25b1e1c6a5464cf220e467bce0, but when I provide it with an image and a prompt like on this page.tsx: "use client" import { LandingNavBar } from &apo ...

Is it possible for a font to exclude the space character?

I'm currently developing a font detection library that must be extremely compact in size, perfect for direct inclusion on every page of a website. I've managed to shrink it down quite a bit already (compressed to 417 bytes). Take a look at it on ...

Placing a CSS overlay above a jQuery element. How to manage z-index?

I'm looking to superimpose a CSS element over some ongoing Jquery activity in the background. The goal is to have the white box displayed prominently, while the jquery popup text appears neatly within the grey side sections. I want the white box to e ...

When trying to install npx create-react-app, the installation process fails because it does not recognize the npm command

Whenever I attempt to execute npx create-react-app, the following error message appears: 'npm' is not recognized as an internal or external command, operable program or batch file. Installing packages. This might take a couple of minutes. Install ...