CSS - achieving equal height and width for all flex items without specifying fixed values for height and weight

There are 3 flex items displayed here, but the 2nd item has a wider width due to its lengthy description. https://i.sstatic.net/OLy1r.png

Is there a way to ensure that all flex items have equal height and width without specifying fixed values, especially when the content of the description changes?

App.js

import "./styles.css";

export default function App() {
  const containerStyles = {
    display: "flex",
    flexDirection: "column",
    backgroundColor: "green",
    marginRight: "10px",
    flex: 1
  };

  return (
    <div style={{ display: "flex" }}>
      <div style={containerStyles}>
        <div>Title 1</div>
        <div>Description 1</div>
        <div>Click to view detail</div>
      </div>
      <div style={containerStyles}>
        <div>Title 2</div>
        <div>Description 2 veryyyy longgggggggggggggggggggggg</div>
        <div>Click to view detail</div>
      </div>
      <div style={containerStyles}>
        <div>Title 3</div>
        <div>Description 3</div>
        <div>Click to view detail</div>
      </div>
    </div>
  );
}

Codesandbox
https://codesandbox.io/s/crazy-cookies-nw0ykj

Answer №1

The reason for the large size of the middle box is due to a single unbreakable word loooooong.... When this word overflows onto the next line, everything functions smoothly. To ensure proper handling of overflow, you can set the overflow-wrap property to anywhere.

Try applying this setting to the flex container.

const containerStyles = {
  display: "flex",
  flexDirection: "column",
  backgroundColor: "green",
  marginRight: "10px",
  flex: 1,
  overflowWrap: 'anywhere',
};

This will result in the text being truncated to fit within the container's width while maintaining equal width and height dimensions.

https://i.sstatic.net/EvX0a.png

Answer №2

Your extended term is causing the issue. To resolve this, you must change the overflow setting to something other than visible. Additionally, consider including the word-wrap property to ensure all content remains within view.

const containerStyles = {
    display: "flex",
    flexDirection: "column",
    backgroundColor: "green",
    marginRight: "10px",
    flex: 1,
    "word-wrap": "break-word",
    overflow: "hidden"
  };

Answer №3

After realizing the importance of having equal width, I have updated my response accordingly.

In this scenario, implementing both justify-content: space-between will ensure that the child elements within the flexbox have the same height, and using overflow-wrap: anywhere will allow longer text to be wrapped, resulting in uniform width for all flex items.

https://i.sstatic.net/KAG3D.png https://i.sstatic.net/oNt5P.png

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

Modifying only specific state value in react-redux using the useDispatch function

Currently, I am implementing Google OAuth for authentication in a NextJS project using next-auth. Upon login, I store the user's name and email in the state. However, the issue arises when I attempt to update only these two fields within the state obj ...

Modifying the dimensions of mat-card in Angular Material

https://i.stack.imgur.com/CP16N.png I am struggling to make both components the same height, similar to the left form. I have tried adjusting margins and padding but nothing seems to work. Below is the HTML code for the parent element: <mat-tab label= ...

Error Encountered: Invalid Parameter Type when Retrieving Item from AWS Dynamo

I've been facing issues when trying to establish a connection between my ReactJS application and AWS DynamoDB. Despite confirming the correctness of the API key, secret key, and region, I keep encountering an InvalidParameterType error. I have even at ...

The Javascript and CSS code for a button fails to trigger the animation function after the initial click

Is it possible to trigger the animation function multiple times after the initial click without making changes to the HTML code? The ID and class in the code must remain as they are. I attempted to use the display property set to none, but it did not yie ...

Getting the parameters of a path from the URL's breadcrumb in Vue.js

Currently utilizing Vue Attempting to include path parameters in breadcrumb URLs for my app's router. Looking to achieve something similar to the following: { path: 'pages/gestion/region/:reg', name: 'gestion-depa', ...

What is the definition of "top of the page" within a PHP document?

