Discovering the largest element within a group to establish the height for the rest

There is a component mapping card elements that look like this:

https://i.stack.imgur.com/CktsE.png

The topmost, leftmost card appears to be missing some height compared to others. Is there a way to determine the height of the tallest components and then set the height of the other cards to match it? I tried the following code snippet but it doesn't seem to work, possibly because it lacks access to the actual rendered size:

Code:

const [maxHeight, setMaxHeight] = useState<number | undefined>();
  useEffect(() => {
    const cards = document.querySelectorAll(".card");
    cards.forEach((card) => {
      if (maxHeight) {
        if (card.getBoundingClientRect().height > maxHeight) {
          setMaxHeight(card.getBoundingClientRect().height);
        }
      }
    });
  }, []);
  

  return (
    <div className="grid grid-cols-1 gap-6 px-2 mx-auto md:grid-cols-3">
      {BestPractices.map((bestPractice) => (
        <Link href={bestPractice.href} key={bestPractice.name}>
          <div
            style={{ height: maxHeight }}
            className="relative p-6 transition-all ease-in-out border rounded-lg drop-shadow-card duration-120 hover:border-brand-dark border-slate-100 bg-slate-100 hover:scale-105 hover:cursor-pointer dark:border-slate-600 dark:bg-slate-800">
            <div
              className={clsx(
                // base styles independent what type of button it is
                "absolute right-6 rounded-full px-3 py-1 text-xs lg:text-sm",
                // different styles depending on type
                bestPractice.category === "Boost Retention" &&
                  "bg-pink-100 text-pink-500 dark:bg-pink-800 dark:text-pink-200",
                bestPractice.category === "Increase Revenue" &&
                  "bg-blue-100 text-blue-500 dark:bg-blue-800 dark:text-blue-200",
                bestPractice.category === "Understand Users" &&
                  "bg-orange-100 text-orange-500 dark:bg-orange-800 dark:text-orange-200"
              )}>
              {bestPractice.category}
            </div>
            <div className="w-12 h-12">
              <bestPractice.icon className="w-12 h-12 " />
            </div>
            <h3 className="mt-3 mb-1 text-xl font-bold text-slate-700 dark:text-slate-200">
              {bestPractice.name}
            </h3>
            <p className="text-sm text-slate-600 dark:text-slate-400">{bestPractice.description}</p>
          </div>
        </Link>
      ))}
    </div>
)

EDIT: Utilizing server components.

Answer №1

Forget using JS to adjust the height. Let CSS handle it effortlessly for you.

Simply add the block class to your <Link /> element and apply the h-full class to its direct child. Don't forget to remove any inline style declarations as well.

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

Designing a spacious and visually appealing interface with the Material UI Grid system, while

I created a personalized library using Material UI. The JSX code I am using is displayed below; <MyLayout container spacing={2}> <MyLayout item xs={9}> <MyComp1 /> </MyLayout> <MyLayout ite ...

Utilizing the DirectusImage tag for a stylish background effect

