`Ensuring uniform height for card titles`

So here's the scenario I'm dealing with:

https://i.sstatic.net/4f7AR.png

There seems to be an issue with alignment in the dark blue boxes when they are displayed in a flex container. The top box is not aligned properly with the one next to it. How can this problem be resolved? I want the upper container to always align correctly with its neighboring components, and there may be more than two components next to each other.

Flex container (using Material UI Grid component)

<Box>
   <Grid container>
         {resultField.fields?.map((blockField, index) => {
              return (
                   <Grid item xs={12} md={6}>
                        <DarkValueBox label={blockField.label} value={blockField.value} />
                   </Grid>
                   );
          })}
    </Grid>

DarkValueBox Component

export const DarkValueBox = ({ label, value, index }) => {
  const classes = useStyles();

  return (
    <Box sx={{ borderRadius: 5 }}>
      <Box
        sx={{
          backgroundColor: "#0A7BB1",
          color: "#fff",
          px: 2,
          py: 3,
          borderRight: "1px solid #fff",
        }}
      >
        <Typography fontWeight={600} variant="subtitle1">
          {label}
        </Typography>
      </Box>
      <Box sx={{ px: 2, py: 2, border: "1px solid lightgrey" }}>
        <Typography variant="subtitle1">
          {value}
        </Typography>
      </Box>
    </Box>
  );
};

For reference, you can view the recreated problem on CodeSandbox:

https://codesandbox.io/s/add-material-ui-forked-ge42z?file=/src/index.js

Answer №1

If you prefer to avoid cutting labels in order to display as much information to the user as possible, one approach would be to ensure that the blue box always takes the height of the tallest container in the row.

To achieve this, you can add a wrapper class to the container DarkValueBox with the following specifications:

wrapper: {
  height: "100%",
  display: "flex",
  flexDirection: "column"
}

Next, set your box class to take up 100% height:

box: {
 backgroundColor: "darkblue",
 border: "1px solid grey",
 color: "white",
 wordBreak: "break-word", // by the way: it's `break-word`, not `word-break`
 height: "100%"
},

That's all there is to it! You can see it in action here: https://codesandbox.io/s/add-material-ui-forked-24zg7?file=/src/index.js

Answer №2

While exploring your Sandbox, I opted to provide guidance rather than writing the code directly.

You've initialized a Grid using Material UI, which is akin to a FlexBox container.

The absence of size or width declarations for the box or its inner containers is noticeable.

Upon specifying the grid size and wrap direction for the outer 'main' container, you can then define sizes for the internal containers, your label boxes.

This adjustment will properly align them.

To prevent excessive text from overflowing the component and instead display '...', utilize the 'textOverflow' property.

Explore more about Material UI Grid - https://mui.com/components/grid/

Learn how to implement text overflow with ellipsis in ReactJS and IE: CSS: text-overflow: ellipsis with reactJS and IE

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

The navigation bar is not extending to fill the entire width of the screen

I recently created a navigation bar using the nav tag and applied CSS to set the width, border, and padding. However, I noticed that there are some empty pixels on each side of the navigation bar that I can't seem to fill up. Here is the HTML code for ...

When using a custom selection color to highlight underlined text, the underline becomes invisible