One source claimed that ob_start() should be placed at the top of the page, while another insisted that session_start() should take precedence. I also came across a guide stating that header() belongs at the beginning of the page. Additionally, I read th ...

Failed verification of C-Lang in React-Hardware/Particle

Currently, I am utilizing React, React Hardware ([https://github.com/iamdustan/react-hardware/]), and Johnny-Five along with the Particle Photon. The error stack displayed below is what pops up when executing my lib/app.js file: # Fatal error in ../deps/v ...

Ensure that Roboto font is utilized on IE 11 and Edge browsers

My goal is to implement the Google Roboto font in my project, but I'm noticing that it doesn't display properly in IE11 / Edge browsers. Below is a snippet of my code: <!DOCTYPE html> <html> <head lang="en"> <meta charse ...

Avoid displaying hidden images or loading content upon clicking a tab

Check out my website here I have created four tabs on my website, and I want each tab to display a different gallery (Nextgen Pro gallery) when clicked. However, the performance is currently very poor because I am loading all the images at once. I plan to ...

Utilizing the filter function iteratively within an Angular factory component

I am facing an issue where I have a factory with 2 controllers. The first controller returns an entire array, while the second controller is supposed to return only one item based on a specific filtering expression. I need to extract the last two parameter ...

Tips on dividing and recycling mongodb connection in Node.js

I am currently troubleshooting the connection to MongoDB using Node.js. The code I have in a file named mongodb.js is as follows: const mongoClient = require('mongodb').MongoClient; const env = process.env.NODE_ENV || 'development'; co ...

Unable to make updates during an ongoing date transition when utilizing react-navigation and redux in a react-native application

Recently, I've delved into learning react-native and redux. My current project involves setting up an authentication scenario with the help of react-navigation. import { authenticate } from './../actions'; const Component = (props) => ...

Is there a way to retrieve all "a" tags with an "href" attribute that contains the term "youtube"?

My goal is to capture all a tags that have the href attribute containing the word youtube. This task requires the use of jquery. ...

Inserting images into a MySQL database using Ajax and file uploads

I am using JQuery Mobile, PHP, and Ajax to upload an image and insert it into a database, but I am facing an issue with inserting the image's name in the database. The result in the database shows "C:fakepathLighthouse.jpg", but I only want to insert ...

connecting a JavaScript object literal to a specific address within an HTML document

I'm encountering an issue with creating an object that contains properties such as {"name":"John Doe","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e08e818d85a0848f8d81889c8b848d8a">[email protected]</ ...

Transitioning from LESS to SASS involves adapting responsive breakpoints

Adapting from Less to Sass has been my current focus. I have defined responsive breakpoints in Less variables: /* Breakpoints */ @break1:~"(max-width: 570px)"; @break2:~"(min-width: 571px) and (max-width: 1002px)"; @break3:~"(min-width: 1003px)"; These ...

I am looking to switch themes on the homepage using a button from an imported component

I have successfully created a toggle button on my main page in app.js to switch between dark and light themes. However, I am facing difficulty in putting the button inside my nav component and using it from that page imported in app.js. Can anyone guide me ...

Retrieving JSON Data using Fetch

My JSON file consists of a simple object containing 2 arrays: { "key1": ["a", "b", "c"], "key2": [ "d", "e", "f"] } When using JavaScript, I fetch the data and push it into an array to use it. const jsonData = '/things.json'; const myArray = ...

Challenges with implementing CSS transitions

I've been delving into CSS3 transitions, and I'm encountering some confusion. I can't tell if I'm misunderstanding something or if it's the browsers causing issues. Initially, I believed Opera had transition support starting from ...

There was a critical error during compilation: java.lang.Il legalAccessError. It appears to be stemming from the class lombok.javac

Even after installing Lombok, updating Java, and setting all necessary System Variables for Maven and JAVA_HOME, I still encounter an error. The POM builds fine and all essential files like mvnw and mvnw.cmd are functioning properly. However, when attempti ...