I'm new to Directus and I need help styling a card with a background image from a Directus Image. Can anyone guide me on how to achieve this? Here's what I have tried: <Card style={{backgroundImage:"url({someColletion.image.id})" &g ...

Determine the width of the window and adjust the positioning of jQuery UI tooltips accordingly

Struggling to adjust the jQuery UI tooltip position based on screen width, but can't seem to figure it out. Can someone assist me in detecting the browser's width and changing the tooltip position accordingly? [fiddle] $(function () { $(doc ...

Updating a legacy Handlebars application with React for a more modern user experience

Recently, I've come across my old Node, Mongo, Express, and Handlebars app which appears to be a bit outdated. The static nature of the app's data being fetched locally from the same domain and rendered on the server side with Handlebars is no lo ...

Error: Unable to authenticate due to timeout on outgoing request to Azure AD after 3500ms

Identifying the Problem I have implemented SSO Azure AD authentication in my application. It functions correctly when running locally at localhost:3000. However, upon deployment to a K8s cluster within the internal network of a private company, I encounte ...

Exploring React-Query's Search Feature

Looking for guidance on optimizing my Product search implementation using react-query. The current solution is functional but could be streamlined. Any suggestions on simplifying this with react-query would be greatly appreciated. import { useEffect, use ...

In a Twitter Bootstrap environment, the button cursor transforms into an I-beam when activated

After utilizing twitter bootstrap to construct a small blog site and integrating a third-party file upload application into the project, I encountered an issue. The CSS files in the file uploader style the "upload button," but when my cursor hovers over it ...

What is the best way to set up unique body backgrounds for each page in WordPress?

My website consists of various pages named as : page-X.php page-Y.php page-Z.php These three pages mentioned above require unique background colors. However, the remaining pages/posts on my site inherit their background-color from the style.css file. ...

Strong tags are not functioning properly within paragraph tags in Firefox

It has come to my attention that when I insert <strong> into a <p>, the text does not appear bold. This issue seems to only occur in Firefox, as Chrome and Safari display it correctly. However, when I place <strong> inside a <li>, e ...

Creating click event handler functions using TypeScript

I encountered an issue when trying to set up an event listener for clicks. The error message I received was that classList does not exist on type EventTarget. class UIModal extends React.Component<Props> { handleClick = (e: Event) => { ...

Changing font styles using the jMenu jQuery plugin: A step-by-step guide

I'm having trouble changing the font-family using CSS with jQuery's "jMenu" plugin. Here is the CSS I currently have: .jMenu{ position : absolute; top : 80px; left : 0px; width : 100%; display:table; margin:0; padding ...

Toggle the visibility of an element by clicking a button

I have a table structured as follows: <tr> <td class="title">Title</td> <td class="body">Body</td> <td class="any">Any text</td> </tr> <tr> <td class="title">Title</td> ...

The art of defining PropTypes for the style attribute in Reactjs

Is there a way to pass the 'style' attribute into my component using JSX syntax? <InputTextWithValidation id="name" style={{width:'100%'}} .../> I'm wondering how I should define the PropTypes for ...

Tips for making sure the Button component in material-ui consistently gives the same ID value for onClick events

Issue arises when trying to log the ID of the Button component, as it only logs correctly when clicked on the edges of the button (outside the containing class with the button label). This problem is not present in a regular JavaScript button where text is ...

Error occurs in private NPM library stating "Failure in parsing module"

I'm currently in the process of setting up my React library and utilizing it locally (via my company's git repository). During development, I can test my application with npm run start, and everything functions as expected. However, when attemp ...

React Navigation error: The variable `isSelectionModeEnabled` cannot be found

I integrated React Navigation in my react-native application and followed the guidelines provided at https://reactnavigation.org/docs/custom-android-back-button-handling/ to customize the behavior of the Android back button. This is the snippet I used for ...

What is preventing MenuItemLink from being displayed in the menu?

I have created a unique page for users to purchase subscriptions, but I am having trouble accessing that page because the button is not appearing in the menu. I followed the steps outlined in the official guide, but only the dashboard and resources buttons ...

What mechanism does material-table utilize to determine which rows have been selected?

In one of my current projects, I'm incorporating the use of material-table. My main focus right now is figuring out how this table identifies selected rows and exploring potential methods for retrieving and altering that specific data. For reference, ...

Disappearing act: CSS3 transform - rotate makes font vanish

When applying a simple css3 transformation rule to an html element which contains various other elements like h1, h2, inputs, and img, I encounter the following issue: div{ -moz-transform: scale(1) rotate(-3deg) translateX(0px) translateY(0px) skew ...

Determine line-height and adjust positions using JavaScript in the Chrome browser

I am creating a basic text reading application with a line to aid in easy reading. The functionality involves using the up and down arrow keys on the keyboard to adjust the position of a red line based on the line-height property. This is accomplished thr ...