`Ensuring uniform height for card titles`

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

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

Tips for retrieving the most recent information from the Apollo cache

Is there a feature in Apollo client similar to mapStateToProps in Redux? Imagine I have a component that queries data and stores it in the cache. Here's an example: class Container extends React.Component { ... ... render() { ...

Guide on making GET/POST requests with express and react router

I have express set up to serve a static html page where my react components mount. I'm using react router for nested routes, like this: https://i.stack.imgur.com/83bvL.png Within the App component (green outline), I render a Header component (orange ...

Leveraging CSS or JavaScript for Displaying or Concealing Vacant Content Sections

I'm working on developing a page template that utilizes section headers and dynamically pulled content from a separate database. The current setup of the page resembles the following structure: <tr> <td> <h3> ...

What is the best way to nest a div within a flexbox container?

I am attempting to create a div that is based on percentages, and I want to nest a div inside another. Specifically, I want the center (xyz div) to only take up 90% of the height of the content-div. My goal is for the "content" div to be responsive to 90% ...

What is the best way to handle typing arguments with different object types in TypeScript?

Currently, I have a function that generates input fields dynamically based on data received from the backend. To ensure proper typing, I've defined interfaces for each type of input field: interface TPField { // CRM id as a hash. id: string nam ...

Large button in Bootstrap 3 breaks out of Jumbotron on smaller mobile screens

I have set up my layout with a Jumbotron that features a prominent call-to-action button. <a href="#" class="btn btn-lg btn-success">Let's Purchase Something Elegant</a> However, when viewed on an XS mobile screen, the button extends bey ...

Tips for aligning 2 divs in the same position?

I am working on a slider feature that includes both videos and pictures. I need to ensure that the videos are hidden when the device width exceeds 600px. Here is the HTML code snippet: <video class="player__video" width="506" height="506" muted preloa ...

Creating head tags that are less bouncy

After running my code, I noticed that the headlines didn't transition smoothly - they seemed to jump rather than flow seamlessly. To address this issue, I attempted to incorporate fadeIn and fadeOut functions which did improve the smoothness slightly. ...

What is the process for incorporating attribute values when constructing XML with fast-xml-parser?

Latest fast-xml-parser update: version 4.3.6 Description I'm trying to incorporate an xml attribute (tokenized="true") in this format : <custom-tag tokenized="true">test test &gt; 14</custom-tag> Input Code var def ...

Difficulty in handling the event arises when using Material UI IconButton onClick

After installing "@material-ui/core": "^4.9.2" and "@material-ui/icons": "^4.9.1", I encountered an issue in my form with multiple rows, each containing an add and remove button. While the regular Button functioned correctly by removing the row it was clic ...

The client using socket.io is receiving events for "double plus one"

While experimenting with socketio, I encountered a bug that others are experiencing as well but I haven't been able to find a valid solution. This is the server-side code: const app = require('express')(); const server = require('http& ...

How to modify the color of the placeholder text in a select dropdown using Material-ui

I have a material-ui select component that displays a dropdown menu of options for the user to choose from. The placeholder text, which currently reads "select option," appears in light grey to prompt the user to make a selection. However, I am concerned t ...

translateZ function fails to function properly on Firefox browser

I've been experimenting with using translateZ to hide a child element called "list" behind the parent element "div2. It works perfectly in Chrome, but unfortunately, it's not working correctly on Firefox. Can anyone provide some guidance or help ...

Is it possible to implement the ::selection Selector in Material-UI?

Is it possible to change the background color of a selected list component in Material-UI using the ::selection selector? See the code example below: import React from 'react'; import { makeStyles } from '@material-ui/core'; import List ...

How do I go about creating shades when working with material theming?

Hello everyone! I am interested in developing a unique theme with Angular Material theming, incorporating my own set of three colors. Typically, the Angular Material themes are defined with shades like this: $mat-red: ( 50: #ffebee, 100: #ffcdd2, 20 ...

Mixing together an array of colors, experimenting with adding a touch of transparency

Here is my first question, diving right in. I recently created a canvas in HTML and followed a tutorial to generate random floating circles that interact with the mouse position......if you want to check it out, click here. The issue I'm facing now ...

After filling out the form, the user's entered information is instantly shown on the same page

I want to create a form where my friend can input information that will be displayed on the same page. For example, when he enters the title in a text field and clicks submit, it should appear in the title box. Additionally, he should be able to enter va ...

Tips for positioning a Material UI Button and TextField side by side, perfectly aligned on the same line and height

I am currently working on aligning a <Button> from the Material UI library so that it is positioned at the same height as the <TextField> next to it and perfectly aligned with that field. Both the <Button> and the <TextField> are e ...

Is it necessary to include the Roboto font when using MUI?

After reviewing the Material UI documentation, it appears that users are responsible for loading the Roboto font: Material UI is designed to use the Roboto font by default. You may add it to your project with npm or yarn via Fontsource, or with the Googl ...

What is the process for configuring my form to automatically send to my email upon clicking the send button?

I found this code snippet on a website and I'm trying to figure out how to make the 'Send!' button redirect users to my email address with their message, name, and email included. Can anyone help me solve this issue? I attempted to add my e ...