Excessive space consumption by children in MUI Grid

I am facing an issue with rendering two large blocks of text side by side, which may contain new lines or no whitespace. I have been using Material UI's Grid, but the grids are not behaving as expected.

To illustrate my problem, I have provided a small example in this CodeSandbox link: https://codesandbox.io/s/react-mui-forked-rzzk26?file=/index.js

Although the grid background colors appear correctly, the text is overflowing beyond its bounds as indicated here: https://i.stack.imgur.com/EpCYL.png

How can I make sure that the long text in the blue box wraps to a new line and does not overflow outside its parent container?

Thank you for your help!

Answer №1

To achieve a wrapped layout with specific width using flexbox, try setting the property flexWrap to "wrap". If this does not produce the desired result, please share your code for further assistance.

Answer №2

UPDATE: Following some debugging, I discovered that the issue was related to a Box component wrapping all other elements in my application:

<Box component="main" sx={{ flexGrow: 1, p: 3 }}>
  {children}
</Box>

By adding overflow: "hidden" to this component's style, I was able to resolve the problems.

I found a helpful answer on How to wrap text without spaces in React? which led me to a solution. Although it hasn't completely resolved the issue in my main project, it has helped identify that the problem lies outside of the grid structure.

<Grid container spacing={2}>
      <Grid item xs={6}>
        <Box
          color={"red"}
          style={{
            flexWrap: "wrap",
            wordWrap: "break-word",
          }}
        >
          {requestContent}
        </Box>
      </Grid>
      <Grid item xs={6}>
        <Box
          color={"primary.secondary"}
          style={{
            flexWrap: "wrap",
            wordWrap: "break-word",
          }}
        >
          {responseContent}
        </Box>
      </Grid>
    </Grid>

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

A guide on incorporating translated routes (by translating the route paths) within a NextJS application

I am currently working on a NextJS web application that will be deployed in multiple countries, with each deployment requiring changes only to the env.country variables. However, I am facing an issue with routes that have significant slugs needing translat ...

Issue with displaying nested React Elements from Component

I am currently facing an issue with my Collection component, which is utilizing a sub-component called RenderCollectionPieces to display UI elements. Strangely, I am able to see the data for image.name in the console but for some reason, the UI elements ar ...

What is the best way to incorporate this code snippet into an object's value?

Is there a way to merge the headStyles object into the headText object? I have already injected the headStyles object into headConfig. import { makeStyles } from '@material-ui/core' const headStyles = { backgroundColor:'green', ...

React app experiencing freezing due to custom asynchronous function utilisation

I am currently facing an issue with my personal project where the application freezes under certain circumstances. The initial load works fine, but when I make changes to the code causing React to re-render, the app just freezes. It seems to be related to ...

Update Material-ui to include an inclusive Datepicker widget

I have integrated the Material-ui Datepicker into my website to allow users to download timed event information from a database. The issue I am facing is that when users select two bracketing dates, events for the end date are not showing up correctly. F ...

Implementing Case-Insensitive Routes with NextJS Redirects

I'm currently revamping an entire website that previously supported case-insensitive routes. This means that someone could enter: or and both URLs would lead to the same page. In my implementation using NextJS 11, I have a file in the pages director ...

Issue with React container not connecting properly

I am currently facing an issue with a search bar where the input value is not displaying properly. Even when I check the value in the console, it only shows one character at a time. https://i.stack.imgur.com/Qm0Lt.png My assumption is that there might be ...

Guide on creating 2 select inputs with distinct elements?

Imagine you have two select inputs called 'Favorite Fruits' and 'Least Favorite Fruits'. Both of them contain a list of 5 fruits. // The following code is an example to demonstrate the issue <select name='favoriteFruits'& ...

Every time I try to utilize "useCallback," it results in a TypeError error popping up

I am struggling with an issue related to a const experience that involves creating 6 experiences with their respective popovers. I have been instructed to use useCallback, but every time I attempt to do so, I encounter an error message. This relates to my ...

Struggling to pass front end form data to the backend API in a React and Express.js application?

I am experiencing some issues with saving data from a React form. In the backend, I can only save the auto-generated ID from Mongoose. The API is not saving email and password in the database, as when I log the request body in the server it shows an empty ...

Transforming a shared component in react to incorporate material theme

In my setup, I have two React projects - Parent and Common project. The Common project contains common components like header and footer that are shared between multiple projects. In the Parent project, I have a material theme defined and configured using ...

obtain the content of a TextField element

In my React component that utilizes MaterialUI, I have created a simple form with a text field and a button: export default function AddToDo() { const classes = useStyles(); return ( <div style={{ display: "flex" }} ...

Prevent the use of unnecessary object properties

Currently, my project involves using Typescript and React in conjunction with Material-UI. Specifically, I am utilizing Material-UI's styling solution, which implements CSS in JS methodology in a Hook-like manner as outlined in their documentation: co ...

What is the best way to add a condition to the renderCell function within a material-ui GridColDef component?

I am currently utilizing the Mui Grid component and aiming to display the content of a column based on certain data conditions. Could someone guide me on how to incorporate a conditional statement in the renderCell method? const columns: GridColDef[] = [ ...

The React Router Dom V6 triggers loaders as soon as the router is initialized

I'm currently working on implementing routing into my application using react-router-dom V6, specifically utilizing the new createBrowserRouter function and RouterProvider component. The issue I'm facing is that when attempting to integrate a lo ...

Input a new value into the registration field to update it

My current task involves adding a new text field to a React form using a button. I need this new input text field to be able to store data on a register. How can I go about setting this property? Typically, when creating a text field, you would utilize co ...

Enhance the appearance of the dropdown component in Material UI Select

I am attempting to change the appearance (such as borders and border radius) of the drop-down section in the Material UI Select component. Although the Material UI documentation provides information on how to style different sub-components, there are no s ...

Error in Next.js: Trying to destructure an undefined object in useContext

While attempting to change the state of my cursor in a Next.js app using useContext, I encountered the following error: TypeError: Cannot destructure 'Object(...)(...)' as it is undefined. The goal is to update the state to isActive: true when h ...

The system does not acknowledge the command 'Working' as either internal or external

I encountered an issue after creating a React app with npx create-react-app and then trying to start the application using npm start The error displayed was as follows: npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data- ...

The react decorator for maintaining type safety fails to identify the appropriate ReturnType of the supplied function

I want to enhance the redux connect feature by using it as a decorator for a specific reducer/state. Although I know that redux connect can already be used as a decorator, I am curious about why my custom implementation does not work the way I expect. Her ...