I am currently working on a website where I want to customize the text selection color. ::selection { background-color:#00ffff; } However, there seems to be an issue in this specific situation: p::after { content:"Try selecting the above elemen ...

Utilize the useCallback hook within each individual list item component rather than in the parent component

I recently developed a React app that features a list of items with a delete action. As I understand it, by passing this delete action from the parent (the item container), I could utilize "useCallback" in the parent component to avoid unnecessary recreati ...

CSS: Specify `left:0` or `left:0px` to position

Is there a difference between using left:0; and left:0px;? From what I've noticed, both seem to work fine without any issues in Firefox's error console. Just curious if they have the same interpretation. I am working on some JavaScript that invo ...

Images from JSON cannot be loaded in ReactJS

It seems that importing image URLs from a JSON file is not working as expected, resulting in certain issues. However, when importing them using the syntax {require("hardcoded URL")}, it works fine. But if variables are used as shown in the image below or l ...

Adjusting a Form Input Field

There seems to be an issue with the text alignment in my submission form input field, as the text appears a few pixels too low. This is causing certain letters like y, q, and j to bleed below the box in Chrome and only partially show in IE 8. To fix this p ...

Alert: Unauthorized hook call and Exception: Cannot access properties of null (reading 'useState')

I am currently working on a project using ASP.NET Core with React. To bundle my code, I have opted to use Webpack 5. Within the index.jsx file, I have the following code: import { useState } from "react"; function App() { const [value, setV ...

Utilizing the Vuex/Redux store pattern to efficiently share a centralized source of data between parent and child components, allowing for customizable variations of the data as

Understanding the advantages of utilizing a store pattern and establishing a single source of truth for data shared across components in an application is essential. Making API calls in a store action that can be called by components, rather than making se ...

material-ui Dropdown displaying selected item issue

Struggling with my material-ui select box that is powered by a state variable. Despite all my attempts, I just can't seem to display the chosen value when selecting an option. Why could this be happening? All I see is a blank bar every time. Even afte ...

The radio button allows for the selection of multiple items, rather than just one at a time

https://i.sstatic.net/9MEzS.png I have implemented a radio input as shown below <input type="radio" id={el.name} className="form-check-input" name={el.name} data-count={el.name} value={el.id} onChange={this.select_role.bind(this)} style={{margin: "4px ...

How can I display a nested dropdown list within a form using JSON data in React?

Utilizing material ui and formik, I have integrated a form that requires a selection box with nested options to be displayed as shown in the image linked here. (The value selected needs to be submitted upon form submission) To keep it simple: I am aiming ...

Restore the initial content of the div element

Currently, I am utilizing the .hide() and .show() functions to toggle the visibility of my page contents. Additionally, I am using the .HTML() function to change the elements inside a specific div. $('#wrap').html(' <span id="t-image"> ...

Navigate to a different page following a successful login, without needing to refresh the current

Currently working on a login page where I need to redirect the user to another page upon successful login. I attempted using the pathname property for redirection but encountered some issues. If anyone has a recommendation for a library that can assist w ...

Using a Mixin as an Argument in Another Mixin in LESS CSS

Is it possible to pass the declaration of one mixin or style to another mixin as an input parameter? Consider the following example involving animation keyframes. This is how keyframes are typically defined in pure CSS: @-moz-keyframes some-name { fr ...

Enhance your calendar with sleek jQuery styling

Currently, I am utilizing the jQuery-ui.css (smoothness) solely for the calendar CSS. Can anyone provide me with a comprehensive list of all the calendar CSS functions available in it? I'm looking to avoid using any unnecessary CSS that might be causi ...

"An issue arises with jqPlot axes and legend when exporting images, as they are not rendering

After successfully rendering my jqPlot, I encountered an issue when exporting it as an image. The axes text and legend labels aren't displaying correctly in the exported image - the text seems to be shifting. Here's a comparison of the actual plo ...

Having difficulty submitting a form with ajax, while accomplishing the same task effortlessly without ajax

I have been experimenting with submitting a form using ajax to the same .php file. When I submit the form without ajax directly (form action), the database gets updated. However, when I try the same process with ajax, there is no change in the database. H ...

Django framework used for implementing MUI

Can MUI be integrated with a Django framework for server-side rendering? I prefer to stick with Django for server-side operations. While I am aware that MUI is usually associated with client-side rendering through React, I came across a CDN library on mu ...

The inner div appears to have content but is actually measured as 0x0 in size

Despite having content, my main div doesn't have a height or width. I am trying to make the main div fill 100% of the space between the header and footer so that the background image for main is displayed across the entire page excluding the header an ...

Incorporate unique images and titles into shared Facebook links on-the-fly using React JS and Next JS applications

I have been working on my React JS web application that features a dynamic list of articles. I am currently trying to implement a feature that allows users to share the current link, which is the article details page, on Facebook. Despite spending hours ...